Debian Codegraph @

docs/faq.rst

raw
1 :doc:`/index`
2
3 F.A.Q
4 =====
5
6 .. |qluster_link| raw:: html
7
8 <a href="/qluster"><strong>Qluster</strong></a>
9
10 .. admonition:: DeepDiff is now part of |qluster_link|.
11
12 *If you're building workflows around data validation and correction,* `Qluster </qluster>`__ *gives your team a structured way to manage rules, review failures, approve fixes, and reuse decisions—without building the entire system from scratch.*
13
14 .. Note::
15 |:mega:| **Please fill out our** `fast 5-question survey <https://forms.gle/E6qXexcgjoKnSzjB8>`__ so that we can learn how & why you use DeepDiff, and what improvements we should make. Thank you! |:dancers:|
16
17
18 Q: DeepDiff report is not precise when ignore_order=True
19 --------------------------------------------------------
20
21 >>> from deepdiff import DeepDiff
22 >>> from pprint import pprint
23 >>> t1 = [
24 ... {
25 ... "key": "some/pathto/customers/foo/",
26 ... "flags": 0,
27 ... "value": ""
28 ... },
29 ... {
30 ... "key": "some/pathto/customers/foo/account_number",
31 ... "flags": 0,
32 ... "value": "somevalue1"
33 ... }
34 ... ]
35 >>>
36 >>> t2 = [
37 ... {
38 ... "key": "some/pathto/customers/foo/account_number",
39 ... "flags": 0,
40 ... "value": "somevalue2"
41 ... },
42 ... {
43 ... "key": "some/pathto/customers/foo/",
44 ... "flags": 0,
45 ... "value": "new"
46 ... }
47 ... ]
48 >>>
49 >>> pprint(DeepDiff(t1, t2))
50 {'values_changed': {"root[0]['key']": {'new_value': 'some/pathto/customers/foo/account_number',
51 'old_value': 'some/pathto/customers/foo/'},
52 "root[0]['value']": {'new_value': 'somevalue2',
53 'old_value': ''},
54 "root[1]['key']": {'new_value': 'some/pathto/customers/foo/',
55 'old_value': 'some/pathto/customers/foo/account_number'},
56 "root[1]['value']": {'new_value': 'new',
57 'old_value': 'somevalue1'}}}
58
59 **Answer**
60
61 This is explained in :ref:`cutoff_distance_for_pairs_label` and :ref:`cutoff_intersection_for_pairs_label`
62
63 Bump up these 2 parameters to 1 and you get what you want:
64
65 >>> pprint(DeepDiff(t1, t2, ignore_order=True, cutoff_distance_for_pairs=1, cutoff_intersection_for_pairs=1))
66 {'values_changed': {"root[0]['value']": {'new_value': 'new', 'old_value': ''},
67 "root[1]['value']": {'new_value': 'somevalue2',
68 'old_value': 'somevalue1'}}}
69
70
71 Q: The report of changes in a nested dictionary is too granular
72 ---------------------------------------------------------------
73
74 **Answer**
75
76 Use :ref:`threshold_to_diff_deeper_label`
77
78 >>> from deepdiff import DeepDiff
79 >>> t1 = {"veggie": "carrots"}
80 >>> t2 = {"meat": "carrots"}
81 >>>
82 >>> DeepDiff(t1, t2, threshold_to_diff_deeper=0)
83 {'dictionary_item_added': ["root['meat']"], 'dictionary_item_removed': ["root['veggie']"]}
84 >>> DeepDiff(t1, t2, threshold_to_diff_deeper=0.33)
85 {'values_changed': {'root': {'new_value': {'meat': 'carrots'}, 'old_value': {'veggie': 'carrots'}}}}
86
87
88
89 Q: TypeError: Object of type type is not JSON serializable
90 ----------------------------------------------------------
91
92 I'm trying to serialize the DeepDiff results into json and I'm getting the TypeError.
93
94 >>> diff=DeepDiff(1, "a")
95 >>> diff
96 {'type_changes': {'root': {'old_type': <class 'int'>, 'new_type': <class 'str'>, 'old_value': 1, 'new_value': 'a'}}}
97 >>> json.dumps(diff)
98 Traceback (most recent call last):
99 File "<stdin>", line 1, in <module>
100 File ".../json/__init__.py", line 231, in dumps
101 return _default_encoder.encode(obj)
102 File ".../json/encoder.py", line 199, in encode
103 chunks = self.iterencode(o, _one_shot=True)
104 File ".../json/encoder.py", line 257, in iterencode
105 return _iterencode(o, 0)
106 File ".../json/encoder.py", line 179, in default
107 raise TypeError(f'Object of type {o.__class__.__name__} '
108 TypeError: Object of type type is not JSON serializable
109
110 **Answer**
111
112 In order to serialize DeepDiff results into json, use to_json()
113
114 >>> diff.to_json()
115 '{"type_changes": {"root": {"old_type": "int", "new_type": "str", "old_value": 1, "new_value": "a"}}}'
116
117
118 Q: How do I parse DeepDiff result paths?
119 ----------------------------------------
120
121 **Answer**
122
123 Use parse_path:
124
125 >>> from deepdiff import parse_path
126 >>> parse_path("root[1][2]['age']")
127 [1, 2, 'age']
128 >>> parse_path("root[1][2]['age']", include_actions=True)
129 [{'element': 1, 'action': 'GET'}, {'element': 2, 'action': 'GET'}, {'element': 'age', 'action': 'GET'}]
130 >>>
131 >>> parse_path("root['joe'].age")
132 ['joe', 'age']
133 >>> parse_path("root['joe'].age", include_actions=True)
134 [{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}]
135
136 Or use the tree view so you can use path(output_format='list'):
137
138 >>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
139 >>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}}
140 >>> ddiff = DeepDiff(t1, t2, view='tree')
141 >>> ddiff
142 {'iterable_item_removed': [<root[4]['b'][2] t1:3, t2:not present>, <root[4]['b'][3] t1:4, t2:not present>]}
143 >>> # Note that the iterable_item_removed is a set. In this case it has 2 items in it.
144 >>> # One way to get one item from the set is to convert it to a list
145 >>> # And then get the first item of the list:
146 >>> removed = list(ddiff['iterable_item_removed'])[0]
147 >>> removed
148 <root[4]['b'][2] t1:3, t2:not present>
149 >>>
150 >>> parent = removed.up
151 >>> parent
152 <root[4]['b'] t1:[1, 2, 3, 4], t2:[1, 2]>
153 >>> parent.path() # gives you the string representation of the path
154 "root[4]['b']"
155 >>> parent.path(output_format='list') # gives you the list of keys and attributes that make up the path
156 [4, 'b']
157
158
159 Q: Why my datetimes are reported in UTC?
160 ----------------------------------------
161
162 **Answer**
163
164 DeepDiff converts all datetimes into UTC. If a datetime is timezone naive, we assume it is in UTC too.
165 That is different than what Python does. Python assumes your timezone naive datetime is in your local timezone. However, you can override it to any other time zone such as your :ref:`default_timezone_label`.
166
167 >>> from deepdiff import DeepDiff
168 >>> from datetime import datetime, timezone
169 >>> d1 = datetime(2020, 8, 31, 13, 14, 1)
170 >>> d2 = datetime(2020, 8, 31, 13, 14, 1, tzinfo=timezone.utc)
171 >>> d1 == d2
172 False
173 >>> DeepDiff(d1, d2)
174 {}
175
176 >>> d3 = d2.astimezone(pytz.timezone('America/New_York'))
177 >>> DeepDiff(d1, d3)
178 {}
179 >>> d1 == d3
180 False
181
182
183 ---------
184
185 Back to :doc:`/index`