File: commandline.rst

package info (click to toggle)
deepdiff 8.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: python: 14,702; makefile: 164; sh: 9
file content (311 lines) | stat: -rw-r--r-- 8,030 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
:doc:`/index`

Command Line
============

`New in DeepDiff 5.2.0`

DeepDiff provides commandline interface to a subset of functionality that it provides through its Python API.

The commands are:

- :ref:`deep_diff_command`
- :ref:`deep_grep_command`
- :ref:`deep_extract_command`
- :ref:`deep_patch_command`


.. _deep_diff_command:

deep diff command
-----------------

Run 

.. code:: bash

  $ deep diff

to get the options:

.. code-block:: bash

    $ deep diff --help
    Usage: deep diff [OPTIONS] T1 T2

      Deep Diff Commandline

      Deep Difference of content in files.
      It can read csv, tsv, json, yaml, and toml files.

      T1 and T2 are the path to the files to be compared with each other.

    Options:
      --cutoff-distance-for-pairs FLOAT
                                      [default: 0.3]
      --cutoff-intersection-for-pairs FLOAT
                                      [default: 0.7]
      --cache-size INTEGER            [default: 0]
      --cache-tuning-sample-size INTEGER
                                      [default: 0]
      --cache-purge-level INTEGER RANGE
                                      [default: 1]
      --create-patch                  [default: False]
      --exclude-paths TEXT
      --exclude-regex-paths TEXT
      --math-epsilon DECIMAL
      --get-deep-distance             [default: False]
      --group-by TEXT
      --ignore-order                  [default: False]
      --ignore-string-type-changes    [default: False]
      --ignore-numeric-type-changes   [default: False]
      --ignore-type-subclasses        [default: False]
      --ignore-string-case            [default: False]
      --ignore-nan-inequality         [default: False]
      --include-private-variables     [default: False]
      --log-frequency-in-sec INTEGER  [default: 0]
      --max-passes INTEGER            [default: 10000000]
      --max_diffs INTEGER
      --number-format-notation [f|e]  [default: f]
      --progress-logger [info|error]  [default: info]
      --report-repetition             [default: False]
      --significant-digits INTEGER
      --truncate-datetime [second|minute|hour|day]
      --verbose-level INTEGER RANGE   [default: 1]
      --help                          Show this message and exit.


Example usage:

Let's imagine we have t1.csv and t2.csv:

.. csv-table:: t1.csv
   :file: ../tests/fixtures/t1.csv
   :header-rows: 1


.. csv-table:: t2.csv
   :file: ../tests/fixtures/t2.csv
   :header-rows: 1

We can run:

.. code-block:: bash

    $ deep diff t1.csv t2.csv --ignore-order
    {'values_changed': {"root[2]['zip']": {'new_value': 90002, 'old_value': 90001}}}

As you can see here the path to the item that is being changed is `root[2]['zip']` which is ok but
what if we assume last names are unique and group by last_name?

.. code-block:: bash

    $ deep diff t1.csv t2.csv --ignore-order --group-by last_name
    { 'values_changed': { "root['Molotov']['zip']": { 'new_value': 90002,
                                                      'old_value': 90001}}}

The path is perhaps more readable now: `root['Molotov']['zip']`. It is more clear that the zip code of Molotov has changed.

.. Note::
    The parameters in the deep diff commandline are a subset of those in :ref:`deepdiff_module_label` 's Python API.


.. _deep_grep_command:

deep grep command
-----------------

Run 

.. code:: bash

  $ deep grep

to get the options:

.. code-block:: bash

    $ deep grep --help
    Usage: deep grep [OPTIONS] ITEM PATH

      Deep Grep Commandline

      Grep through the contents of a file and find the path to the item.
      It can read csv, tsv, json, yaml, and toml files.

    Options:
      -i, --ignore-case              [default: False]
      --exact-match                  [default: False]
      --exclude-paths TEXT
      --exclude-regex-paths TEXT
      --verbose-level INTEGER RANGE  [default: 1]
      --help                         Show this message and exit.


.. csv-table:: t1.csv
   :file: ../tests/fixtures/t1.csv
   :header-rows: 1

.. code-block:: bash

    $ deep grep --ignore-case james t1.csv
    {'matched_values': ["root[2]['first_name']"]}


.. _deep_extract_command:

deep extract command
--------------------

Run

.. code:: bash

  $ deep extract

to get the options:

.. code-block:: bash

    $ deep extract --help
    Usage: deep extract [OPTIONS] PATH_INSIDE PATH

      Deep Extract Commandline

      Extract an item from a file based on the path that is passed. It can read
      csv, tsv, json, yaml, and toml files.

    Options:
      --help  Show this message and exit.

.. csv-table:: t1.csv
   :file: ../tests/fixtures/t1.csv
   :header-rows: 1

.. code-block:: bash

    $ deep extract "root[2]['first_name']" t1.csv
    'James'


.. _deep_patch_command:

deep patch command
------------------

Run

.. code:: bash

  $ deep patch --help

to get the options:

.. code-block:: bash

    $ deep patch --help
    Usage: deep patch [OPTIONS] PATH DELTA_PATH

      Deep Patch Commandline

      Patches a file based on the information in a delta file. The delta file
      can be created by the deep diff command and passing the --create-patch
      argument.

      Deep Patch is similar to Linux's patch command. The difference is that it
      is made for patching data. It can read csv, tsv, json, yaml, and toml
      files.

    Options:
      -b, --backup    [default: False]
      --raise-errors  [default: False]
      --help          Show this message and exit.

Imagine if we have the following files:


.. csv-table:: t1.csv
   :file: ../tests/fixtures/t1.csv
   :header-rows: 1

.. csv-table:: t2.csv
   :file: ../tests/fixtures/t1.csv
   :header-rows: 1


First we need to create a "delta" file which represents the difference between the 2 files.

.. code-block:: bash

    $ deep diff t1.csv t2.csv --ignore-order
    {'values_changed': {"root[2]['zip']": {'new_value': 90002, 'old_value': 90001}}}

We create the delta by using the deep diff command and passing the `--create-patch` argument.
However since we are using `--ignore-order`, `deep diff` will ask us to also use `--report-repetition`:

.. code-block:: bash

    deep diff t1.csv t2.csv --ignore-order --report-repetition --create-patch
    =}values_changed}root[2]['zip']}    new_valueJ_sss.% 

Note that the delta is not human readable. It is meant for us to pass it into a file:

.. code-block:: bash

    deep diff t1.csv t2.csv --ignore-order --report-repetition --create-patch > patch1.pickle

Now this delta file is ready to be applied by the `deep patch` command to any json, csv, toml or yaml file!
It is expecting the structure of the file to be similar to the one in the csv file though.

Let's look at this yaml file:

`another.yaml`

.. code-block:: yaml

    ---
    -
        first_name: Joe
        last_name: Nobody
        zip: 90011
    -
        first_name: Jack
        last_name: Doit
        zip: 22222
    -
        first_name: Sara
        last_name: Stanley
        zip: 11111

All that our delta knows is that `root[2]['zip']` has changed to `90002`.

Let's apply the delta:

.. code-block:: bash

    deep patch --backup another.yaml patch1.pickle --raise-errors

And looking at the `another.yaml` file, the zip code is indeed updated!

.. code-block:: yaml

    - first_name: Joe
      last_name: Nobody
      zip: 90011
    - first_name: Jack
      last_name: Doit
      zip: 22222
    - first_name: Sara
      last_name: Stanley
      zip: 90002

As you can see the formatting of the yaml file is changed.
This is due to the fact that DeepDiff loads the file into a Python dictionary, modifies it and then writes it back to disk.
During this operation, the file loses its original formatting.

.. note::
    The deep patch command only provides a subset of what DeepDiff's :ref:`delta_label`'s Python API provides.
    The deep patch command is minimalistic and is designed to have a similar interface to Linux's patch command
    rather than DeepDiff's :ref:`delta_label`.

Back to :doc:`/index`