Skip to content

Commit

Permalink
Merge pull request #176 from seperman/dev
Browse files Browse the repository at this point in the history
adding exclude_obj_callback
  • Loading branch information
seperman authored Mar 10, 2020
2 parents abd34f6 + 81b0af8 commit f9c0534
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DeepDiff v 4.2.0
# DeepDiff v 4.3.0

<!-- ![Downloads](https://img.shields.io/pypi/dm/deepdiff.svg?style=flat) -->
![Python Versions](https://img.shields.io/pypi/pyversions/deepdiff.svg?style=flat)
Expand Down Expand Up @@ -417,6 +417,7 @@ And then running

# ChangeLog

- v4-3-0: adding exclude_obj_callback
- v4-2-0: .json property is finally removed. Fix for Py3.10. Dropping support for EOL Python 3.4. Ignoring private keys when calculating hashes. For example __init__ is not a part of hash calculation anymore. Fix for #166 Problem with comparing lists, with an boolean as element.
- v4-0-9: Fixing the bug for hashing custom unhashable objects
- v4-0-8: Adding ignore_nan_inequality for float('nan')
Expand Down Expand Up @@ -507,3 +508,4 @@ Thank you!
- Juan Soler (Soleronline) for adding ignore_type_number
- mthaddon for adding timedelta diffing support
- Necrophagos for Hashing of the number 1 vs. True
- gaal-dev for adding exclude_obj_callback
2 changes: 1 addition & 1 deletion deepdiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This module offers the DeepDiff, DeepSearch, grep and DeepHash classes."""
# flake8: noqa
__version__ = '4.2.0'
__version__ = '4.3.0'
import logging

if __name__ == '__main__':
Expand Down
21 changes: 21 additions & 0 deletions deepdiff/deephash_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ exclude_paths: list, default = None
exclude_regex_paths: list, default = None
List of string regex paths or compiled regex paths objects to exclude from the report. If only one item, you can path it as a string instead of a list containing only one regex path.

exclude_obj_callback
function, default = None
A function that takes the object and its path and returns a Boolean. If True is returned, the object is excluded from the results, otherwise it is included.
This is to give the user a higher level of control than one can achieve via exclude_paths, exclude_regex_paths or other means.

hasher: function. default = DeepHash.murmur3_128bit
hasher is the hashing function. The default is DeepHash.murmur3_128bit.
But you can pass another hash function to it if you want.
Expand Down Expand Up @@ -286,6 +291,22 @@ ignore_string_case
>>> DeepHash('hello', ignore_string_case=True)['hello'] == DeepHash('heLLO', ignore_string_case=True)['heLLO']
True

exclude_obj_callback
function, default = None
A function that takes the object and its path and returns a Boolean. If True is returned, the object is excluded from the results, otherwise it is included.
This is to give the user a higher level of control than one can achieve via exclude_paths, exclude_regex_paths or other means.

>>> dic1 = {"x": 1, "y": 2, "z": 3}
>>> t1 = [dic1]
>>> t1_hash = DeepHash(t1, exclude_obj_callback=exclude_obj_callback)
>>>
>>> dic2 = {"z": 3}
>>> t2 = [dic2]
>>> t2_hash = DeepHash(t2, exclude_obj_callback=exclude_obj_callback)
>>>
>>> t1_hash[t1] == t2_hash[t2]
True

number_format_notation : string, default="f"
When numbers are converted to the string, you have the choices between "f" as fixed point and "e" as scientific notation:

Expand Down
1 change: 1 addition & 0 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ def __create_hashtable(self, t, level):
ignore_type_subclasses=self.ignore_type_subclasses,
ignore_string_case=self.ignore_string_case,
number_to_string_func=self.number_to_string,
exclude_obj_callback=self.exclude_obj_callback,
)
# import pytest; pytest.set_trace()
key = item
Expand Down
20 changes: 19 additions & 1 deletion deepdiff/diff_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ view: string, default = text
exclude_types: list, default = None
List of object types to exclude from the report.

exclude_obj_callback: function, default = None
A function that takes the object and its path and returns a Boolean. If True is returned, the object is excluded from the results, otherwise it is included.
This is to give the user a higher level of control than one can achieve via exclude_paths, exclude_regex_paths or other means.

ignore_string_type_changes: Boolean, default = False
Whether to ignore string type changes or not. For example b"Hello" vs. "Hello" are considered the same if ignore_string_type_changes is set to True.

Expand Down Expand Up @@ -497,6 +501,20 @@ ignore_nan_inequality
>>> DeepDiff(float('nan'), float('nan'), ignore_nan_inequality=True)
{}


exclude_obj_callback
function, default = None
A function that takes the object and its path and returns a Boolean. If True is returned, the object is excluded from the results, otherwise it is included.
This is to give the user a higher level of control than one can achieve via exclude_paths, exclude_regex_paths or other means.

>>> def exclude_obj_callback(obj, path):
... return True if "skip" in path or isinstance(obj, int) else False
...
>>> t1 = {"x": 10, "y": "b", "z": "c", "skip_1": 0}
>>> t2 = {"x": 12, "y": "b", "z": "c", "skip_2": 0}
>>> DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback)
{}

**Tree View**

Starting the version 3 You can chooe the view into the deepdiff results.
Expand Down Expand Up @@ -806,7 +824,7 @@ Example:
.. seealso::
Take a look at to_json() documentation in this page for more details.

If you want the original DeepDiff object to be serialized with all the bells and whistles, you can use the to_json_pickle() and to_json_pickle() in order to serialize and deserialize its results into json. Note that json_pickle is unsafe and json pickle dumps from untrusted sources should never be loaded.
If you want the original DeepDiff object to be serialized with all the bells and whistles, you can use the to_json_pickle() and from_json_pickle() in order to serialize and deserialize its results into json. Note that json_pickle is unsafe and json pickle dumps from untrusted sources should never be loaded.

Serialize and then deserialize back to deepdiff
>>> t1 = {1: 1, 2: 2, 3: 3}
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
# built documents.
#
# The short X.Y version.
version = '4.2.0'
version = '4.3.0'
# The full version, including alpha/beta/rc tags.
release = '4.2.0'
release = '4.3.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
5 changes: 3 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
contain the root `toctree` directive.
DeepDiff 4.2.0 documentation!
DeepDiff 4.3.0 documentation!
=============================

**DeepDiff: Deep Difference of dictionaries, iterables, strings and other objects. It will recursively look for all the changes.**
Expand Down Expand Up @@ -281,6 +281,7 @@ Indices and tables
Changelog
=========

- v4-3-0: adding exclude_obj_callback
- v4-2-0: .json property is finally removed. Fix for Py3.10. Dropping support for EOL Python 3.4. Ignoring private keys when calculating hashes. For example __init__ is not a part of hash calculation anymore. Fix for #166 Problem with comparing lists, with an boolean as element.
- v4-1-0: .json property is finally removed.
- v4-0-9: Fixing the bug for hashing custom unhashable objects
Expand Down Expand Up @@ -364,4 +365,4 @@ And thanks to the following people for their great contributions!
- Necrophagos for Hashing of the number 1 vs. True
- Hugo (hugovk) for fixes for Python 3.10 and dropping support for EOL Python 3.4
- Andrey Gavrilin (gaal-dev) for hashing classes.

- gaal-dev for adding exclude_obj_callback
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 4.2.0
current_version = 4.3.0
commit = True
tag = True
tag_name = {new_version}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
if os.environ.get('USER', '') == 'vagrant':
del os.link

version = '4.2.0'
version = '4.3.0'


def get_reqs(filename):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,8 @@ def test_skip_exclude_obj_callback(self):
def exclude_obj_callback(obj, path):
return True if "skip" in path or isinstance(obj, int) else False

t1 = {"x": "a", "y": "b", "z": "c", "skip_1": 0}
t2 = {"x": "a", "y": "b", "z": "c", "skip_2": 1}
t1 = {"x": 10, "y": "b", "z": "c", "skip_1": 0}
t2 = {"x": 12, "y": "b", "z": "c", "skip_2": 0}
ddiff = DeepDiff(t1, t2, exclude_obj_callback=exclude_obj_callback)
result = {}
assert result == ddiff
Expand Down
4 changes: 4 additions & 0 deletions tests/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ def exclude_obj_callback(obj, parent):
t1_hash = DeepHashPrep(t1, exclude_obj_callback=exclude_obj_callback)
assert t1_hash == {'y': 'str:y', 'z': 'str:z', 3: 'int:3',
get_id(dic1): 'dict:{str:z:int:3}', get_id(t1): 'list:dict:{str:z:int:3}'}
dic2 = {"z": 3}
t2 = [dic2]
t2_hash = DeepHashPrep(t2, exclude_obj_callback=exclude_obj_callback)
assert t1_hash[t1] == t2_hash[t2]

def test_string_case(self):
t1 = "Hello"
Expand Down

0 comments on commit f9c0534

Please sign in to comment.