Class variable dictionary not saving with pickle.dump in python 2.7 -
i using pickle save object graph dumping root. when load root has instance variables , connected object nodes. saving nodes in class variable of type dictionary. class variable full before being saved after unpickle data empty.
here class using:
class page(): __crawled = {} def __init__(self, title = '', link = '', relatedurls = []): self.__title = title self.__link = link self.__relatedurls = relatedurls self.__related = [] @property def relatedurls(self): return self.__relatedurls @property def title(self): return self.__title @property def related(self): return self.__related @property def crawled(self): return self.__crawled def crawl(self,url): if url not in self.__crawled: webpage = urlopen(url).read() patfindertitle = re.compile('<title>(.*)</title>') patfinderlink = re.compile('<link rel="canonical" href="([^"]*)" />') patfinderrelated = re.compile('<li><a href="([^"]*)"') findpattitle = re.findall(patfindertitle, webpage) findpatlink = re.findall(patfinderlink, webpage) findpatrelated = re.findall(patfinderrelated, webpage) newpage = page(findpattitle,findpatlink,findpatrelated) self.__related.append(newpage) self.__crawled[url] = newpage else: self.__related.append(self.__crawled[url]) def crawlrelated(self): link in self.__relatedurls: self.crawl(link)
i save such:
with open('medtwicegraph.dat','w') outf: pickle.dump(root,outf)
and load such:
def loadgraph(filename): #returns root open(filename,'r') inf: return pickle.load(inf) root = loadgraph('medtwicegraph.dat')
all data loads except class variable __crawled.
what doing wrong?
python doesn't pickle class objects. saves names , find them. documentation of pickle
:
similarly, classes pickled named reference, same restrictions in unpickling environment apply. note none of class’s code or data pickled, in following example class attribute
attr
not restored in unpickling environment:class foo: attr = 'a class attr' picklestring = pickle.dumps(foo)
these restrictions why picklable functions , classes must defined in top level of module.
similarly, when class instances pickled, class’s code , data not pickled along them. instance data pickled. done on purpose, can fix bugs in class or add methods class , still load objects created earlier version of class. if plan have long-lived objects see many versions of class, may worthwhile put version number in objects suitable conversions can made class’s
__setstate__()
method.
in example fix problems changing __crawled
instance attribute or global variable.
Comments
Post a Comment