Skip to content

Commit d572a7e

Browse files
committed
more examples; cosmetics
1 parent 44b7e76 commit d572a7e

File tree

2 files changed

+124
-36
lines changed

2 files changed

+124
-36
lines changed

HOWTO.md

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,93 @@
11
# Nested-Diff.py HowTo
22

3+
## How to get shortest possible patch
4+
5+
```py
6+
>>> from nested_diff import diff
7+
>>>
8+
>>> a = {'one': 1, 'two': 2, 'three': 'threeeeeeeee'}
9+
>>> b = {'one': 1, 'two': 42}
10+
>>>
11+
>>> diff(a, b, O=False, U=False, trimR=True)
12+
{'D': {'three': {'R': None}, 'two': {'N': 42}}}
13+
>>>
14+
```
15+
16+
## How to get unchanged items only
17+
18+
Diff calculates following changes by default:
19+
20+
* A: added items.
21+
* N: new values for changed items.
22+
* O: old values for changed items.
23+
* R: removed items.
24+
* U: unchanged items.
25+
26+
Thus disabling all except `U` diff will contain unchanged items only:
27+
28+
```py
29+
>>> from nested_diff import Differ
30+
>>>
31+
>>> a = ['one', 'two', 'three']
32+
>>> b = ['ONE', 'two']
33+
>>>
34+
>>> differ = Differ(A=False, N=False, O=False, R=False, U=True)
35+
>>>
36+
>>> is_equal, diff = differ.diff(a, b)
37+
>>> is_equal
38+
False
39+
>>> diff
40+
{'D': [{'U': 'two', 'I': 1}]}
41+
>>>
42+
```
43+
44+
## How to enable unified diffs for text values
45+
46+
```py
47+
>>> from nested_diff import diff, handlers
48+
>>> from nested_diff.formatters import TextFormatter
49+
>>>
50+
>>> a = 'one\ntwo\nthree'
51+
>>> b = 'one\nthree'
52+
>>>
53+
>>> d = diff(a, b, U=False, extra_handlers=[handlers.TextHandler(context=3)])
54+
>>> d
55+
{'D': [{'I': [0, 3, 0, 2]}, {'U': 'one'}, {'R': 'two'}, {'U': 'three'}], 'E': 5}
56+
>>>
57+
>>> print(TextFormatter().format(d))
58+
# <str>
59+
@@ -1,3 +1,2 @@
60+
one
61+
- two
62+
three
63+
<BLANKLINE>
64+
>>>
65+
```
66+
67+
## How to render diff to HTML
68+
69+
```py
70+
>>> from nested_diff import diff
71+
>>> from nested_diff.formatters import HtmlFormatter
72+
>>>
73+
>>> a = ['a', 'b', 'c']
74+
>>> b = ['A', 'b']
75+
>>>
76+
>>> formatter = HtmlFormatter()
77+
>>> diff_ = diff(a, b)
78+
>>>
79+
>>> html_header = formatter.get_page_header(lang='en', title='Nested diff')
80+
>>> html_body = formatter.format(diff_)
81+
>>> html_footer = formatter.get_page_footer()
82+
>>>
83+
```
84+
385
## How to diff objects with types unsupported by nested\_diff
486

5-
Custom type handlers may be set to support any desired types, have a look at
6-
`Differ.set_handler` method and [handlers.py](nested_diff/handlers.py).
87+
External handlers may be set to support any desired types.
88+
Have a look at `Differ.set_handler` method for details and builtin set of
89+
[nested\_diff.handlers](https://github.com/mr-mixas/Nested-Diff.py/tree/master/nested_diff/handlers.py)
90+
for examples.
791

892
## How to ignore some differences during diff
993

@@ -38,6 +122,19 @@ assert differ.diff(a, b) == (False, {'D': [{'I': 2, 'N': 0.2, 'O': 0.1}]})
38122
>>>
39123
```
40124

125+
## How to treat NaNs as equals
126+
127+
```py
128+
>>> from nested_diff import Differ, handlers
129+
>>>
130+
>>> differ = Differ()
131+
>>> differ.set_handler(handlers.FloatHandler(nans_equal=True))
132+
>>>
133+
>>> differ.diff(float('nan'), float('nan'))
134+
(True, {'U': nan})
135+
>>>
136+
```
137+
41138
## How to use nested\_diff tool with git
42139

43140
Ensure `nested_diff` command available, otherwise install it with `pip`:

README.md

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
Recursive diff and patch for nested structures.
44

5-
[![Tests Status](https://github.com/mr-mixas/Nested-Diff.py/actions/workflows/tests.yml/badge.svg)](https://github.com/mr-mixas/Nested-Diff.py/actions?query=branch%3Amaster)
6-
[![Coverage Status](https://coveralls.io/repos/github/mr-mixas/Nested-Diff.py/badge.svg)](https://coveralls.io/github/mr-mixas/Nested-Diff.py)
5+
[![PyPi](https://img.shields.io/pypi/v/nested-diff.svg)](https://pypi.python.org/pypi/nested-diff)
6+
[![Tests](https://github.com/mr-mixas/Nested-Diff.py/actions/workflows/tests.yml/badge.svg)](https://github.com/mr-mixas/Nested-Diff.py/actions?query=branch%3Amaster)
7+
[![Coverage](https://coveralls.io/repos/github/mr-mixas/Nested-Diff.py/badge.svg)](https://coveralls.io/github/mr-mixas/Nested-Diff.py)
78
[![Supported Python versions](https://img.shields.io/pypi/pyversions/nested_diff.svg)](https://pypi.org/project/nested_diff/)
89
[![License](https://img.shields.io/pypi/l/nested_diff.svg)](https://pypi.org/project/nested_diff/)
910

1011
## Main features
1112

12-
* Machine-readable diff structure.
13-
* Human-friendly diff visualization, collapsible html diffs.
14-
* All operation tags are optional and may be disabled.
15-
* Extensibility.
13+
* Machine readable diff structure.
14+
* Human friendly diff visualization, collapsible html diffs.
15+
* All ops (added/removed/changed/unchanged) are optional and may be disabled.
16+
* Any data types support may be added by external handlers.
1617

1718
**[See Live Demo!](https://nesteddiff.pythonanywhere.com/)**
1819

@@ -48,52 +49,42 @@ nested_patch a.json patch.json
4849

4950
```py
5051
>>> from nested_diff import diff, patch
52+
>>> from nested_diff.formatters import TextFormatter
5153
>>>
5254
>>> a = {'one': 1, 'two': 2, 'three': 3}
5355
>>> b = {'one': 1, 'two': 42}
5456
>>>
55-
>>> diff(a, b)
57+
>>>
58+
>>> full_diff = diff(a, b)
59+
>>> full_diff
5660
{'D': {'three': {'R': 3}, 'two': {'N': 42, 'O': 2}, 'one': {'U': 1}}}
5761
>>>
58-
>>> diff(a, b, O=False, U=False)
62+
>>> short_diff = diff(a, b, O=False, U=False) # omit old and unchanged items
63+
>>> short_diff
5964
{'D': {'three': {'R': 3}, 'two': {'N': 42}}}
6065
>>>
6166
>>>
62-
>>> c = [0,1,2,3]
63-
>>> d = [ 1,2,4,5]
64-
>>>
65-
>>> c = patch(c, diff(c, d))
66-
>>> assert c == d
67-
>>>
68-
```
69-
70-
### Formatting diffs
71-
72-
```py
73-
>>> from nested_diff import diff, handlers
74-
>>> from nested_diff.formatters import TextFormatter
67+
>>> a = patch(a, short_diff)
68+
>>> assert a == b
7569
>>>
76-
>>> a = {'one': 1, 'two': 'some\ntext\ninside'}
77-
>>> b = {'one': 0, 'two': 'some\ntext'}
7870
>>>
79-
>>> d = diff(a, b, U=False, extra_handlers=[handlers.TextHandler(context=3)])
80-
>>> print(TextFormatter().format(d))
71+
>>> human_readable = TextFormatter().format(full_diff)
72+
>>> print(human_readable)
8173
{'one'}
82-
- 1
83-
+ 0
74+
1
75+
- {'three'}
76+
- 3
8477
{'two'}
85-
# <str>
86-
@@ -1,3 +1,2 @@
87-
some
88-
text
89-
- inside
78+
- 2
79+
+ 42
9080
<BLANKLINE>
9181
>>>
9282
```
9383

94-
For more examples see [Live Demo](https://nesteddiff.pythonanywhere.com/),
84+
HTML and ANSI colored terminal formatters also available out of the box.
85+
See [Live Demo](https://nesteddiff.pythonanywhere.com/),
9586
[HOWTO](https://github.com/mr-mixas/Nested-Diff.py/blob/master/HOWTO.md) and
96-
[tests](https://github.com/mr-mixas/Nested-Diff.py/tree/master/tests).
87+
[nested\_diff.formatters](https://github.com/mr-mixas/Nested-Diff.py/tree/master/nested_diff/formatters.py).
9788

9889
## Diff structure
9990

0 commit comments

Comments
 (0)