Description
From jims...@gmail.com on December 10, 2009 17:15:39
[formerly a comment in Issue 73 : Fix schema.View.define decorator]
I've had a go at implementing reduce functions using this decorator (after
having applied the patch from issue 73 ). Basically, the fun passed to
view_wrapped is a higher-order function that returns one or more
functions. The first is taken to be the map_fun, and if it exists, the
second is taken as the reduce_fun. fun.name is taken as the name of
the view. Here is the modified define method:
@classmethod
def define(cls, design, name=None, language='python', wrapper=DEFAULT,
**defaults):
"""Factory method for use as a decorator."""
def view_wrapped(fun):
funs = fun() # extract the map & reduce functions
if type(funs) != list:
funs = [funs]
map_fun = funs[0]
reduce_fun = None
if len(funs) > 1:
reduce_fun = funs[1]
return cls(design, name=fun.__name__,
map_fun=map_fun, reduce_fun=reduce_fun,
language=language, wrapper=wrapper,
**defaults)
return view_wrapped
example usage:
@View.define('doc_name')
def view_name():
def fun_map(doc):
yield None, doc
def fun_reduce(keys, values, rereduce):
return values
return [fun_map, fun_reduce]
or, without a reduce phase:
@View.define('doc_name')
def view_name():
def fun_map(doc):
yield None, doc
return fun_map
Any thoughts? I've tested the above code, and it works as expected. I can
produce a patch if necessary.
Original issue: http://code.google.com/p/couchdb-python/issues/detail?id=105