import - How to substitute module.Class() to locally defined Class() when loading with Python's Pickle? -


I have a pickle dump in which there is an array of fu. Bar () objects I am attempting to unplick it, but the definition of the bar () class is in the same file that is trying to unplick, and not in the foo module. Therefore, Pickles complains that it could not get module fu.

I tried to do something similar to the FU module: import imp,

  class bar: pass foo_module = Imp.new_module ('foo') foo_module .bar = times sys.modules ['foo'] = foo_module import foo print foo.Bar ()  

Which works, but when I try to add it after :

  Import pickle p = pickle.load (open ("my-pickle.pkl"))  

Friendly traceback (most recent call) Last): File "pyppd.py", line 69, ppds = loads (ppds_decompressed) file "/usr/lib/python2.6/pickle.py", loaded by In the [key] (self) file "/ usr / lib / python2" in the package, unpickler (file) .load () file "/usr/lib/python2.6/pickle.py", line 858, in the return. 6 / pickle.py ", line 1069, load_inst klass = self.find_class (module, name) in the file" /usr/lib/python2.6/pickle.py ", line 1124, find_class __import __ (module) file" /tmp/test.py "," Unpickler "(File) in line 69, P = (" My-pickle.pkl ")) file" /usr/lib/python2.6/pickle.py ", line 1374, Load in Load, .load () file "/ usr / lib / python2.6 / pickle.py", line 858, "send / usr / lib / python2.6/pickle.py", line 1069 in load transmission [key] (self) , Load_in clay Import = import into the self.find_class (module, name) file "/usr/lib/python2.6/pickle.py", line 1124, find_class __import __ (module) Import: a module name foo

Any thoughts?

  class bar: passed myUnpickler (pickle.Unpickler): def find_class (auto, module , Name): if module == "foo" and name == "bar": back second: return pickle.Unpickler.find_class (auto, module, name) bars = MyUnpickler (open ("objects.pkl")). If you are calling this snippet of code from another module, then assume that  baz , then the unclic types of objects  falcon Bar, , no  foo.bar . Belonging to the class definitions of  foo.bar  and  baz.bar , you will not have any problems. But be careful with downstream usage of  isinstance ,  type , etc. Generally, some are trying to do this kind of thing, but one stop is probably not smart because your codebase now has two examples of  bar  if possible, you can get your path Should put  foo  in 


Comments