Se si desidera che la risposta accettata astratta di funzionare è possibile utilizzare:
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') # 'n' for new
for key in names_of_spaces_to_save:
try:
my_shelf[key] = dict_of_values_to_save[key]
except TypeError:
#
# __builtins__, my_shelf, and imported modules can not be shelved.
#
#print('ERROR shelving: {0}'.format(key))
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())
per ottenere/caricare l'area di lavoro:
import my_pkg as mp
x=1
mp.load_workspace('a', globals())
print x #print 3 for me
ha funzionato quando l'ho eseguito. Devo ammettere che non capisco dir()
e globals()
100%, quindi non sono sicuro se ci potrebbe essere qualche strana avvertenza, ma finora sembra funzionare. I commenti sono benvenuti :)
dopo qualche ricerca più se si chiama save_workspace
come ho suggerito con globali e save_workspace
è all'interno di una funzione non funzionerà come previsto se si desidera salvare i veriables in un ambito locale. Per quello usa locals()
. Ciò accade perché globals prende i globali dal modulo in cui è definita la funzione, non da dove viene chiamata sarebbe la mia ipotesi.
Perfetto. Questo è quello che stavo cercando. A proposito, trovo questa frase nel tuo post super divertente: "Per accantonare il tuo lavoro" :) – user10
E qui pensavo che "sottaceti" fossero divertenti! :) http://en.wikipedia.org/wiki/Inherently_funny_word – unutbu
è possibile astrarre i dettagli del salvataggio delle variabili in una funzione separata? Ad esempio, quando si esegue il loop dei nomi nello spazio dei nomi locali corrente, è possibile passarlo come argomento, ma come passare le globali? Devi anche passare le globali? come in 'save (dir(), globals())' e quindi esegui il tuo codice sopra? Inoltre è possibile chiamare semplicemente una funzione che riassume il ripristino? – Pinocchio