Pickling Dynamically Generated Classes?
Solution 1:
When the Pickler encounters an object of a type it knows nothing about, it looks for a reduce method. Defining this method when you build your custom class using type should solve the problem of pickling.
If you provide initial args then in addition you might need to define a getnewargs method
Solution 2:
You can assign a global name to your dynamically generated class to make it picklable.
>>>classFoo(object):...pass>>>class_name = 'Goo'>>>my_class = type(class_name, (Foo, ), {'run': lambda self, x: 2*x })>>>globals()[class_name] = my_class>>>g = my_class()>>>pickle.dumps(g)
Of course, you need to make sure that the names of your classes are unique.
Solution 3:
One idea would be to pickle a tuple with:
- The name of the dynamic class
- The subclass tuple (possibly in string form from repr())
- The class dictionary
- The actual instance
This would allow you to pickle a class and then reconstruct it later using type() and subclassing Unpickler.
Solution 4:
For non-dynamic classes, python's pickling mechanism records the module and class name as strings. At unpickle time, it automatically loads the class object from that module.
As mentioned in the original post, the problem here is that for dynamic classes, the class definition itself is not pickled by the default pickler. Since dynamic classes don't exist in a module's source file, unpickling a dynamic class generally won't work.
The trickiest part of pickling the class itself is storing the methods' bytecode. Buried in PiCloud, there's an enhanced pickler under the hood that can pickle dynamic functions you could probably use or extend it to handle your objects.
Solution 5:
I have never done it myself, but http://docs.python.org/library/pickle.html#subclassing-unpicklers seems to indicate that you can override the behavior by subclassing Unpickler
Post a Comment for "Pickling Dynamically Generated Classes?"