[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: how to pass the workspace ?

Stef Mientki

3/13/2008 8:36:00 PM

Gary Herron wrote:
> Stef Mientki wrote:
>> hello,
>>
>> I've GUI tree with drag and drop nodes,
>> where each nodes contains a code snippet.
>> Now I want to run one or more branches in that tree,
>> so I enumerate over the nodes and have to run something like this:
>>
>> execfile ( node1 )
>> execfile ( node2 )
>> etc..
>>
>> Now how do I pass the workspace created in node1, to node 2, etc ?
>>
>> thanks,
>> Stef Mientki
>>
>
> RTFM! In particular:
> http://docs.python.org/lib/built-in-funcs.h...
thanks Gary, I read that,
but sorry, unfortunately I don't understand it,
this is what I tried:

tree_globs = {}
tree_locals = {}
tree_globs [ 'NewVar' ] = 24
filename = self.Get_Editor_Filename (nodename)
execfile ( filename, tree_globs, tree_locals )
print dir ( tree_globs)
print dir ( tree_locals )

where the code in the executed file is:

beer = 'testje'
print dir()
print dir (globals)
print dir (locals)

the output I get is:

aap

['beer']

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__',
'__setattr__', '__str__']

['__call__', '__class__', '__cmp__', '__delattr__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__',
'__setattr__', '__str__']

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
'__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__setitem__', '__str__', 'clear', 'copy',
'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

The result of globals and locals in the file is eaxctly the same and
none of them mentions 'NewVar' and 'beer'
The result of tree_globs and tree_locals is exactly the same and both
doesn't contain 'NewVar' and 'beer'
Maybe it's very simple after reading the manual,
but apperently I'm missing the essential clue.
What am I missing ?

thanks,
Stef Mientki
2 Answers

Dennis Lee Bieber

3/14/2008 6:36:00 AM

0

On Thu, 13 Mar 2008 21:35:42 +0100, Stef Mientki
<stef.mientki@gmail.com> declaimed the following in comp.lang.python:


> The result of globals and locals in the file is eaxctly the same and
> none of them mentions 'NewVar' and 'beer'
> The result of tree_globs and tree_locals is exactly the same and both
> doesn't contain 'NewVar' and 'beer'
> Maybe it's very simple after reading the manual,
> but apperently I'm missing the essential clue.
> What am I missing ?
>
Use of globals() and locals() vs globals and locals?

-=-=-=-=- execsub.py
beer = 'testje'
print dir()
print "dir(globals)", dir (globals)
print "dir(locals) ", dir (locals)
print "globals() ", globals()
print "locals() ", locals()
-=-=-=-=-

>>> sub_globs = {"newvar" : 42}
>>> sub_locals = {}
>>> execfile("execsub.py", sub_globs, sub_locals)
['beer']
dir(globals) ['__call__', '__class__', '__cmp__', '__delattr__',
'__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__str__']
dir(locals) ['__call__', '__class__', '__cmp__', '__delattr__',
'__doc__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__self__', '__setattr__', '__str__']
globals() {'__builtins__': {'IndexError': <class exceptions.IndexError
<snip>
>>> print sub_locals
{'beer': 'testje'}
>>>

I don't have an explanation for why "newvar" doesn't make it into
the sub file, but as can be seen, the sub local update did make it back
out...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Stef Mientki

3/14/2008 9:32:00 AM

0

Thanks, Gary and Dennis,

Dennis Lee Bieber wrote:
> On Thu, 13 Mar 2008 21:35:42 +0100, Stef Mientki
> <stef.mientki@gmail.com> declaimed the following in comp.lang.python:
>
>
>
>> The result of globals and locals in the file is eaxctly the same and
>> none of them mentions 'NewVar' and 'beer'
>> The result of tree_globs and tree_locals is exactly the same and both
>> doesn't contain 'NewVar' and 'beer'
>> Maybe it's very simple after reading the manual,
>> but apperently I'm missing the essential clue.
>> What am I missing ?
>>
>>
> Use of globals() and locals() vs globals and locals?
>
Well I get an error with that.
But you both give me some ideas, and it's working now:
- I don't need the locals
- from the file where the execute is launched, you must use the normal
dictionary call

tree_globs = {}
tree_globs [ 'NewVar' ] = 24
filename = self.Get_Editor_Filename (nodename)
execfile ( filename, tree_globs )
print self.tree_globs['NewVar']
print self.tree_globs['beer']

where the code in the executed file is:

beer = 'testje'

the output I get is:

24
testje

and the "self.tree_globs" travels perfectly though all the files executed.

cheers,
Stef