Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Code Objects / Variable Names

Reply
Thread Tools

Code Objects / Variable Names

 
 
F. Schaefer
Guest
Posts: n/a
 
      01-07-2004
PROBLEM:

How can you know what variables are involved in a code object.

It is not possible to execute the code object simply into a
dictionary and then watch the defined variables. This is
because these code objects only contain conditions, that
read out variables.

EXAMPLE:

For the following (compiled) condition

(door == "ajar" and alternator == "off") || time > 25

I need to extract, that the variables 'door', 'alternator' and 'time'
are involved.


Best Regards,

Frank Schäfer.
 
Reply With Quote
 
 
 
 
Francis Avila
Guest
Posts: n/a
 
      01-07-2004
F. Schaefer wrote in message ...
>PROBLEM:
>
> How can you know what variables are involved in a code object.
>
> It is not possible to execute the code object simply into a
> dictionary and then watch the defined variables. This is
> because these code objects only contain conditions, that
> read out variables.


This seems a strange requirement. Perhaps you could elaborate on what
you're doing and we could suggest alternative approaches? One *rarely*
needs to pass around raw code objects in Python, and both producing and
executing them is considerably slower than just about any other construct
you could use (not that it matters much).

>EXAMPLE:
>
> For the following (compiled) condition
>
> (door == "ajar" and alternator == "off") || time > 25
>
> I need to extract, that the variables 'door', 'alternator' and 'time'
> are involved.



>>> cobj = compile('(door == "ajar" and alternator == "off") or time >

25', '<stdin>', 'exec')
>>> dir(cobj)

['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__str__', 'co_argcount', 'co_cellvars',
'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags',
'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals',
'co_stacksize', 'co_varnames']
>>> cobj.co_names

('door', 'alternator', 'time')

See "Code objects" under "Internal Types" in section 3.2, "Standard Type
Hierarchy," of the Python Language Reference Manual.

>>> import dis #just for fun.
>>> dis.disco(cobj)

1 0 LOAD_NAME 0 (door)
3 LOAD_CONST 0 ('ajar')
6 COMPARE_OP 2 (==)
9 JUMP_IF_FALSE 10 (to 22)
12 POP_TOP
13 LOAD_NAME 1 (alternator)
16 LOAD_CONST 1 ('off')
19 COMPARE_OP 2 (==)
>> 22 JUMP_IF_TRUE 10 (to 35)

25 POP_TOP
26 LOAD_NAME 2 (time)
29 LOAD_CONST 2 (25)
32 COMPARE_OP 4 (>)
>> 35 POP_TOP

36 LOAD_CONST 3 (None)
39 RETURN_VALUE

--
Francis Avila

 
Reply With Quote
 
 
 
 
Andrew Clover
Guest
Posts: n/a
 
      01-07-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (F. Schaefer) wrote:

> How can you know what variables are involved in a code object.


http://www.python.org/doc/current/re...s.html#l2h-138

> (door == "ajar" and alternator == "off") || time > 25


> I need to extract, that the variables 'door', 'alternator' and 'time'
> are involved.


>>> c= compile('door=="ajar" and alternator=="off" or time>25', '', 'eval')
>>> c.co_names

('door', 'alternator', 'time')

HTH. Of course if the expression itself contains the likes of 'eval',
'globals()' etc. this cannot be exhaustive.

--
Andrew Clover
(E-Mail Removed)
http://www.doxdesk.com/
 
Reply With Quote
 
Lonnie Princehouse
Guest
Posts: n/a
 
      01-07-2004
You can use decompyle to get equivalent source from a code object, and
then use the compiler module to parse the source into syntax trees.

Traverse the trees to figure out which variables are being used.

If you've already got the source somewhere, you won't need decompyle.



(E-Mail Removed) (F. Schaefer) wrote in message news:<(E-Mail Removed). com>...
> PROBLEM:
>
> How can you know what variables are involved in a code object.
>
> It is not possible to execute the code object simply into a
> dictionary and then watch the defined variables. This is
> because these code objects only contain conditions, that
> read out variables.
>
> EXAMPLE:
>
> For the following (compiled) condition
>
> (door == "ajar" and alternator == "off") || time > 25
>
> I need to extract, that the variables 'door', 'alternator' and 'time'
> are involved.
>
>
> Best Regards,
>
> Frank Schäfer.

 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      01-08-2004
On 7 Jan 2004 00:51:45 -0800, rumours say that (E-Mail Removed)
(F. Schaefer) might have written:

>For the following (compiled) condition
>
> (door == "ajar" and alternator == "off") || time > 25
>
> I need to extract, that the variables 'door', 'alternator' and 'time'
> are involved.


I believe that the solution that Francis and Andrew proposed is the most
suitable for your case. There is another one, however, that *might* be
useful sometime: use the eval(your_code, globals, locals) form, and
catch the NameError exceptions.
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic variable names or using a variable as another's name Peter Buckley Ruby 5 02-27-2009 06:05 PM
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM



Advertisments