Jika Anda ingin jawaban yang diterima disarikan berfungsi, Anda dapat menggunakan:
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())
untuk mendapatkan / memuat ruang kerja:
import my_pkg as mp
x=1
mp.load_workspace('a', globals())
print x
itu berhasil ketika saya menjalankannya. Saya akui saya tidak mengerti dir()dan globals()100% jadi saya tidak yakin apakah mungkin ada peringatan aneh, tetapi sejauh ini tampaknya berhasil. Komentar diterima :)
setelah beberapa penelitian lebih lanjut jika Anda memanggil save_workspaceseperti yang saya sarankan dengan global dan save_workspacedalam fungsi itu tidak akan berfungsi seperti yang diharapkan jika Anda ingin menyimpan veriables dalam lingkup lokal. Untuk penggunaan itu locals(). Ini terjadi karena global mengambil global dari modul tempat fungsi didefinisikan, bukan dari tempat dipanggilnya akan menjadi tebakan saya.