[Python] curry?
Rob Hudson
rob at euglug.net
Thu Oct 5 14:26:57 PDT 2006
What's curry?
I read a short article on it but didn't "get it". I think I understand
the basic concept but am failing to find a use case for it. Seeing
examples of the application helps me understand things better.
Here's an example in Django that I'm looking at:
def inclusion_tag(self, file_name, context_class=Context,
takes_context=False):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if takes_context:
if params[0] == 'context':
params = params[1:]
else:
raise TemplateSyntaxError, "Any tag function
decorated with takes_context=True must have a first argument of 'context'"
class InclusionNode(Node):
def __init__(self, vars_to_resolve):
self.vars_to_resolve = vars_to_resolve
def render(self, context):
resolved_vars = [resolve_variable(var, context) for
var in self.vars_to_resolve]
if takes_context:
args = [context] + resolved_vars
else:
args = resolved_vars
dict = func(*args)
if not getattr(self, 'nodelist', False):
from django.template.loader import get_template
t = get_template(file_name)
self.nodelist = t.nodelist
return self.nodelist.render(context_class(dict))
compile_func = curry(generic_tag_compiler, params,
defaults, func.__name__, InclusionNode)
compile_func.__doc__ = func.__doc__
self.tag(func.__name__, compile_func)
return func
return dec
Thanks,
Rob
More information about the Python
mailing list