473,325 Members | 2,872 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,325 software developers and data experts.

Code Objects / Variable Names

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.
Jul 18 '05 #1
4 1210
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

Jul 18 '05 #2
dr*******@netscape.net (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
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #3
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.

dr*******@netscape.net (F. Schaefer) wrote in message news:<f8**************************@posting.google. 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.

Jul 18 '05 #4
On 7 Jan 2004 00:51:45 -0800, rumours say that dr*******@netscape.net
(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
Jul 18 '05 #5

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

Similar topics

22
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
26
by: William L. Bahn | last post by:
Am I invoking any undefined behavior in the following program? /* Function to print out the bit pattern stored in a variable regardless of type */ #include <limits.h> /* CHAR_BIT */ #include...
136
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
14
by: rsine | last post by:
I did a recent post about what the standard was for naming a string builder object. My concern was that I did not what to use "str" prefix since this is used for strings but did not know what to...
6
by: Fuzzyman | last post by:
Hello all, I'm trying to extract the code object from a function, and exec it without explicitly passing parameters. The code object 'knows' it expects to receive paramaters. It's 'arg_count'...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.