@@ -80,13 +80,10 @@ def __init__(self, |
80 | 80 | "number_format_notation, apply_hash, ignore_type_in_groups, ignore_string_type_changes, " |
81 | 81 | "ignore_numeric_type_changes, ignore_type_subclasses, ignore_string_case " |
82 | 82 | "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 |
90 | 87 | else: |
91 | 88 | self.hashes = {} |
92 | 89 | self.obj = obj |
@@ -158,17 +155,17 @@ def murmur3_128bit(obj): |
158 | 155 |
|
159 | 156 | def __getitem__(self, obj): |
160 | 157 | key = obj |
161 | | - result = None |
| 158 | + result_n_count = (None, 0) |
162 | 159 |
|
163 | 160 | try: |
164 | | - result = self.hashes[key] |
| 161 | + result_n_count = self.hashes[key] |
165 | 162 | except (TypeError, KeyError): |
166 | 163 | key = get_id(obj) |
167 | 164 | try: |
168 | | - result = self.hashes[key] |
| 165 | + result_n_count = self.hashes[key] |
169 | 166 | except KeyError: |
170 | 167 | 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 |
172 | 169 |
|
173 | 170 | def __contains__(self, obj): |
174 | 171 | result = False |
@@ -180,13 +177,33 @@ def __contains__(self, obj): |
180 | 177 | result = get_id(obj) in self.hashes |
181 | 178 | return result |
182 | 179 |
|
| 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 | + |
183 | 193 | 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 |
185 | 199 |
|
186 | 200 | __req__ = __eq__ |
187 | 201 |
|
188 | 202 | 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) |
190 | 207 |
|
191 | 208 | __str__ = __repr__ |
192 | 209 |
|
@@ -215,12 +232,12 @@ def _prep_obj(self, obj, parent, parents_ids=EMPTY_FROZENSET, is_namedtuple=Fals |
215 | 232 | obj = {i: getattr(obj, i) for i in obj.__slots__} |
216 | 233 | except AttributeError: |
217 | 234 | self.hashes[UNPROCESSED].append(obj) |
218 | | - return unprocessed |
| 235 | + return (unprocessed, 0) |
219 | 236 |
|
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) |
222 | 239 | result = "nt{}".format(result) if is_namedtuple else "obj{}".format(result) |
223 | | - return result |
| 240 | + return result, counts |
224 | 241 |
|
225 | 242 | def _skip_this(self, obj, parent): |
226 | 243 | skip = False |
@@ -238,6 +255,7 @@ def _skip_this(self, obj, parent): |
238 | 255 | def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribute=False, original_type=None): |
239 | 256 |
|
240 | 257 | result = [] |
| 258 | + counts = 0 |
241 | 259 |
|
242 | 260 | key_text = "%s{}".format(INDEX_VS_ATTRIBUTE[print_as_attribute]) |
243 | 261 | for key, item in obj.items(): |
@@ -247,16 +265,17 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut |
247 | 265 | key_formatted = "'%s'" % key if not print_as_attribute and isinstance(key, strings) else key |
248 | 266 | key_in_report = key_text % (parent, key_formatted) |
249 | 267 |
|
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) |
251 | 269 | if not key_hash: |
252 | 270 | continue |
253 | 271 | item_id = get_id(item) |
254 | 272 | if (parents_ids and item_id in parents_ids) or self._skip_this(item, parent=key_in_report): |
255 | 273 | continue |
256 | 274 | 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) |
258 | 276 | hashed = KEY_TO_VAL_STR.format(key_hash, hashed) |
259 | 277 | result.append(hashed) |
| 278 | + counts += count |
260 | 279 |
|
261 | 280 | result.sort() |
262 | 281 | result = ';'.join(result) |
@@ -269,10 +288,11 @@ def _prep_dict(self, obj, parent, parents_ids=EMPTY_FROZENSET, print_as_attribut |
269 | 288 | break |
270 | 289 | else: |
271 | 290 | type_str = 'dict' |
272 | | - return "{}:{{{}}}".format(type_str, result) |
| 291 | + return "{}:{{{}}}".format(type_str, result), counts |
273 | 292 |
|
274 | 293 | def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
275 | 294 |
|
| 295 | + counts = 0 |
276 | 296 | result = defaultdict(int) |
277 | 297 |
|
278 | 298 | for i, item in enumerate(obj): |
@@ -285,9 +305,10 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
285 | 305 | continue |
286 | 306 |
|
287 | 307 | 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) |
289 | 309 | # counting repetitions |
290 | 310 | result[hashed] += 1 |
| 311 | + counts += count |
291 | 312 |
|
292 | 313 | if self.ignore_repetition: |
293 | 314 | result = list(result.keys()) |
@@ -300,7 +321,7 @@ def _prep_iterable(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
300 | 321 | result = ','.join(result) |
301 | 322 | result = KEY_TO_VAL_STR.format(type(obj).__name__, result) |
302 | 323 |
|
303 | | - return result |
| 324 | + return result, counts |
304 | 325 |
|
305 | 326 | def _prep_bool(self, obj): |
306 | 327 | return BoolObj.TRUE if obj else BoolObj.FALSE |
@@ -319,29 +340,30 @@ def _prep_tuple(self, obj, parent, parents_ids): |
319 | 340 | obj._asdict |
320 | 341 | # It must be a normal tuple |
321 | 342 | 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) |
323 | 344 | # We assume it is a namedtuple then |
324 | 345 | 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 |
327 | 348 |
|
328 | 349 | def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
329 | 350 | """The main diff method""" |
| 351 | + counts = 1 |
330 | 352 |
|
331 | 353 | if isinstance(obj, bool): |
332 | 354 | obj = self._prep_bool(obj) |
333 | 355 | result = None |
334 | 356 | else: |
335 | 357 | result = not_hashed |
336 | 358 | try: |
337 | | - result = self.hashes[obj] |
| 359 | + result, counts = self.hashes[obj] |
338 | 360 | except (TypeError, KeyError): |
339 | 361 | pass |
340 | 362 | else: |
341 | | - return result |
| 363 | + return result, counts |
342 | 364 |
|
343 | 365 | if self._skip_this(obj, parent): |
344 | | - return |
| 366 | + return None, 0 |
345 | 367 |
|
346 | 368 | elif obj is None: |
347 | 369 | result = 'NONE' |
@@ -355,18 +377,18 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
355 | 377 | result = self._prep_number(obj) |
356 | 378 |
|
357 | 379 | 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) |
359 | 381 |
|
360 | 382 | 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) |
362 | 384 |
|
363 | 385 | 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) |
365 | 387 |
|
366 | 388 | elif obj == BoolObj.TRUE or obj == BoolObj.FALSE: |
367 | 389 | result = 'bool:true' if obj is BoolObj.TRUE else 'bool:false' |
368 | 390 | 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) |
370 | 392 |
|
371 | 393 | if result is not_hashed: # pragma: no cover |
372 | 394 | self.hashes[UNPROCESSED].append(obj) |
@@ -387,12 +409,12 @@ def _hash(self, obj, parent, parents_ids=EMPTY_FROZENSET): |
387 | 409 | # The hashes will be later used for comparing the objects. |
388 | 410 | # Object to hash when possible otherwise ObjectID to hash |
389 | 411 | try: |
390 | | - self.hashes[obj] = result |
| 412 | + self.hashes[obj] = (result, counts) |
391 | 413 | except TypeError: |
392 | 414 | obj_id = get_id(obj) |
393 | | - self.hashes[obj_id] = result |
| 415 | + self.hashes[obj_id] = (result, counts) |
394 | 416 |
|
395 | | - return result |
| 417 | + return result, counts |
396 | 418 |
|
397 | 419 |
|
398 | 420 | if __name__ == "__main__": # pragma: no cover |
|