How can I pickle
a dictionary object that contains instances of a Class in one file (Python File 1) and pickle.load
in another file (Python File 2)?
I have a HUGE complicated dataset made up of several files and I created a class to store all of my attributes. I made a dictionary to store all of the samples and attributes . key = sample, value = instance of the class containing the atributes. Example below:
#Python File 1
import random
class Storage:
def __init__(self,label,x,y):
self.label = label; self.x = x; self.y = y
def get_x(self): return(self.x)
def get_y(self): return(self.y)
D_var_instance = {}
L = ["A","B","C"]
for var in L:
D_var_instance[var] = Storage(label=var,x=random.random(),y=random.random())
print(D_var_instance["A"])
#<__main__.Storage instance at 0x102811128>
print(D_var_instance["A"].get_x())
#0.193517721574
It takes me a long time to make this with my real dataset, I tried using pickle
and pickle.dump
the dictionary object but it's not working:
#Python File 1
import pickle
pickle.dump(D_var_instance,open("/path/to/dump.txt","w"))
pickle.dump(Storage, open("/path/to/storagedump.txt","w"))
I tried loading in another Python file with this code:
#Python File 2
import pickle
Storage = pickle.load(open("/path/to/storagedump.txt","r"))
D_var_instance = pickle.load(open("/path/to/dump.txt","r"))
Got this error:
AttributeError: 'module' object has no attribute 'Storage'