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__; 1 1844 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
by: Jens |
last post by:
Hi,
has anyone an idea why the following code does not work.
s = """
def a(n):
return n*n
|
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...
| |
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...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |