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_same_sets_same_hash(self):
t1 = {1, 3, 2}
t2 = {2, 3, 1}
t1_hash = DeepHashPrep(t1)
t2_hash = DeepHashPrep(t2)
assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]
def test_repetition_by_default_does_not_effect(self):
list1 = [3, 4]
list1_id = get_id(list1)
a = [1, 2, list1]
a_id = get_id(a)
list2 = [4, 3, 3]
list2_id = get_id(list2)
b = [list2, 2, 1]
b_id = get_id(b)
hash_a = DeepHashPrep(a)
hash_b = DeepHashPrep(b)
assert hash_a[list1_id] == hash_b[list2_id]
assert hash_a[a_id] == hash_b[b_id]
def test_nested_lists_same_hash2(self):
t1 = [1, 2, [3, [4, 5]]]
t2 = [[[5, 4], 3], 2, 1]
t1_hash = DeepHashPrep(t1)
t2_hash = DeepHashPrep(t2)
assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]
def test_setting_repetition_off_unequal_hash(self):
list1 = [3, 4]
list1_id = get_id(list1)
a = [1, 2, list1]
a_id = get_id(a)
list2 = [4, 3, 3]
list2_id = get_id(list2)
b = [list2, 2, 1]
b_id = get_id(b)
hash_a = DeepHashPrep(a, ignore_repetition=False)
hash_b = DeepHashPrep(b, ignore_repetition=False)
assert not hash_a[list1_id] == hash_b[list2_id]
assert not hash_a[a_id] == hash_b[b_id]
assert hash_a[list1_id].replace('3|1', '3|2') == hash_b[list2_id]
def test_same_sets_in_lists_same_hash(self):
t1 = ["a", {1, 3, 2}]
t2 = [{2, 3, 1}, "a"]
t1_hash = DeepHashPrep(t1)
t2_hash = DeepHashPrep(t2)
assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]
def test_list_of_sets(self):
a = {1}
b = {2}
obj = [a, b]
result = DeepHash(obj)
expected_result = {1, 2, get_id(a), get_id(b), get_id(obj)}
assert set(result.keys()) == expected_result
def test_setting_repetition_off_unequal_hash(self):
list1 = [3, 4]
list1_id = get_id(list1)
a = [1, 2, list1]
a_id = get_id(a)
list2 = [4, 3, 3]
list2_id = get_id(list2)
b = [list2, 2, 1]
b_id = get_id(b)
hash_a = DeepHashPrep(a, ignore_repetition=False)
hash_b = DeepHashPrep(b, ignore_repetition=False)
assert not hash_a[list1_id] == hash_b[list2_id]
assert not hash_a[a_id] == hash_b[b_id]
assert hash_a[list1_id].replace('3|1', '3|2') == hash_b[list2_id]
def test_nested_lists_same_hash3(self):
t1 = [{1: [2, 3], 4: [5, [6, 7]]}]
t2 = [{4: [[7, 6], 5], 1: [3, 2]}]
t1_hash = DeepHashPrep(t1)
t2_hash = DeepHashPrep(t2)
assert t1_hash[get_id(t1)] == t2_hash[get_id(t2)]
def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribute=False, original_type=None):
result = []
key_text = "%s{}".format(INDEX_VS_ATTRIBUTE[print_as_attribute])
for key, item in obj.items():
key_formatted = "'%s'" % key if not print_as_attribute and isinstance(key, strings) else key
key_in_report = key_text % (parent, key_formatted)
key_hash = self._hash(key, parent=key_in_report, parents_ids=parents_ids)
item_id = get_id(item)
if (parents_ids and item_id in parents_ids) or self._skip_this(item, parent=key_in_report):
continue
parents_ids_added = add_to_frozen_set(parents_ids, item_id)
hashed = self._hash(item, parent=key_in_report, parents_ids=parents_ids_added)
hashed = KEY_TO_VAL_STR.format(key_hash, hashed)
result.append(hashed)
result.sort()
result = ';'.join(result)
if print_as_attribute:
type_ = original_type or type(obj)
type_str = type_.__name__
for type_group in self.ignore_type_in_groups:
if self.type_check_func(type_, type_group):
type_str = ','.join(map(lambda x: x.__name__, type_group))
break
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:
obj_id = get_id(obj)
self[obj_id] = result
return result