473,473 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1843
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: pasa | last post by:
I'm an old time python user, but just got bitten by namespaces in eval. If this is an old discussion somewhere, feel free to point me there. Based on the documentation, I would have expected the...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
3
by: Jens | last post by:
Hi, has anyone an idea why the following code does not work. s = """ def a(n): return n*n
6
by: Lonnie Princehouse | last post by:
List comprehensions appear to store their temporary result in a variable named "_" (or presumably "_", "_" etc for nested comprehensions) In other words, there are variables being put into the...
6
by: dave.g1234 | last post by:
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...
0
by: Gordon Fraser | last post by:
Hi, I'm trying to parse Python code to an AST, apply some changes to the AST and then compile and run the AST, but I'm running into problems when trying to evaluate/execute the resulting code...
0
by: Gordon Fraser | last post by:
Hi, Am Dienstag, den 07.10.2008, 15:30 +0100 schrieb Orestis Markou: That seems to work, thanks. Still trying to make sense of the manual for eval, as it says if the passed globals dict lacks...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.