|
4 | 4 |
|
5 | 5 | diff-patch is a library that exposes two functions, `diff` and `patch`.
|
6 | 6 |
|
7 |
| -The `diff` function takes two objects and deep compares them, returning an object that represents the difference between the two (the `diffData`). |
| 7 | +The library does not have any dependencies. |
8 | 8 |
|
9 |
| -The `patch` function takes an object and a diff object and applies the diff to the object, returning a new object. |
| 9 | +The `diff` function takes two objects (`obj1` and `obj2`) and deep compares them, returning an object that represents the difference between the two (the `diffData`). |
| 10 | +`diffData` is an object that can be used to apply the diff to `obj1` to get an object that is the same as `obj2`. |
| 11 | + |
| 12 | +The `patch` function takes an `obj` and a `diffData` object and applies the diff to the object, returning a new object. |
10 | 13 |
|
11 | 14 | The `diff` and `patch` functions are implemented in the following languages:
|
12 | 15 |
|
@@ -70,14 +73,14 @@ console.log( "obj3: ", obj3);
|
70 | 73 | To install the Python3 version of diff-patch, run:
|
71 | 74 |
|
72 | 75 | ```bash
|
73 |
| -pip install diff-patch |
| 76 | +pip install fsoft-diff-patch |
74 | 77 | ```
|
75 | 78 |
|
76 | 79 | Then, in your Python code:
|
77 | 80 |
|
78 | 81 | ```python
|
79 | 82 |
|
80 |
| -from diff_patch import diff, patch |
| 83 | +from fsoft_diff_patch import diff, patch |
81 | 84 |
|
82 | 85 | obj1 = {
|
83 | 86 | 'a': 1,
|
@@ -110,4 +113,35 @@ obj3 = patch(obj1, diff_data)
|
110 | 113 | print("obj3: ", obj3)
|
111 | 114 | ```
|
112 | 115 |
|
| 116 | +### Hint |
| 117 | + |
| 118 | +The `diff` function can be used to see if two objects are the same, by comparing the result of `diff` to an empty object. |
| 119 | + |
| 120 | +```python |
| 121 | +from diff_patch import diff |
| 122 | + |
| 123 | +obj1 = { |
| 124 | + 'a': 1, |
| 125 | + 'b': 2, |
| 126 | + 'c': { |
| 127 | + 'd': 3, |
| 128 | + 'e': 4, |
| 129 | + }, |
| 130 | +} |
| 131 | + |
| 132 | +obj2 = { |
| 133 | + 'a': 1, |
| 134 | + 'b': 2, |
| 135 | + 'c': { |
| 136 | + 'd': 3, |
| 137 | + 'e': 4, |
| 138 | + }, |
| 139 | +} |
| 140 | + |
| 141 | +diff_data = diff(obj1, obj2) |
| 142 | + |
| 143 | +# if the objects are the same, diff_data will be an empty object |
| 144 | +if not diff_data: |
| 145 | + print("The objects are the same") |
| 146 | +``` |
113 | 147 |
|
0 commit comments