GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fixing deephash hashes dictionary · FOSSAware/deepdiff@7084827

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • deepdiff
    • deephash.py
  • tests
    • test_hash.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎deepdiff/deephash.py‎

Copy file name to clipboardExpand all lines: deepdiff/deephash.py
+57-35Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ def __init__(self,
8080
"number_format_notation, apply_hash, ignore_type_in_groups, ignore_string_type_changes, "
8181
"ignore_numeric_type_changes, ignore_type_subclasses, ignore_string_case "
8282
"number_to_string_func, parent") % ', '.join(kwargs.keys()))
83-
if hashes is not None:
84-
if isinstance(hashes, MutableMapping):
85-
self.hashes = hashes
86-
elif isinstance(hashes, DeepHash):
87-
self.hashes = hashes.hashes
88-
else:
89-
self.hashes = {}
83+
if isinstance(hashes, MutableMapping):
84+
self.hashes = hashes
85+
elif isinstance(hashes, DeepHash):
86+
self.hashes = hashes.hashes
9087
else:
9188
self.hashes = {}
9289
self.obj = obj
@@ -158,17 +155,17 @@ def murmur3_128bit(obj):
158155

159156
def __getitem__(self, obj):
160157
key = obj
161-
result = None
158+
result_n_count = (None, 0)
162159

163160
try:
164-
result = self.hashes[key]
161+
result_n_count = self.hashes[key]
165162
except (TypeError, KeyError):
166163
key = get_id(obj)
167164
try:
168-
result = self.hashes[key]
165+
result_n_count = self.hashes[key]
169166
except KeyError:
170167
raise KeyError('{} is not one of the hashed items.'.format(obj)) from None
171-
return result
168+
return result_n_count[0] # To keep it backward compatible, we only get the hash and not the count
172169

173170
def __contains__(self, obj):
174171
result = False
@@ -180,13 +177,33 @@ def __contains__(self, obj):
180177
result = get_id(obj) in self.hashes
181178
return result
182179

180+
@property
181+
def only_hashes(self):
182+
"""
183+
A dictionary containing only the hashes and not the counts of items within each object.
184+
"""
185+
result = {}
186+
for key, value in self.hashes.items():
187+
if key not in RESERVED_DICT_KEYS:
188+
result[key] = value[0]
189+
else:
190+
result[key] = value
191+
return result
192+
183193
def __eq__(self, other):
184-
return self.hashes == other
194+
if isinstance(other, DeepHash):
195+
return self.hashes == other.hashes
196+
else:
197+
# We only care about the hashes
198+
return self.only_hashes == other
185199

186200
__req__ = __eq__
187201

188202
def __repr__(self):
189-
return short_repr(self.hashes, max_length=100)
203+
"""
204+
Hide the counts since it will be confusing to see them when they are hidden everywhere else.
205+
"""
206+
return short_repr(self.only_hashes, max_length=500)
190207

191208
__str__ = __repr__
192209

@@ -215,12 +232,12 @@ def _prep_obj(self, obj, parent, parents_ids=EMPTY_FROZENSET, is_namedtuple=Fals
215232
obj = {i: getattr(obj, i) for i in obj.__slots__}
216233
except AttributeError:
217234
self.hashes[UNPROCESSED].append(obj)
218-
return unprocessed
235+
return (unprocessed, 0)
219236

220-
result = self._prep_dict(obj, parent=parent, parents_ids=parents_ids,
221-
print_as_attribute=True, original_type=original_type)
237+
result, counts = self._prep_dict(obj, parent=parent, parents_ids=parents_ids,
238+
print_as_attribute=True, original_type=original_type)
222239
result = "nt{}".format(result) if is_namedtuple else "obj{}".format(result)
223-
return result
240+
return result, counts
224241

225242
def _skip_this(self, obj, parent):
226243
skip = False
@@ -238,6 +255,7 @@ def _skip_this(self, obj, parent):
238255
def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribute=False, original_type=None):
239256

240257
result = []
258+
counts = 0
241259

242260
key_text = "%s{}".format(INDEX_VS_ATTRIBUTE[print_as_attribute])
243261
for key, item in obj.items():
@@ -247,16 +265,17 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut
247265
key_formatted = "'%s'" % key if not print_as_attribute and isinstance(key, strings) else key
248266
key_in_report = key_text % (parent, key_formatted)
249267

250-
key_hash = self._hash(key, parent=key_in_report, parents_ids=parents_ids)
268+
key_hash, _ = self._hash(key, parent=key_in_report, parents_ids=parents_ids)
251269
if not key_hash:
252270
continue
253271
item_id = get_id(item)
254272
if (parents_ids and item_id in parents_ids) or self._skip_this(item, parent=key_in_report):
255273
continue
256274
parents_ids_added = add_to_frozen_set(parents_ids, item_id)
257-
hashed = self._hash(item, parent=key_in_report, parents_ids=parents_ids_added)
275+
hashed, count = self._hash(item, parent=key_in_report, parents_ids=parents_ids_added)
258276
hashed = KEY_TO_VAL_STR.format(key_hash, hashed)
259277
result.append(hashed)
278+
counts += count
260279

261280
result.sort()
262281
result = ';'.join(result)
@@ -269,10 +288,11 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut
269288
break
270289
else:
271290
type_str = 'dict'
272-
return "{}:{{{}}}".format(type_str, result)
291+
return "{}:{{{}}}".format(type_str, result), counts
273292

274293
def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
275294

295+
counts = 0
276296
result = defaultdict(int)
277297

278298
for i, item in enumerate(obj):
@@ -285,9 +305,10 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
285305
continue
286306

287307
parents_ids_added = add_to_frozen_set(parents_ids, item_id)
288-
hashed = self._hash(item, parent=new_parent, parents_ids=parents_ids_added)
308+
hashed, count = self._hash(item, parent=new_parent, parents_ids=parents_ids_added)
289309
# counting repetitions
290310
result[hashed] += 1
311+
counts += count
291312

292313
if self.ignore_repetition:
293314
result = list(result.keys())
@@ -300,7 +321,7 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET):
300321
result = ','.join(result)
301322
result = KEY_TO_VAL_STR.format(type(obj).__name__, result)
302323

303-
return result
324+
return result, counts
304325

305326
def _prep_bool(self, obj):
306327
return BoolObj.TRUE if obj else BoolObj.FALSE
@@ -319,29 +340,30 @@ def _prep_tuple(self, obj, parent, parents_ids):
319340
obj._asdict
320341
# It must be a normal tuple
321342
except AttributeError:
322-
result = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
343+
result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
323344
# We assume it is a namedtuple then
324345
else:
325-
result = self._prep_obj(obj, parent, parents_ids=parents_ids, is_namedtuple=True)
326-
return result
346+
result, counts = self._prep_obj(obj, parent, parents_ids=parents_ids, is_namedtuple=True)
347+
return result, counts
327348

328349
def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
329350
"""The main diff method"""
351+
counts = 1
330352

331353
if isinstance(obj, bool):
332354
obj = self._prep_bool(obj)
333355
result = None
334356
else:
335357
result = not_hashed
336358
try:
337-
result = self.hashes[obj]
359+
result, counts = self.hashes[obj]
338360
except (TypeError, KeyError):
339361
pass
340362
else:
341-
return result
363+
return result, counts
342364

343365
if self._skip_this(obj, parent):
344-
return
366+
return None, 0
345367

346368
elif obj is None:
347369
result = 'NONE'
@@ -355,18 +377,18 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
355377
result = self._prep_number(obj)
356378

357379
elif isinstance(obj, MutableMapping):
358-
result = self._prep_dict(obj=obj, parent=parent, parents_ids=parents_ids)
380+
result, counts = self._prep_dict(obj=obj, parent=parent, parents_ids=parents_ids)
359381

360382
elif isinstance(obj, tuple):
361-
result = self._prep_tuple(obj=obj, parent=parent, parents_ids=parents_ids)
383+
result, counts = self._prep_tuple(obj=obj, parent=parent, parents_ids=parents_ids)
362384

363385
elif isinstance(obj, Iterable):
364-
result = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
386+
result, counts = self._prep_iterable(obj=obj, parent=parent, parents_ids=parents_ids)
365387

366388
elif obj == BoolObj.TRUE or obj == BoolObj.FALSE:
367389
result = 'bool:true' if obj is BoolObj.TRUE else 'bool:false'
368390
else:
369-
result = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids)
391+
result, counts = self._prep_obj(obj=obj, parent=parent, parents_ids=parents_ids)
370392

371393
if result is not_hashed: # pragma: no cover
372394
self.hashes[UNPROCESSED].append(obj)
@@ -387,12 +409,12 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET):
387409
# The hashes will be later used for comparing the objects.
388410
# Object to hash when possible otherwise ObjectID to hash
389411
try:
390-
self.hashes[obj] = result
412+
self.hashes[obj] = (result, counts)
391413
except TypeError:
392414
obj_id = get_id(obj)
393-
self.hashes[obj_id] = result
415+
self.hashes[obj_id] = (result, counts)
394416

395-
return result
417+
return result, counts
396418

397419

398420
if __name__ == "__main__": # pragma: no cover
Collapse file

‎tests/test_hash.py‎

Copy file name to clipboardExpand all lines: tests/test_hash.py
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def __getattr__(self, key):
7272
def __str__(self):
7373
return "Bad Object"
7474

75+
def __repr__(self):
76+
return "<Bad obj id {}>".format(id(self))
77+
7578
t1 = Bad()
7679

7780
result = DeepHash(t1)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.