[Tutor] Accessing the name of a Function
Kent Johnson
kent37 at tds.net
Thu Dec 14 00:42:26 CET 2006
Carroll, Barry wrote:
> Greetings:
>
> Andreas and Kent helped me solve my problem. We are writing a keyword
> driven framework for embedded SW testing. The framework supports scores
> of keywords, built-in and user-defined. We are still writing the
> framework, so none of the functions for manipulating the target device
> has been written, although the keywords are defined. As a result, he
> framework's output log is full of "keyword not found" errors, which mask
> real errors in framework operation.
>
> To get rid of these 'pseudo errors' I wanted a stub handler that would
> output an information message, with the name of the function and its
> arguments. Using the sys._getframe method and the inspect.getargvalues
> methods, I was able to write a single routine that will handle any
> function signature. Here is the result:
You could also do this very simply and cleanly with a decorator,
assuming Python >= 2.4. This example is pretty much what you want:
http://wiki.python.org/moin/PythonDecoratorLibrary#head-d4ce77c6d6e75aad25baf982f6fec0ff4b3653f4
In your approach, you can call _getframe() in handlestub, see for example
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66062
Kent
>
> A sample keyword triggered function:
> ##########
> import sys
> from actioncommon import handlestub
>
> def doaction_dbopen(ctrl, proc, schemafile, configfile,
> dataset="default"):
> '''A, Config, DB, Open, schemafile, configfile, dataset="default"
>
> Open the testbench data base.
> Where:
> schemafile (str) - the xml schema file for the product to be
> tested;
> configfile (str) - the configuration file for the product to be
> tested;
> dataset (str) - optional configuration data set to activate.
>
> defaults to "default"
> Example:
> A, Config, DB, Open, "master.xml", "test.tex", "current"
> '''
> # TODO: replace stub handler with action logic.
> handlestub(ctrl, proc, sys._getframe())
> return True
> ##########
>
> The stub handler:
> ##########
> from inspect import getargvalues
>
> def handlestub(ctrl, proc, thestubframe):
> """Identifies actions that are defined but not yet implemented."""
>
> themsg = "Execuiting %s (implementation pending) with arguments: " %
> \
> thestubframe.f_code.co_name
> (theargnames, trash1, trash2,
> thelocalsdict)=getargvalues(thestubframe)
> # The first two arguments are internal to the application: do not
> display.
> if len(theargnames) <= 2:
> themsg += "None"
> else:
> for theargname in theargnames[2:]:
> themsg += "%s=%s " % (theargname,
> thelocalsdict[theargname]) ctrl.log.logaction(ctrl.log.INFO, themsg)
> ##########
>
> The resulting log output:
> ##########
> INFO Execuiting doaction_dbopen (implementation pending) with
> arguments: schemafile=USBIBM.XML configfile=USBIBM.TEX
> dataset=usbimbstart
> ##########
>
> Thanks to Andreas and Kent for their help
>
> Regards,
>
> Barry
> barry.carroll at psc.com
> 541-302-1107
> ________________________
> We who cut mere stones must always be envisioning cathedrals.
>
> -Quarry worker's creed
>
>
>> -----Original Message-----
>> From: Kent Johnson [mailto:kent37 at tds.net]
>> Sent: Wednesday, December 13, 2006 2:55 AM
>> To: Carroll, Barry
>> Cc: tutor at python.org
>> Subject: Re: [Tutor] Accessing the name of a Function
>>
>> Carroll, Barry wrote:
>>> Andreas:
>>>
>>> You're right, that is kind of messy and somewhat limited. In the
>>> present case, however, it is the function's defined name that I
> want, so
>>> this would work okay.
>>>
>>> I'm guessing that there is a way to determine the number and names
> of
>>> the arguments to the function as well. I'll go look at the sys
> module
>>> and see what I can find.
>> Look at the inspect module, specifically getargspec() and
> getargvalues().
>> Kent
>>
>
>
>
More information about the Tutor
mailing list