I got a module stuff.py
with a lot of unorganized objects.
import stuff
myList = getattr(stuff, 'atomsList')
myList.append ('protons')
setattr(stuff, 'atomsList', myList)
How do I save the stuff
module now as stuff.py
?
I got a module stuff.py
with a lot of unorganized objects.
import stuff
myList = getattr(stuff, 'atomsList')
myList.append ('protons')
setattr(stuff, 'atomsList', myList)
How do I save the stuff
module now as stuff.py
?
The idea with modules I thought is that they are defined in a general sense and are only made specific when they are 'instantiated' in the same way as classes ( and you can't edit the source code). So unless you want to directly edit your class, you should save the instantiated class using pickle.
I think Pickle is what you're looking for...
E.g.:
import pickle
import stuff
myList = getattr(stuff, 'atomsList')
myList.append ('protons')
setattr(stuff, 'atomsList', myList)
with open("instantiated_stuff.pickle", ‘wb’) as f:
pickle.dump(stuff, f)
Then to open the file use:
with open("instantiated_stuff.pickle", ‘rb’) as pickle_file:
opend_stuff = pickle.load(pickle_file)