Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_primitive_bool(self):
self.assertTrue(util.is_primitive(True))
self.assertTrue(util.is_primitive(False))
def test_is_primitive_str(self):
self.assertTrue(util.is_primitive('hello'))
self.assertTrue(util.is_primitive(''))
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)))
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')))
def test_is_primitive_object(self):
self.assertFalse(util.is_primitive(Thing('test')))
>>> 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):
def _get_flattener(self, obj):
if PY2 and isinstance(obj, types.FileType):
return self._flatten_file
if util.is_bytes(obj):
return self._flatten_bytestring
if util.is_primitive(obj):
return lambda obj: obj
# Decimal is a primitive when use_decimal is True
if self._use_decimal and isinstance(obj, decimal.Decimal):
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"
>>> 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):