def _get_flattener(self, obj): if PY2 and isinstance(obj, file): return self._flatten_file if util.is_primitive(obj): return lambda obj: obj if util.is_bytes(obj): return self._flatten_bytestring list_recurse = self._list_recurse if util.is_list(obj): if self._mkref(obj): return list_recurse else: self._push() return self._getref # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.TUPLE: [self._flatten(v) for v in obj]} if util.is_set(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.SET: [self._flatten(v) for v in obj]} if util.is_dictionary(obj): return self._flatten_dict_obj if util.is_type(obj): return _mktyperef if util.is_object(obj): return self._ref_obj_instance if util.is_module_function(obj): return self._flatten_function # instance methods, lambdas, old style classes... self._pickle_warning(obj) return None
def _get_flattener(self, obj): if util.is_primitive(obj): return lambda obj: obj list_recurse = self._list_recurse if util.is_list(obj): if self._mkref(obj): return list_recurse else: self._push() return self._getref # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.TUPLE: [self._flatten(v) for v in obj]} if util.is_set(obj): if not self.unpicklable: return list_recurse return lambda obj: {tags.SET: [self._flatten(v) for v in obj]} if util.is_dictionary(obj): return self._flatten_dict_obj if util.is_type(obj): return _mktyperef if util.is_object(obj): return self._ref_obj_instance # else, what else? (methods, functions, old style classes...) return None
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,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): if self._mkref(obj): return self._pop([self.flatten(v) for v in obj]) else: return self._getref(obj) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): if self.unpicklable: return self._pop({tags.TUPLE: [self.flatten(v) for v in obj]}) else: return self._pop([self.flatten(v) for v in obj]) if util.is_set(obj): if self.unpicklable: return self._pop({tags.SET: [self.flatten(v) for v in obj]}) else: return self._pop([self.flatten(v) for v in obj]) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): if self._mkref(obj): # We've never seen this object so return its # json representation. return self._pop(self._flatten_obj_instance(obj)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) # else, what else? (methods, functions, old style classes...) return None
def test_is_list_other(self): self.assertFalse(is_list(1)) self.assertFalse(is_set(1)) self.assertFalse(is_tuple(1))
def test_is_list_dict(self): self.assertFalse(is_list({'key':'value'})) self.assertFalse(is_set({'key':'value'})) self.assertFalse(is_tuple({'key':'value'}))
def test_is_list_set(self): self.assertTrue(is_set(set([1, 2])))
def test_is_list_set(self): self.assertTrue(util.is_set({1, 2}))
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,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([ self.flatten(v) for v in obj ]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]}) if util.is_set(obj): return self._pop({tags.SET: [ self.flatten(v) for v in obj ]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): data = {} has_class = hasattr(obj, '__class__') has_dict = hasattr(obj, '__dict__') if self._mkref(obj): if has_class and not util.is_repr(obj): module, name = _getclassdetail(obj) if self.unpicklable is True: data[tags.OBJECT] = '%s.%s' % (module, name) if util.is_repr(obj): if self.unpicklable is True: data[tags.REPR] = '%s/%s' % (obj.__class__.__module__, repr(obj)) else: data = unicode(obj) return self._pop(data) if util.is_dictionary_subclass(obj): return self._pop(self._flatten_dict_obj(obj, data)) if util.is_noncomplex(obj): return self._pop([self.flatten(v) for v in obj]) if has_dict: return self._pop(self._flatten_dict_obj(obj.__dict__, data)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)
def test_is_list_other(self): self.assertFalse(util.is_list(1)) self.assertFalse(util.is_set(1)) self.assertFalse(util.is_tuple(1))
def test_is_list_dict(self): self.assertFalse(util.is_list({'key': 'value'})) self.assertFalse(util.is_set({'key': 'value'})) self.assertFalse(util.is_tuple({'key': 'value'}))
def test_is_list_set(self): self.assertTrue(util.is_set(set([1, 2])))
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,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([self.flatten(v) for v in obj]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [self.flatten(v) for v in obj]}) if util.is_set(obj): return self._pop({tags.SET: [self.flatten(v) for v in obj]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): data = {} has_class = hasattr(obj, '__class__') has_dict = hasattr(obj, '__dict__') if self._mkref(obj): if has_class and not util.is_repr(obj): module, name = _getclassdetail(obj) if self.unpicklable is True: data[tags.OBJECT] = '%s.%s' % (module, name) if util.is_repr(obj): if self.unpicklable is True: data[tags.REPR] = '%s/%s' % (obj.__class__.__module__, repr(obj)) else: data = unicode(obj) return self._pop(data) if util.is_dictionary_subclass(obj): return self._pop(self._flatten_dict_obj(obj, data)) if util.is_noncomplex(obj): return self._pop([self.flatten(v) for v in obj]) if has_dict: return self._pop(self._flatten_dict_obj( obj.__dict__, data)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)
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,2,))[tags.TUPLE] [1, 2] >>> p.flatten({'key': 'value'}) {'key': 'value'} """ self._push() if self._depth == self._max_depth: return self._pop(repr(obj)) if util.is_primitive(obj): return self._pop(obj) if util.is_list(obj): return self._pop([ self.flatten(v) for v in obj ]) # We handle tuples and sets by encoding them in a "(tuple|set)dict" if util.is_tuple(obj): return self._pop({tags.TUPLE: [ self.flatten(v) for v in obj ]}) if util.is_set(obj): return self._pop({tags.SET: [ self.flatten(v) for v in obj ]}) if util.is_dictionary(obj): return self._pop(self._flatten_dict_obj(obj, obj.__class__())) if util.is_type(obj): return self._pop(_mktyperef(obj)) if util.is_object(obj): if self._mkref(obj): # We've never seen this object so return its # json representation. return self._pop(self._flatten_obj_instance(obj)) else: # We've seen this object before so place an object # reference tag in the data. This avoids infinite recursion # when processing cyclical objects. return self._pop(self._getref(obj)) return self._pop(data)
def test_is_list_dict(self): self.assertFalse(is_list({"key": "value"})) self.assertFalse(is_set({"key": "value"})) self.assertFalse(is_tuple({"key": "value"}))