Skip to content

Commit d6074cb

Browse files
committed
more delta docs
1 parent f1325e3 commit d6074cb

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

docs/delta_doc.rst

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,72 @@ In fact only a few Python object types are allowed by default. The user of DeepD
160160
Delta Mutate parameter
161161
----------------------
162162

163-
Whether to mutate the original object or not.
163+
Whether to mutate the original object when applying the delta or not.
164+
165+
For example:
166+
167+
>>> t1 = [1, 2, [3, 5, 6]]
168+
>>> t2 = [2, 3, [3, 6, 8]]
169+
170+
>>> diff = DeepDiff(t1, t2, ignore_order=True, report_repetition=True)
171+
>>> diff
172+
{'values_changed': {'root[0]': {'new_value': 3, 'old_value': 1}, 'root[2][1]': {'new_value': 8, 'old_value': 5}}}
173+
>>> delta = Delta(diff)
174+
>>> delta
175+
<Delta: {'values_changed': {'root[0]': {'new_value': 3}, 'root[2][1]': {'new_value': 8}}}>
176+
177+
Note that we can apply delta to objects different than the original objects there were made from:
178+
179+
>>> t3 = ["a", 2, [3, "b", "c"]]
180+
>>> t3 + delta
181+
[3, 2, [3, 8, 'c']]
182+
183+
If we check t3, it is still the same as the original value of t3:
184+
185+
>>> t3
186+
['a', 2, [3, 'b', 'c']]
187+
188+
Now let's make the delta with mutate=True
189+
190+
>>> delta2 = Delta(diff, mutate=True)
191+
>>> t3 + delta2
192+
[3, 2, [3, 8, 'c']]
193+
>>> t3
194+
[3, 2, [3, 8, 'c']]
195+
196+
Applying the delta to t3 mutated the t3 itself in this case!
197+
164198

165199
.. _delta_and_numpy_label:
166200

167201
Delta and Numpy
168202
---------------
169203

204+
>>> from deepdiff import DeepDiff, Delta
205+
>>> import numpy as np
206+
>>> t1 = np.array([1, 2, 3, 5])
207+
>>> t2 = np.array([2, 2, 7, 5])
208+
>>> diff = DeepDiff(t1, t2)
209+
>>> diff
210+
{'values_changed': {'root[0]': {'new_value': 2, 'old_value': 1}, 'root[2]': {'new_value': 7, 'old_value': 3}}}
211+
>>> delta = Delta(diff)
212+
170213
.. note::
171214
When applying delta to Numpy arrays, make sure to put the delta object first and the numpy array second. This is because Numpy array overrides the + operator and thus DeepDiff's Delta won't be able to be applied.
172215

173-
>>>
216+
>>> t1 + delta
217+
Traceback (most recent call last):
218+
File "<stdin>", line 1, in <module>
219+
raise DeltaNumpyOperatorOverrideError(DELTA_NUMPY_OPERATOR_OVERRIDE_MSG)
220+
deepdiff.delta.DeltaNumpyOperatorOverrideError: A numpy ndarray is most likely being added to a delta. Due to Numpy override the + operator, you can only do: delta + ndarray and NOT ndarray + delta
221+
222+
Let's put the delta first then:
223+
224+
>>> delta + t1
225+
array([2, 2, 7, 5])
226+
>>> delta + t2 == t2
227+
array([ True, True, True, True])
228+
174229

175230
.. note::
176231
You can not apply a delta that was created from normal Python objects to Numpy arrays.

0 commit comments

Comments
 (0)