Example #1
0
def get_obj_cls(obj):
    if not obj or util.is_function(obj):
        return None

    if util.is_container(obj):
        return obj

    if not util.is_primitive(obj):
        return type(obj)

    return None
Example #2
0
    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
Example #3
0
    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
Example #4
0
    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
Example #5
0
    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
Example #6
0
 def test_is_primitive_unicode(self):
     self.assertTrue(is_primitive(unicode('hello')))
     self.assertTrue(is_primitive(unicode('')))
     self.assertTrue(is_primitive(unicode('hello')))
Example #7
0
 def test_is_primitive_None(self):
     self.assertTrue(is_primitive(None))
Example #8
0
 def test_is_primitive_unicode(self):
     self.assertTrue(is_primitive(unicode("hello")))
     self.assertTrue(is_primitive(unicode("")))
     self.assertTrue(is_primitive(unicode("hello")))
Example #9
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,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)
Example #10
0
 def test_is_primitive_bytes(self):
     self.assertFalse(util.is_primitive(b'hello'))
     self.assertFalse(util.is_primitive('foo'.encode('utf-8')))
     self.assertTrue(util.is_primitive('foo'))
Example #11
0
 def test_is_primitive_bytes(self):
     self.assertFalse(util.is_primitive(b'hello'))
     if PY2:
         self.assertFalse(util.is_primitive('foo'))
     else:
         self.assertTrue(util.is_primitive('foo'))
Example #12
0
 def test_is_primitive_long(self):
     self.assertTrue(util.is_primitive(long(3)))
Example #13
0
 def test_is_primitive_float(self):
     self.assertTrue(util.is_primitive(0))
     self.assertTrue(util.is_primitive(3.5))
     self.assertTrue(util.is_primitive(-3.5))
     self.assertTrue(util.is_primitive(float(3)))
Example #14
0
 def test_is_primitive_int(self):
     self.assertTrue(util.is_primitive(0))
     self.assertTrue(util.is_primitive(3))
     self.assertTrue(util.is_primitive(-3))
Example #15
0
 def test_is_primitive_set(self):
     self.assertFalse(util.is_primitive({1, 3}))
Example #16
0
 def test_is_primitive_bytes(self):
     self.assertFalse(util.is_primitive(b'hello'))
     self.assertFalse(util.is_primitive('foo'.encode('utf-8')))
     self.assertTrue(util.is_primitive('foo'))
Example #17
0
 def test_is_primitive_dict(self):
     self.assertFalse(is_primitive({"key": "value"}))
     self.assertFalse(is_primitive({}))
Example #18
0
 def test_is_primitive_dict(self):
     self.assertFalse(is_primitive({'key':'value'}))
     self.assertFalse(is_primitive({}))
Example #19
0
 def test_is_primitive_set(self):
     self.assertFalse(is_primitive(set([1, 3])))
Example #20
0
 def test_is_primitive_bool(self):
     self.assertTrue(util.is_primitive(True))
     self.assertTrue(util.is_primitive(False))
Example #21
0
 def test_is_primitive_None(self):
     self.assertTrue(util.is_primitive(None))
Example #22
0
 def test_is_primitive_str(self):
     self.assertTrue(util.is_primitive('hello'))
     self.assertTrue(util.is_primitive(''))
Example #23
0
 def test_is_primitive_long(self):
     self.assertTrue(util.is_primitive(2 ** 64))
Example #24
0
 def test_is_primitive_int(self):
     self.assertTrue(is_primitive(0))
     self.assertTrue(is_primitive(3))
     self.assertTrue(is_primitive(-3))
Example #25
0
 def test_is_primitive_unicode(self):
     self.assertTrue(util.is_primitive(compat.ustr('')))
     self.assertTrue(util.is_primitive('hello'))
Example #26
0
 def test_is_primitive_long(self):
     self.assertTrue(is_primitive(long(3)))
Example #27
0
 def test_is_primitive_unicode(self):
     self.assertTrue(util.is_primitive(unicode('hello')))
     self.assertTrue(util.is_primitive(unicode('')))
     self.assertTrue(util.is_primitive(unicode('hello')))
Example #28
0
 def test_is_primitive_str(self):
     self.assertTrue(is_primitive("hello"))
     self.assertTrue(is_primitive(""))
Example #29
0
 def test_is_primitive_float(self):
     self.assertTrue(is_primitive(0))
     self.assertTrue(is_primitive(3.5))
     self.assertTrue(is_primitive(-3.5))
     self.assertTrue(is_primitive(float(3)))
Example #30
0
 def test_is_primitive_list(self):
     self.assertFalse(util.is_primitive([]))
     self.assertFalse(util.is_primitive([4, 4]))
Example #31
0
 def test_is_primitive_bool(self):
     self.assertTrue(is_primitive(True))
     self.assertTrue(is_primitive(False))
Example #32
0
 def test_is_primitive_dict(self):
     self.assertFalse(util.is_primitive({'key': 'value'}))
     self.assertFalse(util.is_primitive({}))
Example #33
0
 def test_is_primitive_str(self):
     self.assertTrue(is_primitive('hello'))
     self.assertTrue(is_primitive(''))
Example #34
0
 def test_is_primitive_tuple(self):
     self.assertFalse(util.is_primitive((1, 3)))
     self.assertFalse(util.is_primitive((1, )))
Example #35
0
 def test_is_primitive_list(self):
     self.assertFalse(is_primitive([]))
     self.assertFalse(is_primitive([4, 4]))
Example #36
0
 def test_is_primitive_set(self):
     self.assertFalse(util.is_primitive(set([1, 3])))
Example #37
0
 def test_is_primitive_tuple(self):
     self.assertFalse(is_primitive((1, 3)))
     self.assertFalse(is_primitive((1,)))
Example #38
0
 def test_is_primitive_object(self):
     self.assertFalse(util.is_primitive(Thing('test')))
Example #39
0
 def test_is_primitive_object(self):
     self.assertFalse(is_primitive(Thing('test')))
Example #40
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,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)
Example #41
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,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
Example #42
0
 def test_is_primitive_set(self):
     self.assertFalse(util.is_primitive({1, 3}))
Example #43
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,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)
Example #44
0
 def test_is_primitive_bytes(self):
     self.assertFalse(util.is_primitive(b'hello'))
     if PY2:
         self.assertFalse(util.is_primitive('foo'))
     else:
         self.assertTrue(util.is_primitive('foo'))