namespaces and eval

D

dave.g1234

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__;
 
A

Arnaud Delobelle

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()

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]
 
G

George Sakkis

sys.modules gives you all the live modules.


sys._getframe(1).f_globals (resp. sys._getframe(1).f_locals) gives you
the caller's globals (resp. locals).


Functions in Python are objects like any other, and can be passed as
parameters.  I.e:

x.py
----------
def foo(bar):
    bar()








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

Neat, although the order is counter-intuitive; the callables should
better be called in the same order they are given in compose(). Here's
yet another take on the composition syntax:

DONE = object()

class Chainable(object):
def __init__(self, val):
self.val = val
def __or__(self, func):
if func is DONE:
return self.val
self.val = func(self.val)
return self

# Test
def double(x): return 2*x
def square(x): return x**2
def succ(x): return x+1
18.0

Admittedly 'DONE' is ugly but it's necessary to denote that the piping
ends and the final value (rather than the Composable wrapper) should
be returned. The OP's wrap class suffers from the same problem but it
masks it by overriding __getattr__ in an old-style class. If the class
is changed to new-style, it becomes more obvious that
wrap(x).foo().foo().foo().foo() returns the wrap object, not x.

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top