If you want the accepted answer to be assigned to a function, you can use:
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
To get / download a workspace:
import my_pkg as mp x=1 mp.load_workspace('a', globals()) print x
It worked when I ran it. I admit that I do not understand dir() and globals() 100%, so I'm not sure if there might be some strange warning, but for now it works. Comments are welcome :)
after some additional research, if you call save_workspace , as I suggested using global variables, and save_workspace inside the function, it will not work properly if you want to save the verification in the local area. To do this, use locals() . This is due to the fact that global variables take global values ββfrom the module where the function is defined, and not from where it is called, it will be my guess.
Pinocchio Jul 13 '16 at 20:23 2016-07-13 20:23
source share