george.sakkis at gmail.com
Tue Oct 14 21:32:53 CEST 2008
On Oct 14, 2:35 pm, "Aaron \"Castironpi\" Brady"
<castiro... at gmail.com> wrote:
> On Oct 14, 9:42 am, George Sakkis <george.sak... at gmail.com> wrote:
> > On Oct 14, 3:06 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > wrote:
> > > En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
> > > <castiro... at gmail.com> escribió:
> > > > On Oct 10, 3:36 am, Bruno Desthuilliers <bruno.
> > > > 42.desthuilli... at websiteburo.invalid> wrote:
> > > >> I don't get what you're after ??? The decorator has full access to both
> > > >> the actual params *and* the function's signature (via
> > > >> inspect.getargspec). So your initial question "if you wanted a decorator
> > > >> that examines the parameters to a function" seems fully answered. You
> > > >> will indeed have to write a couple lines of code if you want the same
> > > >> formating as the one you'd get with inspect.currentframe(), but what ?
> > > >> FWIW, Michele Simionato's decorator module has some trick to allow for
> > > >> signature-preserving decorators, so you may want to have a look - but
> > > >> I'm not sure if this would solve your problem - at least in a sane way.
> > > > It's not exactly the next Millennium problem, but there are some
> > > > substantial checks you have to do on a per-parameter basis to see the
> > > > same thing that a function sees, when all you have is *args, **kwargs.
> > > > You are wrapping a function with this signature:
> > > > def f( a, b, c= None, *d, **e ):
> > > > You want to find out the values of 'a', 'b', and 'c' in a decorator.
> > > > You have these calls:
> > > > f( 0, 1, 'abc', 'def', h= 'ghi' )
> > > > f( 0, 1 )
> > > > f( 0, 1, h= 'abc' )
> > > > f( 0, 1, 'abc', c= 'def' ) #raise TypeError: multiple values
> > > > How do you determine 'a', 'b', and 'c'?
> > > I'm afraid you'll have to duplicate the logic described here: http://docs.python.org/reference/expressions.html#id9
> > > To my knowledge, there is no available Python code (in the stdlib or
> > > something) that already does that.
> > I wrote such a beast some time ago; it's hairy but to the best of my
> > knowledge it seems to reproduce the standard Python logic:http://code.activestate.com/recipes/551779/
> > George
> I didn't see a 'got a duplicate argument for keyword "d"' error, but I
> can add one if I need to.
Why don't you try it out:
>>> def f( a, b, c= None, *d, **e ): pass
>>> getcallargs(f, 0, 1, 'abc', c= 'def' )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "getcallargs.py", line 53, in getcallargs
"argument '%s'" % (f_name,arg))
TypeError: f() got multiple values for keyword argument 'c'
More information about the Python-list