Si desea que la respuesta aceptada se resuma para que funcione, puede usar:
import shelve
def save_workspace(filename, names_of_spaces_to_save, dict_of_values_to_save):
'''
filename = location to save workspace.
names_of_spaces_to_save = use dir() from parent to save all variables in previous scope.
-dir() = return the list of names in the current local scope
dict_of_values_to_save = use globals() or locals() to save all variables.
-globals() = Return a dictionary representing the current global symbol table.
This is always the dictionary of the current module (inside a function or method,
this is the module where it is defined, not the module from which it is called).
-locals() = Update and return a dictionary representing the current local symbol table.
Free variables are returned by locals() when it is called in function blocks, but not in class blocks.
Example of globals and dir():
>>> x = 3 #note variable value and name bellow
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'x': 3, '__doc__': None, '__package__': None}
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'x']
'''
print 'save_workspace'
print 'C_hat_bests' in names_of_spaces_to_save
print dict_of_values_to_save
my_shelf = shelve.open(filename,'n')
for key in names_of_spaces_to_save:
try:
my_shelf[key] = dict_of_values_to_save[key]
except TypeError:
pass
my_shelf.close()
def load_workspace(filename, parent_globals):
'''
filename = location to load workspace.
parent_globals use globals() to load the workspace saved in filename to current scope.
'''
my_shelf = shelve.open(filename)
for key in my_shelf:
parent_globals[key]=my_shelf[key]
my_shelf.close()
an example script of using this:
import my_pkg as mp
x = 3
mp.save_workspace('a', dir(), globals())
para obtener / cargar el espacio de trabajo:
import my_pkg as mp
x=1
mp.load_workspace('a', globals())
print x
funcionó cuando lo ejecuté. Admitiré que no entiendo dir()y al globals()100%, así que no estoy seguro de si podría haber alguna advertencia extraña, pero hasta ahora parece funcionar. Los comentarios son bienvenidos :)
después de investigar un poco más, si llama save_workspacecomo sugerí con globales y save_workspaceestá dentro de una función, no funcionará como se esperaba si desea guardar los verificables en un ámbito local. Para ese uso locals(). Esto sucede porque globals toma los globales del módulo donde se define la función, no desde donde se llama, supongo.