Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _prep_obj(self, obj, parents_ids=frozenset({}), is_namedtuple=False):
"""Difference of 2 objects"""
try:
if is_namedtuple:
obj = obj._asdict()
else:
obj = obj.__dict__
except AttributeError:
try:
obj = {i: getattr(obj, i) for i in obj.__slots__}
except AttributeError:
self[UNPROCESSED].append(obj)
return unprocessed
result = self._prep_dict(obj, parents_ids)
result = "nt{}".format(result) if is_namedtuple else "obj{}".format(result)
return result
elif isinstance(obj, tuple):
result = self._prep_tuple(obj, parents_ids)
elif isinstance(obj, (set, frozenset)):
result = self._prep_set(obj)
elif isinstance(obj, Iterable):
result = self._prep_iterable(obj, parents_ids)
else:
result = self._prep_obj(obj, parents_ids)
if result is not_hashed: # pragma: no cover
self[UNPROCESSED].append(obj)
elif result is unprocessed:
pass
elif self.constant_size:
if isinstance(obj, strings):
result_cleaned = result
else:
result_cleaned = prepare_string_for_hashing(result, include_string_type_changes=self.include_string_type_changes)
result = self.hasher(result_cleaned)
# It is important to keep the hash of all objects.
# The hashes will be later used for comparing the objects.
self[obj_id] = result
return result
def _prep_obj(self, obj, parent, parents_ids=EMPTY_FROZENSET, is_namedtuple=False):
"""Difference of 2 objects"""
original_type = type(obj)
try:
if is_namedtuple:
obj = obj._asdict()
else:
obj = obj.__dict__
except AttributeError:
try:
obj = {i: getattr(obj, i) for i in obj.__slots__}
except AttributeError:
self[UNPROCESSED].append(obj)
return unprocessed
result = self._prep_dict(obj, parent=parent, parents_ids=parents_ids,
print_as_attribute=True, original_type=original_type)
result = "nt{}".format(result) if is_namedtuple else "obj{}".format(result)
return result
significant_digits=self.significant_digits,
number_format_notation=self.number_format_notation,
ignore_string_type_changes=self.ignore_string_type_changes,
ignore_numeric_type_changes=self.ignore_numeric_type_changes,
ignore_type_in_groups=self.ignore_type_in_groups,
ignore_type_subclasses=self.ignore_type_subclasses,
ignore_string_case=self.ignore_string_case,
number_to_string_func=self.number_to_string,
)
item_hash = hashes_all[item]
except Exception as e: # pragma: no cover
logger.error("Can not produce a hash for %s."
"Not counting this object.\n %s" %
(level.path(), e))
else:
if item_hash is unprocessed: # pragma: no cover
logger.warning("Item %s was not processed while hashing "
"thus not counting this object." %
level.path())
else:
self._add_hash(hashes=hashes, item_hash=item_hash, item=item, i=i)
return hashes
elif isinstance(obj, tuple):
result = self._prep_tuple(obj=obj, parent=parent, parents_ids=parents_ids)
elif isinstance(obj, Iterable):
result = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
elif obj == BoolObj.TRUE or obj == BoolObj.FALSE:
result = 'bool:true' if obj is BoolObj.TRUE else 'bool:false'
else:
result = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids)
if result is not_hashed: # pragma: no cover
self[UNPROCESSED].append(obj)
elif result is unprocessed:
pass
elif self.apply_hash:
if isinstance(obj, strings):
result_cleaned = result
else:
result_cleaned = prepare_string_for_hashing(
result, ignore_string_type_changes=self.ignore_string_type_changes,
ignore_string_case=self.ignore_string_case)
result = self.hasher(result_cleaned)
# It is important to keep the hash of all objects.
# The hashes will be later used for comparing the objects.
try:
self[obj] = result
except TypeError: