471,073 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

namespaces and eval

Sorry for the repost, didnt' quite finish

Suppose I have a function in module X that calls eval e.g,

X.py
_______
Def foo(bar):
Eval(bar)
_______

Now eval will be called using the default eval(bar,globals(),locals())
and globals will pull in anything in module X.

Now I have module Y that calls bar like so
Y.py
________
from x import *
def biz():
print "Im a silly example!"
Foo("biz()")
_______

Python will complain that it cannot find biz because it's not the X
module.

My question - is there any way to a) get a listing of active namespaes
search ALL active namespaces for a particular variable b) get the
namespace of the caller withing passing it in (here, Y) c) do anything
else to get biz without passing foo("biz()",locals(),globals()) or
hacking biz into the __builtin__ namespace(don't know if it's even
possible, but it defeats the purpose of what I'm trying to do) ? I
realize this is here for a reason, but I'm working on something that's
kind of a hack already

Thanks
dave
More gratuitous context. Long story I'm trying to write a hack for a
concise way of adding arbitrary methods to objects for JPA/JQuery like
chaning eg.

def foo(self):
print "foo"
return self;

wrap(x).foo().foo().foo().foo() etc....

instead of x.foo = foo, which alters the object.

class wrap:
def __init__(self, obj, globalz=globals(),localz= locals()):
self.obj = obj
self.localz = localz;
self.globalz = globalz;

def __callback__(self, *args, **kw):
newargs = [self.obj];
newargs.extend(args);
print locals
ret = eval(self.name, self.globalz, self.localz)
(*newargs,**kw);
return wrap(ret);

def __getattr__(self,name):
if hasattr(self.obj,name):
return getattr(self.obj,name);
self.name = name;
return self.__callback__;


Jun 27 '08 #1
1 1685
da********@gmail.com writes:
Sorry for the repost, didnt' quite finish

Suppose I have a function in module X that calls eval e.g,

X.py
_______
Def foo(bar):
Eval(bar)
_______

Now eval will be called using the default eval(bar,globals(),locals())
and globals will pull in anything in module X.

Now I have module Y that calls bar like so
Y.py
________
from x import *
def biz():
print "Im a silly example!"
Foo("biz()")
_______

Python will complain that it cannot find biz because it's not the X
module.

My question - is there any way to a) get a listing of active namespaes
search ALL active namespaces for a particular variable
sys.modules gives you all the live modules.
b) get the namespace of the caller withing passing it in (here, Y)
sys._getframe(1).f_globals (resp. sys._getframe(1).f_locals) gives you
the caller's globals (resp. locals).
c) do anything else to get biz without passing
foo("biz()",locals(),globals()) or hacking biz into the __builtin__
namespace(don't know if it's even possible, but it defeats the
purpose of what I'm trying to do) ?
Functions in Python are objects like any other, and can be passed as
parameters. I.e:

x.py
----------
def foo(bar):
bar()
>>from x import foo
def biz(): print "Boo!"
foo(biz)
Boo!
>>>
I realize this is here for a reason, but I'm working on something
that's kind of a hack already

Thanks
dave
More gratuitous context. Long story I'm trying to write a hack for a
concise way of adding arbitrary methods to objects for JPA/JQuery like
chaning eg.

def foo(self):
print "foo"
return self;

wrap(x).foo().foo().foo().foo() etc....
What about a composition function instead? I.e.

compose(foo, foo, foo, foo)(x)

Here's one that I made earlier:
http://www.marooned.org.uk/~arno/python/compose.html

[snip]

--
Arnaud
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Random | last post: by
3 posts views Thread by Jens | last post: by
6 posts views Thread by Lonnie Princehouse | last post: by
6 posts views Thread by dave.g1234 | last post: by
reply views Thread by Gordon Fraser | last post: by
reply views Thread by leo001 | last post: by

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.