Debian Codegraph @

docs/commandline.rst

raw
1 :doc:`/index`
2
3 Command Line
4 ============
5
6 `New in DeepDiff 5.2.0`
7
8 DeepDiff provides commandline interface to a subset of functionality that it provides through its Python API.
9
10 The commands are:
11
12 - :ref:`deep_diff_command`
13 - :ref:`deep_grep_command`
14 - :ref:`deep_extract_command`
15 - :ref:`deep_patch_command`
16
17
18 .. _deep_diff_command:
19
20 deep diff command
21 -----------------
22
23 Run
24
25 .. code:: bash
26
27 $ deep diff
28
29 to get the options:
30
31 .. code-block:: bash
32
33 $ deep diff --help
34 Usage: deep diff [OPTIONS] T1 T2
35
36 Deep Diff Commandline
37
38 Deep Difference of content in files.
39 It can read csv, tsv, json, yaml, and toml files.
40
41 T1 and T2 are the path to the files to be compared with each other.
42
43 Options:
44 --cutoff-distance-for-pairs FLOAT
45 [default: 0.3]
46 --cutoff-intersection-for-pairs FLOAT
47 [default: 0.7]
48 --cache-size INTEGER [default: 0]
49 --cache-tuning-sample-size INTEGER
50 [default: 0]
51 --cache-purge-level INTEGER RANGE
52 [default: 1]
53 --create-patch [default: False]
54 --exclude-paths TEXT
55 --exclude-regex-paths TEXT
56 --math-epsilon DECIMAL
57 --get-deep-distance [default: False]
58 --group-by TEXT
59 --ignore-order [default: False]
60 --ignore-string-type-changes [default: False]
61 --ignore-numeric-type-changes [default: False]
62 --ignore-type-subclasses [default: False]
63 --ignore-string-case [default: False]
64 --ignore-nan-inequality [default: False]
65 --include-private-variables [default: False]
66 --log-frequency-in-sec INTEGER [default: 0]
67 --max-passes INTEGER [default: 10000000]
68 --max_diffs INTEGER
69 --number-format-notation [f|e] [default: f]
70 --progress-logger [info|error] [default: info]
71 --report-repetition [default: False]
72 --significant-digits INTEGER
73 --truncate-datetime [second|minute|hour|day]
74 --verbose-level INTEGER RANGE [default: 1]
75 --view [-|colored|colored_compact]
76 [default: -]
77 Format for displaying differences.
78 --help Show this message and exit.
79
80
81 Example usage:
82
83 Let's imagine we have t1.csv and t2.csv:
84
85 .. csv-table:: t1.csv
86 :file: ../tests/fixtures/t1.csv
87 :header-rows: 1
88
89
90 .. csv-table:: t2.csv
91 :file: ../tests/fixtures/t2.csv
92 :header-rows: 1
93
94 We can run:
95
96 .. code-block:: bash
97
98 $ deep diff t1.csv t2.csv --ignore-order
99 {'values_changed': {"root[2]['zip']": {'new_value': 90002, 'old_value': 90001}}}
100
101 As you can see here the path to the item that is being changed is `root[2]['zip']` which is ok but
102 what if we assume last names are unique and group by last_name?
103
104 .. code-block:: bash
105
106 $ deep diff t1.csv t2.csv --ignore-order --group-by last_name
107 { 'values_changed': { "root['Molotov']['zip']": { 'new_value': 90002,
108 'old_value': 90001}}}
109
110 The path is perhaps more readable now: `root['Molotov']['zip']`. It is more clear that the zip code of Molotov has changed.
111
112 .. Note::
113 The parameters in the deep diff commandline are a subset of those in :ref:`deepdiff_label` 's Python API.
114
115 To output in a specific format, for example the colored compact view (see :doc:`colored_view` for output details):
116
117 .. code-block:: bash
118
119 $ deep diff t1.json t2.json --view colored_compact
120
121
122 .. _deep_grep_command:
123
124 deep grep command
125 -----------------
126
127 Run
128
129 .. code:: bash
130
131 $ deep grep
132
133 to get the options:
134
135 .. code-block:: bash
136
137 $ deep grep --help
138 Usage: deep grep [OPTIONS] ITEM PATH
139
140 Deep Grep Commandline
141
142 Grep through the contents of a file and find the path to the item.
143 It can read csv, tsv, json, yaml, and toml files.
144
145 Options:
146 -i, --ignore-case [default: False]
147 --exact-match [default: False]
148 --exclude-paths TEXT
149 --exclude-regex-paths TEXT
150 --verbose-level INTEGER RANGE [default: 1]
151 --help Show this message and exit.
152
153
154 .. csv-table:: t1.csv
155 :file: ../tests/fixtures/t1.csv
156 :header-rows: 1
157
158 .. code-block:: bash
159
160 $ deep grep --ignore-case james t1.csv
161 {'matched_values': ["root[2]['first_name']"]}
162
163
164 .. _deep_extract_command:
165
166 deep extract command
167 --------------------
168
169 Run
170
171 .. code:: bash
172
173 $ deep extract
174
175 to get the options:
176
177 .. code-block:: bash
178
179 $ deep extract --help
180 Usage: deep extract [OPTIONS] PATH_INSIDE PATH
181
182 Deep Extract Commandline
183
184 Extract an item from a file based on the path that is passed. It can read
185 csv, tsv, json, yaml, and toml files.
186
187 Options:
188 --help Show this message and exit.
189
190 .. csv-table:: t1.csv
191 :file: ../tests/fixtures/t1.csv
192 :header-rows: 1
193
194 .. code-block:: bash
195
196 $ deep extract "root[2]['first_name']" t1.csv
197 'James'
198
199
200 .. _deep_patch_command:
201
202 deep patch command
203 ------------------
204
205 Run
206
207 .. code:: bash
208
209 $ deep patch --help
210
211 to get the options:
212
213 .. code-block:: text
214
215 $ deep patch --help
216 Usage: deep patch [OPTIONS] PATH DELTA_PATH
217
218 Deep Patch Commandline
219
220 Patches a file based on the information in a delta file. The delta file
221 can be created by the deep diff command and passing the --create-patch
222 argument.
223
224 Deep Patch is similar to Linux's patch command. The difference is that it
225 is made for patching data. It can read csv, tsv, json, yaml, and toml
226 files.
227
228 Options:
229 -b, --backup [default: False]
230 --raise-errors [default: False]
231 --help Show this message and exit.
232
233 Imagine if we have the following files:
234
235
236 .. csv-table:: t1.csv
237 :file: ../tests/fixtures/t1.csv
238 :header-rows: 1
239
240 .. csv-table:: t2.csv
241 :file: ../tests/fixtures/t2.csv
242 :header-rows: 1
243
244
245 First we need to create a "delta" file which represents the difference between the 2 files.
246
247 .. code-block:: bash
248
249 $ deep diff t1.csv t2.csv --ignore-order
250 {'values_changed': {"root[2]['zip']": {'new_value': 90002, 'old_value': 90001}}}
251
252 We create the delta by using the deep diff command and passing the `--create-patch` argument.
253 However since we are using `--ignore-order`, `deep diff` will ask us to also use `--report-repetition`:
254
255 .. code-block:: bash
256
257 deep diff t1.csv t2.csv --ignore-order --report-repetition --create-patch
258 =}values_changed}root[2]['zip']} new_valueJ_sss.%
259
260 Note that the delta is not human readable. It is meant for us to pass it into a file:
261
262 .. code-block:: bash
263
264 deep diff t1.csv t2.csv --ignore-order --report-repetition --create-patch > patch1.pickle
265
266 Now this delta file is ready to be applied by the `deep patch` command to any json, csv, toml or yaml file!
267 It is expecting the structure of the file to be similar to the one in the csv file though.
268
269 Let's look at this yaml file:
270
271 `another.yaml`
272
273 .. code-block:: yaml
274
275 ---
276 -
277 first_name: Joe
278 last_name: Nobody
279 zip: 90011
280 -
281 first_name: Jack
282 last_name: Doit
283 zip: 22222
284 -
285 first_name: Sara
286 last_name: Stanley
287 zip: 11111
288
289 All that our delta knows is that `root[2]['zip']` has changed to `90002`.
290
291 Let's apply the delta:
292
293 .. code-block:: bash
294
295 deep patch --backup another.yaml patch1.pickle --raise-errors
296
297 And looking at the `another.yaml` file, the zip code is indeed updated!
298
299 .. code-block:: yaml
300
301 - first_name: Joe
302 last_name: Nobody
303 zip: 90011
304 - first_name: Jack
305 last_name: Doit
306 zip: 22222
307 - first_name: Sara
308 last_name: Stanley
309 zip: 90002
310
311 As you can see the formatting of the yaml file is changed.
312 This is due to the fact that DeepDiff loads the file into a Python dictionary, modifies it and then writes it back to disk.
313 During this operation, the file loses its original formatting.
314
315 .. note::
316 The deep patch command only provides a subset of what DeepDiff's :ref:`delta_label`'s Python API provides.
317 The deep patch command is minimalistic and is designed to have a similar interface to Linux's patch command
318 rather than DeepDiff's :ref:`delta_label`.
319
320 Back to :doc:`/index`