[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.python

Problem with exec

Justus Schwabedal

3/13/2008 10:48:00 PM

I'm trying to parallise with python. Specifically I'm sending code to
the processes and let them exec this code (in ascii form). However I
ran into a problem with Namespaces (I think) which I do not understand.

Here's what I do first:
---------------------------------------
bash-3.2$ cat execBug.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
exec header
print "f() =",f()

bash-3.2$ ./execBug.py
f() = 0.633306324515


it est: it works.
However when I do this:

bash-3.2$ cat execBug2.py
#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
exec header
return f()
print "g() =",g()
bash-3.2$ ./execBug2.py
g() =
Traceback (most recent call last):
File "./execBug2.py", line 10, in <module>
print "g() =",g()
File "./execBug2.py", line 9, in g
return f()
File "<string>", line 4, in f
NameError: global name 'randn' is not defined
bash-3.2$ ???

I get this global name error. I can fix it with adding some line like
"global randn" but I don't want to do this. I want to do exactly what
I wrote: Import the function scipy.randn in the local namespace of the
function "g" so that "return f()" makes sense. Can anybody help me out?
Yours Justus
4 Answers

alitosis

3/14/2008 3:41:00 AM

0

On Mar 14, 9:47 am, Justus Schwabedal <justus.schwabe...@gmx.de>
wrote:
[snipped]
> However when I do this:
>
> bash-3.2$ cat execBug2.py
> #! /usr/bin/python
> header="""
> from scipy import randn
> def f():
> return randn()
> """
> def g():
> exec header
> return f()
> print "g() =",g()
> bash-3.2$ ./execBug2.py
> g() =
> Traceback (most recent call last):
> File "./execBug2.py", line 10, in <module>
> print "g() =",g()
> File "./execBug2.py", line 9, in g
> return f()
> File "<string>", line 4, in f
> NameError: global name 'randn' is not defined
> bash-3.2$ ???
>
> I get this global name error. I can fix it with adding some line like
> "global randn" but I don't want to do this. I want to do exactly what I wrote: Import the function scipy.randn in the local namespace of the
>
> function "g" so that "return f()" makes sense. Can anybody help me out?
> Yours Justus

Maybe using an explicit namespace is good enough for your needs:

#! /usr/bin/python
header="""
from scipy import randn
def f():
return randn()
"""
def g():
n = {}
exec header in n
return n['f']()
print "g() =",g()


(i don't understand the issues, but I can cut-and-paste with the best
of them)

Justus Schwabedal

3/14/2008 5:36:00 PM

0


On Mar 14, 2008, at 4:41 AM, alitosis@gmail.com wrote:

> On Mar 14, 9:47 am, Justus Schwabedal <justus.schwabe...@gmx.de>
> wrote:
> [snipped]
>> However when I do this:
>>
>> bash-3.2$ cat execBug2.py
>> #! /usr/bin/python
>> header="""
>> from scipy import randn
>> def f():
>> return randn()
>> """
>> def g():
>> exec header
>> return f()
>> print "g() =",g()
>> bash-3.2$ ./execBug2.py
>> g() =
>> Traceback (most recent call last):
>> File "./execBug2.py", line 10, in <module>
>> print "g() =",g()
>> File "./execBug2.py", line 9, in g
>> return f()
>> File "<string>", line 4, in f
>> NameError: global name 'randn' is not defined
>> bash-3.2$ ???
>>
>> I get this global name error. I can fix it with adding some line like
>> "global randn" but I don't want to do this. I want to do exactly
>> what I wrote: Import the function scipy.randn in the local
>> namespace of the
>>
>> function "g" so that "return f()" makes sense. Can anybody help me
>> out?
>> Yours Justus
>
> Maybe using an explicit namespace is good enough for your needs:
>
> #! /usr/bin/python
> header="""
> from scipy import randn
> def f():
> return randn()
> """
> def g():
> n = {}
> exec header in n
> return n['f']()
> print "g() =",g()
>
>
> (i don't understand the issues, but I can cut-and-paste with the best
> of them)
> --
> http://mail.python.org/mailman/listinfo/p...

That's what I was looking for: It's probably even a faster solution
than declaring all the imports global, isn't it?

Jeff

10/21/2011 5:31:00 PM

0

On Oct 20, 2:30 am, UsurperTom <Usurper...@aol.com> wrote:
> On Oct 20, 12:59 am, "who?" <yourimageunre...@sbcglobal.net> wrote:
>
> > I started speaking to him
>
> Bullshit. You never spoke to me.

Yes, I did. We spoke to each other in email 3 to 7 years ago.
I don't expect you to remember. It was very brief.

Jeff

10/21/2011 5:32:00 PM

0

On Oct 20, 3:41 am, Fattuchus <fattuc...@yahoo.com> wrote:
> On Oct 19, 3:32 pm, "who?" <yourimageunre...@sbcglobal.net> wrote:
>
> > On Oct 19, 1:31 pm, UsurperTom <Usurper...@aol.com> wrote:
>
> > > On Oct 19, 4:22 am, Fattuchus <fattuc...@yahoo.com> wrote:
>
> > > > How many times have I asked you to stop?
>
> > > He's bitter that his father abandoned him.
>
> > Why don't you shut the fuck up? Every time you say
> > this, it just shows what a loser you are. No matter
> > how much you dislike somebody, you don't say
> > something like that.
>
> I don't like to see quarrelling--period.  But Blackmonk has made
> personal attacks against me over and over
> again.  I wish others would call him on it.

That's between you and him.