Skip to content

Commit 2ec492d

Browse files
committed
docs
1 parent 7b61e31 commit 2ec492d

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

deepdiff/deephash_doc.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ If you try to hash it:
132132
TypeError: unhashable type: 'dict'
133133

134134
But with DeepHash:
135+
135136
>>> from deepdiff import DeepHash
136137
>>> obj = {1: 2, 'a': 'b'}
137138
>>> DeepHash(obj)
@@ -141,11 +142,13 @@ But with DeepHash:
141142
DeepHash is calculating the hash of the obj and any other object that obj contains.
142143
The output of DeepHash is a dictionary of object IDs to their hashes.
143144
In order to get the hash of obj itself, you need to use the object (or the id of object) to get its hash:
145+
144146
>>> hashes = DeepHash(obj)
145147
>>> hashes[obj]
146148
34150898645750099477987229399128149852
147149

148150
Which you can write as:
151+
149152
>>> hashes = DeepHash(obj)[obj]
150153

151154
At first it might seem weird why DeepHash(obj)[obj] but remember that DeepHash(obj) is a dictionary of hashes of all other objects that obj contains too.
@@ -154,27 +157,32 @@ But with DeepHash:
154157
Murmur 3 128bit hashing algorithm. If you prefer to use another hashing algorithm, you can pass it using the hasher parameter. Read more about Murmur3 here: https://en.wikipedia.org/wiki/MurmurHash
155158

156159
If you do a deep copy of obj, it should still give you the same hash:
160+
157161
>>> from copy import deepcopy
158162
>>> obj2 = deepcopy(obj)
159163
>>> DeepHash(obj2)[obj2]
160164
34150898645750099477987229399128149852
161165

162166
Note that by default DeepHash will include string type differences. So if your strings were bytes:
167+
163168
>>> obj3 = {1: 2, b'a': b'b'}
164169
>>> DeepHash(obj3)[obj3]
165170
64067525765846024488103933101621212760
166171

167172
But if you want the same hash if string types are different, set ignore_string_type_changes to True:
173+
168174
>>> DeepHash(obj3, ignore_string_type_changes=True)[obj3]
169175
34150898645750099477987229399128149852
170176

171177
ignore_numeric_type_changes is by default False too.
178+
172179
>>> obj1 = {4:10}
173180
>>> obj2 = {4.0: Decimal(10.0)}
174181
>>> DeepHash(obj1)[4] == DeepHash(obj2)[4.0]
175182
False
176183

177184
But by setting it to True, we can get the same hash.
185+
178186
>>> DeepHash(obj1, ignore_numeric_type_changes=True)[4] == DeepHash(obj2, ignore_numeric_type_changes=True)[4.0]
179187
True
180188

@@ -184,6 +192,7 @@ number_format_notation: String, default = "f"
184192

185193
ignore_string_type_changes: Boolean, default = True
186194
By setting it to True, both the string and bytes of hello return the same hash.
195+
187196
>>> DeepHash(b'hello', ignore_string_type_changes=True)
188197
{b'hello': 221860156526691709602818861774599422448}
189198
>>> DeepHash('hello', ignore_string_type_changes=True)
@@ -203,11 +212,13 @@ ignore_numeric_type_changes: Boolean, default = False
203212
231678797214551245419120414857003063149
204213

205214
You can pass a list of tuples or list of lists if you have various type groups. When t1 and t2 both fall under one of these type groups, the type change will be ignored. DeepDiff already comes with 2 groups: DeepDiff.strings and DeepDiff.numbers . If you want to pass both:
215+
206216
>>> from deepdiff import DeepDiff
207217
>>> ignore_type_in_groups = [DeepDiff.strings, DeepDiff.numbers]
208218

209219

210220
ignore_type_in_groups example with custom objects:
221+
211222
>>> class Burrito:
212223
... bread = 'flour'
213224
... def __init__(self):
@@ -277,6 +288,7 @@ ignore_string_case
277288

278289
number_format_notation : string, default="f"
279290
When numbers are converted to the string, you have the choices between "f" as fixed point and "e" as scientific notation:
291+
280292
>>> t1=10002
281293
>>> t2=10004
282294
>>> t1_hash = DeepHash(t1, significant_digits=3, number_format_notation="f")
@@ -292,3 +304,21 @@ number_format_notation : string, default="f"
292304
>>>
293305
>>> t1_hash[t1] == t2_hash[t2]
294306
True
307+
308+
Defining your own number_to_string_func
309+
Lets say you want the hash of numbers below 100 to be the same for some reason.
310+
311+
>>> from deepdiff import DeepHash
312+
>>> from deepdiff.helper import number_to_string
313+
>>> def custom_number_to_string(number, *args, **kwargs):
314+
... number = 100 if number < 100 else number
315+
... return number_to_string(number, *args, **kwargs)
316+
...
317+
>>> t1 = [10, 12, 100000]
318+
>>> t2 = [50, 63, 100021]
319+
>>> t1_hash = DeepHash(t1, significant_digits=3, number_format_notation="e", number_to_string_func=custom_number_to_string)
320+
>>> t2_hash = DeepHash(t2, significant_digits=3, number_format_notation="e", number_to_string_func=custom_number_to_string)
321+
>>> t1_hash[t1] == t2_hash[t2]
322+
True
323+
324+
So both lists produced the same hash thanks to the low significant digits for 100000 vs 100021 and also the custom_number_to_string that converted all numbers below 100 to be 100!

deepdiff/diff_doc.rst

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -310,26 +310,22 @@ Approximate number comparison (significant_digits after the decimal point in sci
310310
{}
311311

312312
Defining your own number_to_string_func
313-
Lets say you want the numbers comparison do it in logarithmic scale and in scientific notation.
314-
>>> import math
313+
Lets say you want the numbers comparison happen only for numbers above 100 for some reason.
314+
315315
>>> from deepdiff import DeepDiff
316316
>>> from deepdiff.helper import number_to_string
317-
>>>
318-
>>>
319-
>>> def log_number_to_string(number, *args, **kwargs):
320-
... number = math.log(number)
317+
>>> def custom_number_to_string(number, *args, **kwargs):
318+
... number = 100 if number < 100 else number
321319
... return number_to_string(number, *args, **kwargs)
322320
...
323-
>>>
324-
>>> DeepDiff(100000, 100021, significant_digits=4, number_format_notation="e", number_to_string_func=log_number_to_string)
321+
>>> t1 = [10, 12, 100000]
322+
>>> t2 = [50, 63, 100021]
323+
>>> DeepDiff(t1, t2, significant_digits=3, number_format_notation="e")
324+
{'values_changed': {'root[0]': {'new_value': 50, 'old_value': 10}, 'root[1]': {'new_value': 63, 'old_value': 12}}}
325+
>>>
326+
>>> DeepDiff(t1, t2, significant_digits=3, number_format_notation="e",
327+
... number_to_string_func=custom_number_to_string)
325328
{}
326-
>>>
327-
>>> t1 = [10, 100000]
328-
>>> t2 = [11, 100021]
329-
>>>
330-
>>> DeepDiff(t1, t2, significant_digits=4, number_format_notation="e", number_to_string_func=log_number_to_string)
331-
{'values_changed': {'root[0]': {'new_value': 11, 'old_value': 10}}}
332-
333329

334330
.. note::
335331
All the examples for the text view work for the tree view too.

tests/test_diff_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ def custom_number_to_string(number, *args, **kwargs):
13921392
number = 100 if number < 100 else number
13931393
return number_to_string(number, *args, **kwargs)
13941394

1395-
ddiff = DeepDiff(100000, 100021, significant_digits=3, number_format_notation="e",
1395+
ddiff = DeepDiff(t1, t2, significant_digits=3, number_format_notation="e",
13961396
number_to_string_func=custom_number_to_string)
13971397

13981398
assert {} == ddiff

0 commit comments

Comments
 (0)