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_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_list(self):
self.assertTrue(util.is_list([1, 2]))
restore = self._restore_id
elif has_tag(obj, tags.REF): # Backwards compatibility
restore = self._restore_ref
elif has_tag(obj, tags.ITERATOR):
restore = self._restore_iterator
elif has_tag(obj, tags.TYPE):
restore = self._restore_type
elif has_tag(obj, tags.REPR): # Backwards compatibility
restore = self._restore_repr
elif has_tag(obj, tags.REDUCE):
restore = self._restore_reduce
elif has_tag(obj, tags.OBJECT):
restore = self._restore_object
elif has_tag(obj, tags.FUNCTION):
restore = self._restore_function
elif util.is_list(obj):
restore = self._restore_list
elif has_tag(obj, tags.TUPLE):
restore = self._restore_tuple
elif has_tag(obj, tags.SET):
restore = self._restore_set
elif util.is_dictionary(obj):
restore = self._restore_dict
else:
def restore(x):
return x
return restore(obj)
[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):
restore = self._restore_id
elif has_tag(obj, tags.REF): # Backwards compatibility
restore = self._restore_ref
elif has_tag(obj, tags.ITERATOR):
restore = self._restore_iterator
elif has_tag(obj, tags.TYPE):
restore = self._restore_type
elif has_tag(obj, tags.REPR): # Backwards compatibility
restore = self._restore_repr
elif has_tag(obj, tags.REDUCE):
restore = self._restore_reduce
elif has_tag(obj, tags.OBJECT):
restore = self._restore_object
elif has_tag(obj, tags.FUNCTION):
restore = self._restore_function
elif util.is_list(obj):
restore = self._restore_list
elif has_tag(obj, tags.TUPLE):
restore = self._restore_tuple
elif has_tag(obj, tags.SET):
restore = self._restore_set
elif util.is_dictionary(obj):
restore = self._restore_dict
else:
restore = lambda x: x
return restore(obj)
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]}
restore = self._restore_id
elif has_tag(obj, tags.REF): # Backwards compatibility
restore = self._restore_ref
elif has_tag(obj, tags.ITERATOR):
restore = self._restore_iterator
elif has_tag(obj, tags.TYPE):
restore = self._restore_type
elif has_tag(obj, tags.REPR): # Backwards compatibility
restore = self._restore_repr
elif has_tag(obj, tags.REDUCE):
restore = self._restore_reduce
elif has_tag(obj, tags.OBJECT):
restore = self._restore_object
elif has_tag(obj, tags.FUNCTION):
restore = self._restore_function
elif util.is_list(obj):
restore = self._restore_list
elif has_tag(obj, tags.TUPLE):
restore = self._restore_tuple
elif has_tag(obj, tags.SET):
restore = self._restore_set
elif util.is_dictionary(obj):
restore = self._restore_dict
else:
restore = lambda x: x
return restore(obj)
[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):
# ignore the reserved attribute
if k in tags.RESERVED:
continue
self._namestack.append(k)
# step into the namespace
value = self.restore(v)
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
instance[k] = value
else:
instance.__dict__[k] = value
# step out
self._namestack.pop()
return self._pop(instance)
if util.is_list(obj):
return self._pop([self.restore(v) for v in obj])
if has_tag(obj, tags.TUPLE):
return self._pop(tuple([self.restore(v) for v in obj[tags.TUPLE]]))
if has_tag(obj, tags.SET):
return self._pop(set([self.restore(v) for v in obj[tags.SET]]))
if util.is_dictionary(obj):
data = {}
for k, v in obj.iteritems():
self._namestack.append(k)
data[k] = self.restore(v)
self._namestack.pop()
return self._pop(data)