Example #1
0
 def restore(self, obj):
     """Restores a flattened object to its original python state.
     
     Simply returns any of the basic builtin types
     >>> u = Unpickler()
     >>> u.restore('hello world')
     'hello world'
     >>> u.restore({'key': 'value'})
     {'key': 'value'}
     """
     if self._isclassdict(obj):
         cls = self._loadclass(obj['classmodule__'], obj['classname__'])
         
         try:
             instance = object.__new__(cls)
         except TypeError:
             # old-style classes
             instance = cls()
         
         for k, v in obj.iteritems():
             # ignore the fake attribute
             if k in ['classmodule__', 'classname__']:
                 continue
             if k == 'classdictitems__':
                 for dictk, dictv in v.iteritems():
                     instance[dictk] = self.restore(dictv)
                 continue
             value = self.restore(v)
             if util.is_noncomplex(instance):
                 instance[k] = value
             else:
                 instance.__dict__[k] = value
         return instance
     elif util.iscollection(obj):
         # currently restores all collections to lists, even sets and tuples
         data = []
         for v in obj:
             data.append(self.restore(v))
         return data
     elif util.isdictionary(obj):
         data = {}
         for k, v in obj.iteritems():
             data[k] = self.restore(v)
         return data
     else:
         return obj
Example #2
0
 def restore(self, obj):
     """Restores a flattened object to its original python state.
     
     Simply returns any of the basic builtin types
     >>> u = Unpickler()
     >>> u.restore('hello world')
     'hello world'
     >>> u.restore({'key': 'value'})
     {'key': 'value'}
     """
     if self._isclassdict(obj):
         cls = self._loadclass(obj['classmodule__'], obj['classname__'])
         
         try:
             instance = object.__new__(cls)
         except TypeError:
             # old-style classes
             instance = cls()
         
         for k, v in obj.iteritems():
             # ignore the fake attribute
             if k in ['classmodule__', 'classname__']:
                 continue
             if k == 'classdictitems__':
                 for dictk, dictv in v.iteritems():
                     instance[dictk] = self.restore(dictv)
                 continue
             value = self.restore(v)
             if util.is_noncomplex(instance):
                 instance[k] = value
             else:
                 instance.__dict__[k] = value
         return instance
     elif util.iscollection(obj):
         # currently restores all collections to lists, even sets and tuples
         data = []
         for v in obj:
             data.append(self.restore(v))
         return data
     elif util.isdictionary(obj):
         data = {}
         for k, v in obj.iteritems():
             data[k] = self.restore(v)
         return data
     else:
         return obj
Example #3
0
 def flatten(self, obj):
     """Takes an object and returns a JSON-safe representation of it.
     
     Simply returns any of the basic builtin datatypes
     >>> p = Pickler()
     >>> p.flatten('hello world')
     'hello world'
     >>> p.flatten(u'hello world')
     u'hello world'
     >>> p.flatten(49)
     49
     >>> p.flatten(350.0)
     350.0
     >>> p.flatten(True)
     True
     >>> p.flatten(False)
     False
     >>> r = p.flatten(None)
     >>> r is None
     True
     >>> p.flatten(False)
     False
     >>> p.flatten([1, 2, 3, 4])
     [1, 2, 3, 4]
     >>> p.flatten((1,))
     (1,)
     >>> p.flatten({'key': 'value'})
     {'key': 'value'}
     """
     
     if util.isprimitive(obj):
         #logging.info('Returning: %s', obj)
         return obj
     elif util.iscollection(obj):
         data = [] # obj.__class__()
         for v in obj:
             data.append(self.flatten(v))
         return obj.__class__(data)
         #TODO handle tuple and sets
     elif util.isdictionary(obj):
         data = obj.__class__()
         for k, v in obj.iteritems():
             data[k] = self.flatten(v)
         return data
     elif isinstance(obj, object):
         #logging.info('Type: %s', type(obj))
         data = {}
         #module, name = self._getclassdetail(obj)
         #if self.unpicklable is True:
         #    data['classmodule__'] = module
         #    data['classname__'] = name 
         if util.is_dictionary_subclass(obj):
             if self.unpicklable is True:
                 # this will place items in a sub dictionary (arguably not a pure JSON representation, 
                 # since it should be at root level.  However, this method preserves the object
                 # so that it can be recreated as a Python object
                 data['classdictitems__'] = self.flatten(dict(obj))
             else:
                 # this option will place everything at root, but it allows a dictionary key
                 # to overwrite an instance variable if both have the same name
                 for k, v in obj.iteritems():
                     data[k] = self.flatten(v)
         #elif util.is_collection_subclass(obj):
         #    data['__classcollectionitems__'] = self.flatten()
         elif util.is_noncomplex(obj):
             data = [] # obj.__class__()
             for v in obj:
                 data.append(self.flatten(v))
         #elif obj.__getattribute__('__dict__') is not None:
         else:
             try:
                 for k, v in obj.__dict__.iteritems():
                     #logging.info('Flattening k,v: %s,%s', k, v)
                     data[str(k)] = self.flatten(v)
             except AttributeError:
                 return data
         return data
Example #4
0
 def flatten(self, obj):
     """Takes an object and returns a JSON-safe representation of it.
     
     Simply returns any of the basic builtin datatypes
     >>> p = Pickler()
     >>> p.flatten('hello world')
     'hello world'
     >>> p.flatten(u'hello world')
     u'hello world'
     >>> p.flatten(49)
     49
     >>> p.flatten(350.0)
     350.0
     >>> p.flatten(True)
     True
     >>> p.flatten(False)
     False
     >>> r = p.flatten(None)
     >>> r is None
     True
     >>> p.flatten(False)
     False
     >>> p.flatten([1, 2, 3, 4])
     [1, 2, 3, 4]
     >>> p.flatten((1,))
     (1,)
     >>> p.flatten({'key': 'value'})
     {'key': 'value'}
     """
     
     if util.isprimitive(obj):
         return obj
     elif util.iscollection(obj):
         data = [] # obj.__class__()
         for v in obj:
             data.append(self.flatten(v))
         return obj.__class__(data)
         #TODO handle tuple and sets
     elif util.isdictionary(obj):
         data = obj.__class__()
         for k, v in obj.iteritems():
             data[k] = self.flatten(v)
         return data
     elif isinstance(obj, object):
         data = {}
         module, name = self._getclassdetail(obj)
         if self.unpicklable is True:
             data['classmodule__'] = module
             data['classname__'] = name 
         if util.is_dictionary_subclass(obj):
             if self.unpicklable is True:
                 # this will place items in a sub dictionary (arguably not a pure JSON representation, 
                 # since it should be at root level.  However, this method preserves the object
                 # so that it can be recreated as a Python object
                 data['classdictitems__'] = self.flatten(dict(obj))
             else:
                 # this option will place everything at root, but it allows a dictionary key
                 # to overwrite an instance variable if both have the same name
                 for k, v in obj.iteritems():
                     data[k] = self.flatten(v)
         #elif util.is_collection_subclass(obj):
         #    data['__classcollectionitems__'] = self.flatten()
         elif util.is_noncomplex(obj):
             data = [] # obj.__class__()
             for v in obj:
                 data.append(self.flatten(v))
         else:
             for k, v in obj.__dict__.iteritems():
                 data[str(k)] = self.flatten(v)
         return data