473,395 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

inspect feature

Hello,

The 'inspect' module has this method:

inspect.getargvalues(frame)

It takes a frame and returns the parameters used to call it, including
the locals as defined in the frame, as shown.
>>def f( a, b, d= None, *c, **e ):
.... import inspect
.... return inspect.getargvalues( inspect.currentframe() )
....
>>f( 0, 1, 'abc', 'def', ( 3, 2 ), h= 'ghi' )
(['a', 'b', 'd'], 'c', 'e', {'a': 0, 'c': ('def', (3, 2)), 'b': 1,
'e': {'h': 'g
hi'}, 'd': 'abc', 'inspect': <module 'inspect' from 'C:\Programs
\Python26\lib\in
spect.pyc'>})

However, if you wanted a decorator that examines the parameters to a
function, you're out of luck. By the time you have a frame, you're
already in the function.

Perhaps it would not be as common as something like 'join' for
example, or even the rest of the functions in 'inspect', but do you
think something similar to 'getargvalues' that accepted a function and
an argument list, and returned a dictionary mapping parameters to
values, could be useful?

Oct 9 '08 #1
8 1322
Aaron "Castironpi" Brady a écrit :
Hello,

The 'inspect' module has this method:

inspect.getargvalues(frame)

It takes a frame and returns the parameters used to call it, including
the locals as defined in the frame, as shown.
>>>def f( a, b, d= None, *c, **e ):
... import inspect
... return inspect.getargvalues( inspect.currentframe() )
...
>>>f( 0, 1, 'abc', 'def', ( 3, 2 ), h= 'ghi' )
(['a', 'b', 'd'], 'c', 'e', {'a': 0, 'c': ('def', (3, 2)), 'b': 1,
'e': {'h': 'g
hi'}, 'd': 'abc', 'inspect': <module 'inspect' from 'C:\Programs
\Python26\lib\in
spect.pyc'>})

However, if you wanted a decorator that examines the parameters to a
function, you're out of luck. By the time you have a frame, you're
already in the function.
Hem...

def decorator(func):
def _decorator(*args, *kw):
print "func args are ", *args, **kw
return func(*args, **kw)
return _decorator
Oct 9 '08 #2
On Oct 9, 3:48*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Aaron "Castironpi" Brady a écrit :
Hello,
The 'inspect' module has this method:
inspect.getargvalues(frame)
It takes a frame and returns the parameters used to call it, including
the locals as defined in the frame, as shown.
>>def f( a, b, d= None, *c, **e ):
... * * import inspect
... * * return inspect.getargvalues( inspect.currentframe() )
...
>>f( 0, 1, 'abc', 'def', ( 3, 2 ), h= 'ghi' )
(['a', 'b', 'd'], 'c', 'e', {'a': 0, 'c': ('def', (3, 2)), 'b': 1,
'e': {'h': 'g
hi'}, 'd': 'abc', 'inspect': <module 'inspect' from 'C:\Programs
\Python26\lib\in
spect.pyc'>})
However, if you wanted a decorator that examines the parameters to a
function, you're out of luck. *By the time you have a frame, you're
already in the function.

Hem...

def decorator(func):
* * *def _decorator(*args, *kw):
* * * * *print "func args are ", *args, **kw
* * * * *return func(*args, **kw)
* * *return _decorator
It is less of a problem without tuple unpacking, but you still have
code like:

if len( args )>= 2:
b= args[ 1 ]
else:
try:
b= (somehow check b's default val.)
except NoDefaultVal:
raise ArgumentError

Worse yet, you have it for each parameter. Unless I missed something,
this is the only way to mimic/recreate the signature of the decoratee.

Oct 9 '08 #3
Aaron "Castironpi" Brady a écrit :
On Oct 9, 3:48 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>Aaron "Castironpi" Brady a écrit :
>>Hello,
The 'inspect' module has this method:
inspect.getargvalues(frame)
It takes a frame and returns the parameters used to call it, including
the locals as defined in the frame, as shown.
>def f( a, b, d= None, *c, **e ):
... import inspect
... return inspect.getargvalues( inspect.currentframe() )
...
>f( 0, 1, 'abc', 'def', ( 3, 2 ), h= 'ghi' )
(['a', 'b', 'd'], 'c', 'e', {'a': 0, 'c': ('def', (3, 2)), 'b': 1,
'e': {'h': 'g
hi'}, 'd': 'abc', 'inspect': <module 'inspect' from 'C:\Programs
\Python26\lib\in
spect.pyc'>})
However, if you wanted a decorator that examines the parameters to a
function, you're out of luck. By the time you have a frame, you're
already in the function.
Hem...

def decorator(func):
def _decorator(*args, *kw):
print "func args are ", *args, **kw
return func(*args, **kw)
return _decorator

It is less of a problem without tuple unpacking, but you still have
code like:

if len( args )>= 2:
b= args[ 1 ]
else:
try:
b= (somehow check b's default val.)
except NoDefaultVal:
raise ArgumentError

Worse yet, you have it for each parameter. Unless I missed something,
this is the only way to mimic/recreate the signature of the decoratee.
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.
Oct 10 '08 #4
On Oct 10, 3:36*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Aaron "Castironpi" Brady a écrit :
On Oct 9, 3:48 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
Aaron "Castironpi" Brady a écrit :
>Hello,
The 'inspect' module has this method:
inspect.getargvalues(frame)
It takes a frame and returns the parameters used to call it, including
the locals as defined in the frame, as shown.
def f( a, b, d= None, *c, **e ):
... * * import inspect
... * * return inspect.getargvalues( inspect.currentframe() )
...
f( 0, 1, 'abc', 'def', ( 3, 2 ), h= 'ghi' )
(['a', 'b', 'd'], 'c', 'e', {'a': 0, 'c': ('def', (3, 2)), 'b': 1,
'e': {'h': 'g
hi'}, 'd': 'abc', 'inspect': <module 'inspect' from 'C:\Programs
\Python26\lib\in
spect.pyc'>})
However, if you wanted a decorator that examines the parameters to a
function, you're out of luck. *By the time you have a frame, you're
already in the function.
Hem...
def decorator(func):
* * *def _decorator(*args, *kw):
* * * * *print "func args are ", *args, **kw
* * * * *return func(*args, **kw)
* * *return _decorator
It is less of a problem without tuple unpacking, but you still have
code like:
if len( args )>= 2:
* *b= args[ 1 ]
else:
* *try:
* * * b= (somehow check b's default val.)
* *except NoDefaultVal:
* * * raise ArgumentError
Worse yet, you have it for each parameter. *Unless I missed something,
this is the only way to mimic/recreate the signature of the decoratee.

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'?
Oct 10 '08 #5
En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
<ca********@gmail.comescribió:
On Oct 10, 3:36*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>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.

--
Gabriel Genellina

Oct 14 '08 #6
On Oct 14, 3:06 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
<castiro...@gmail.comescribió:
On Oct 10, 3:36 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
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
Oct 14 '08 #7
On Oct 14, 9:42*am, George Sakkis <george.sak...@gmail.comwrote:
On Oct 14, 3:06 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
<castiro...@gmail.comescribió:
On Oct 10, 3:36 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
>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.

Is there some reason why the built-in behavior should not be made
available, such as it's poorly defined outside the function? Or is it
just the fact that it's complicated that keeps it out of 'inspect'?
Oct 14 '08 #8
On Oct 14, 2:35 pm, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:
On Oct 14, 9:42 am, George Sakkis <george.sak...@gmail.comwrote:
On Oct 14, 3:06 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 10 Oct 2008 14:18:53 -0300, Aaron "Castironpi" Brady
<castiro...@gmail.comescribió:
On Oct 10, 3:36 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
I don't get what you're after ??? The decorator has full access toboth
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 allowfor
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 saneway.
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'

George
Oct 14 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a problem to get the command that has called a function if the command was given on multiple lines. E.g.: ################################################### import inspect ...
1
by: Thomas Guettler | last post by:
Hi, the line numbers of inspect.getinnerframes are different from traceback.format_exception. This results in wrong lines being shown in cgitb. An example is below. I looked at the...
2
by: Fernando Perez | last post by:
Hi all, IPython has suffered quite a few problems with the inspect module in python 2.3. For these, unfortunately all I've been able to do is guard with overreaching except clauses, as I had...
11
by: It's me | last post by:
I discovered the hardway what inspect.isclass() is doing. Consider this no brainer code: ### import inspect class Abc: def Hello(self): return
1
by: Darran Edmundson | last post by:
I was playing around with the inspect module the other day trying to write a quick and dirty "smart" logger. By this I mean that writing a message to the global log would also yield some info...
0
by: Ron Adam | last post by:
While playing around with the inspect module I found that the Blockfinder doesn't recognize single line function definitions. Adding the following two lines to it fixes it, but I'm not sure if it...
1
by: aj | last post by:
What is the difference (if any) between inspect check database and db2dart ??? Do they both find the same potential problems? Does one provide more comprehensive checking than the other? ...
4
by: Chris Pax | last post by:
Hello, I recently been trying to use the inspect module to inspect the arguments of gtk objects, such as gtk.Button. I tried like this: inspect.getargspec(gtk.Button.__init__) and get the...
0
by: rajasankar | last post by:
Hi, I am using Jython based application and trying to use inspect.py in the python files. Here is my code import inspect,os,sys,pprint,imp def handle_stackframe_without_leak(getframe): ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.