On Thu, Apr 30, 2009 at 03:27:10PM -0700, Philip Jenvey wrote: > Unfortunately our @jsonify just sucks in that you can't specify args > to dumps. Otherwise this would be even slightly easier than Domhnall's > example: > > # Don't even need a subclass > def myenc(d): > if if isinstance(d, decimal.Decimal): > return float(str(d) > # etc > raise TypeError() > > simplejson.dumps(data, default=myenc) > > We need to fix jsonify -- what's a little problematic is it's built to > take no arguments, which is significantly different from a decorator > that's built to take arguments. We may be able to make the decorator > work both ways for 0.10 but it'd tricky/annoying.
It's tricky, but not too tricky, if you only accept keyword arguments in
to @jsonify():
def jsonify(fn=None, arg1=value1, arg2=value2):
if fn is None:
# this is @jsonify(arg1=...), so return a decorator
return functools.partial(jsonify, arg1=arg1, arg2=arg2)
@functools.wraps
def wrapped(*a, **kw):
# do stuff
retval = fn(*a, **kw)
return simplejson.dumps(retval, arg1=arg1, arg2=arg2)
return wrapped
Use it as
@jsonify
def fn(...):
@jsonify()
def fn(...):
@jsonify(arg1=42)
def fn(...):
but don't try
@jsonify(42)
def fn(...):
> Pylons 1.0 could definitely break it, however: @jsonify -> @jsonify()
Marius Gedminas
--
There is nothing more practical than a good theory.
-- James Clerk Maxwell
signature.asc
Description: Digital signature
