How about creating a Factory class with methods for creating, sorting and unpacking dynamically created type objects? The following is a rough start. To use, simply replace the calls with pickle.dump (type, fh) with TypeFactory.pickle (type, fh) and replace the calls with pickle.load (fh) with TypeFactory.unpickle (fh).
import pickle class TypeFactory(object): def __init__(self): pass @staticmethod def create_type(name='DynamicType', dict={}): return type(name, (object,), dict) @staticmethod def pickle(t, fh): dict = t.__dict__.copy() name = t.__name__ for key in dict.keys(): if key.startswith('__') and key.endswith('__'): del dict[key] pickle.dump((name, dict), fh) @classmethod def unpickle(cls, fh): name, dict = pickle.load(fh) return cls.create_type(name, dict)
Garrett
source share