Deep compare two objects and produces a diffData to transform object1 to object2
Project description
diff-patch
Introduction
diff-patch is a library that exposes two functions, diff
and patch
.
The library does not have any dependencies.
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
).
diffData
is an object that can be used to apply the diff to obj1
to get an object that is the same as obj2
.
The patch
function takes an obj
and a diffData
object and applies the diff to the object, returning a new object.
The diff
and patch
functions are implemented in the following languages:
- TypeScript
- Python3
The diffData
object can be used interchangeably between the languages.
This makes this library very useful for networked applications, where the client and server are written in different languages.
diffData
can be serialized to JSON and deserialized back to an object. It aims to be as small as possible, and is designed to be used in a networked environment.
More languages will be added in the future, contributions are welcome.
Usage
To install the Python3 version of diff-patch, run:
pip install fsoft-diff-patch
Then, in your Python code:
from fsoft_diff_patch import diff, patch
obj1 = {
'a': 1,
'b': 2,
'c': {
'd': 3,
'e': 4,
},
}
obj2 = {
'a': 1,
'b': 2,
'c': {
'd': 3,
'e': 5, # changed
},
}
# create the diff data
diff_data = diff(obj1, obj2)
# show the diff data for debugging
print("diff_data: ", diff_data)
# apply the diff data to obj1 to get an object that is the same as obj2
obj3 = patch(obj1, diff_data)
# show the result for debugging
print("obj3: ", obj3)
Hint
The diff
function can be used to see if two objects are the same, by comparing the result of diff
to an empty object.
from diff_patch import diff
obj1 = {
'a': 1,
'b': 2,
'c': {
'd': 3,
'e': 4,
},
}
obj2 = {
'a': 1,
'b': 2,
'c': {
'd': 3,
'e': 4,
},
}
diff_data = diff(obj1, obj2)
# if the objects are the same, diff_data will be an empty object
if not diff_data:
print("The objects are the same")
Contributors
This library was created by Fabio Rotondo.
New language implementations are more than welcome.
Please open an issue or a pull request if you want to contribute.
Collaborators:
The official repository for this library is here.
License
This library is licensed under the MIT License.
See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fsoft_diff_patch-0.1.1.tar.gz
.
File metadata
- Download URL: fsoft_diff_patch-0.1.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b14b023cf6af5908c0b2153c02faa35dbf155b380e90ae01e2d332179379b649 |
|
MD5 | b1edec448c4d106cf003aa549106cbff |
|
BLAKE2b-256 | 8faafb0099468975f80e63917edb400a15b0980ba520a85637816ffcb30dc1af |
File details
Details for the file fsoft_diff_patch-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: fsoft_diff_patch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ab17a0dc6d2041022586a9192ee95cdbe2c4ed5b6d0d38fe2fabcf8868b8895 |
|
MD5 | db1ae37a6db1dbd83b24e74abd482d9c |
|
BLAKE2b-256 | 6f713b298a8576d2802e47293e9a7ee718872e35dd798dd15c751f332b888d4b |