I would like to write a function that pickles all objects in the current namespace which are instances of classes from a given module. the idea is that during an ipython session a user creates many objects from mymodule, and may need to save all of them quickly.
so, for example i want something like this
from mymodule import cat, dog, pickle_all
c= cat(), d=dog()
pickle_all('killer_sesh.p')
end session, forget about for a month, come back and start new session,
from mymodule import upickle_all
objs = unpickle_all('killer_sesh.p')
so my first attempt (show below) works when pasted into the current namespace. if i define as a function, or put it somewhere else, like in mymodule, then the dir() command doesnt return the current namespace, but the namespace as seen by the function. even if i pass the result of dir() as an argument, the objects are not available within the function.
import pickle
filename= 'killer_sesh.p'
module='mymodule'
module_objects = {}
for k in dir():
try:
if eval(k).__module__.split('.')[0] == module:
if k[0]!='_':
print(k)
module_objects[k] = eval(k)
except(AttributeError):
pass
file= open(filename,'w')
pickle.dump(module_objects, file)
file.close()
is a function like this possible?
Right, that’s because you’re using
evalto evaluate them, which evaluates them in the current namespace. You could pass theglobalsandlocalsfrom a different mainspace, and callevalwith those parameters. In other words, instead offoo(dir()), foo(dir(), globals(), locals())`.However, this whole design is a bad idea. Using
evalto get the members of a scope is a bad idea. In fact, usingevalfor almost anything is a bad idea.A much better solution is to pass the thing you want to evaluate the members of, and use
getattrto get them.And an even better solution is to avoid using
dirand then figuring out how to get the attributes with those names; if you want to inspect things, that’s what the inspect module is for. (And even when you want to do something thatinspectdoesn’t quite offer, reading its source code—which is linked from the docs—will usually tell you the best way to do it.)