I'm trying to mimic the matlab
load and save functions. I'm following this thread: Shelve Code gives KeyError
It is smart. However, if I write that code in a separate module, and try to import that module and invoke that function, then it can't access the global variables.
Specifically, I write a happy.py
and have the functions inside:
def save(filename='tmp', globals_=None):
if globals_ is None:
globals_ = globals()
globals()
import shelve
my_shelf = shelve.open(filename, 'n')
for key, value in globals_.items():
if not key.startswith('__'):
try:
my_shelf[key] = value
except Exception:
print('ERROR shelving: "%s"' % key)
else:
print('shelved: "%s"' % key)
my_shelf.close()
def load(filename='tmp', globals_=None):
import shelve
my_shelf = shelve.open(filename)
for key in my_shelf:
globals()[key] = my_shelf[key]
my_shelf.close()
and when I try
a = 1
b = 2
happy.save()
It would not give save a
and b
.
Is this because global()
would not give the objects outside the module? How can I do what I want to do then?