Aaron Scott wrote:
Pickling the source code is much sturdier. It's very unlikely that
the same code runs differently in different interpreters. It's much
more likely that the same code runs the same, or not at all.
Okay, I've run into another problem. I've saved the code to a string,
so I can call it up when I need it. I want to keep these functions all
together, though, so I'm pushing them into a dictionary when I execute
it. It seems like when I put it in a dictionary, though, it messes up
the scope of the functions contained within. For example:
import cPickle
def Main():
holder =}
functiontest =def PickleTest():\n\tprint cPickle"
exec functiontest in holder
print holder["PickleTest"]()
Main()
... produces:
Traceback (most recent call last):
File "pickletest.py", line 11, in <module>
Main()
File "pickletest.py", line 9, in Main
print holder["PickleTest"]()
File "<string>", line 2, in PickleTest
NameError: global name 'cPickle' is not defined
Is there any way to do this so that the functions have access to the
higher scope?
Thanks.
Why not use import ? Simply recreate the source file, if necessary, and
import it again.
If you must import without it being in a clear text file, check out the
deprecated imputil module, standard in 2.x, but removed in Python 3.0.
And if you find some way to get __import__ or one of its relatives to
work from a stream instead of a file, please let us know.
--
http://mail.python.org/mailman/listinfo/python-list