Skip to content

Commit

Permalink
ignore order in group enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
seperman committed Mar 26, 2019
1 parent 3141ceb commit 60230b6
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 27 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Tested on Python 3.4, 3.5, 3.6, 3.7, Pypy3

### Install from PyPi:

`pip install deepdiff`
`pip install deepdiff`

DeepDiff prefers to use Murmur3 for hashing. However you have to manually install Murmur3 by running:

`pip install mmh3`
`pip install mmh3`

Otherwise DeepDiff will be using SHA256 for hashing which is a cryptographic hash and is considerably slower.

Expand Down Expand Up @@ -157,8 +157,7 @@ Digits **after** the decimal point. Internally it uses "{:.Xf}".format(Your Numb
'new_value': 3.0,
'old_type': <class 'int'>,
'old_value': 3}}}
>>> ddiff = DeepDiff(t1, t2, ignore_type_in_groups=True)
>>> pprint(ddiff, indent=2)
>>> ddiff = DeepDiff(t1, t2, ignore_type_in_groups=[(int, float)])
{}
```

Expand Down Expand Up @@ -408,9 +407,11 @@ On MacOS Mojave some user experience difficulty when installing Murmur3.

The problem can be solved by running:

`xcode-select --install`
`xcode-select --install`

And then running

And then running `pip install mmh3`
`pip install mmh3`

# ChangeLog

Expand Down Expand Up @@ -448,6 +449,16 @@ And then running `pip install mmh3`
- v0-5-6: Adding slots support
- v0-5-5: Adding loop detection

# Releases

We use bump2version to bump and tag releases.

```bash
git checkout master && git pull
bump2version {patch|minor|major}
git push && git push --tags
```

# Contribute

1. Please make your PR against the dev branch
Expand Down
4 changes: 3 additions & 1 deletion deepdiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""This module offers the DeepDiff, DeepSearch, grep and DeepHash classes."""
# flake8: noqa
__version__ = '4.0.2'
import logging
import pkg_resources

if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s')

__version__ = pkg_resources.get_distribution("deepdiff").version

from .diff import DeepDiff
from .search import DeepSearch, grep
from .deephash import DeepHash
13 changes: 10 additions & 3 deletions deepdiff/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ def get_ignore_types_in_groups(self, ignore_type_in_groups,
ignore_numeric_type_changes):
if ignore_type_in_groups:
if isinstance(ignore_type_in_groups[0], type):
ignore_type_in_groups = [OrderedSet(ignore_type_in_groups)]
else:
ignore_type_in_groups = list(map(OrderedSet, ignore_type_in_groups))
ignore_type_in_groups = [ignore_type_in_groups]
else:
ignore_type_in_groups = []

result = []
for item_group in ignore_type_in_groups:
new_item_group = OrderedSet()
for item in item_group:
item = type(item) if item is None or not isinstance(item, type) else item
new_item_group.add(item)
result.append(new_item_group)
ignore_type_in_groups = result

if ignore_string_type_changes and self.strings not in ignore_type_in_groups:
ignore_type_in_groups.append(OrderedSet(self.strings))

Expand Down
6 changes: 4 additions & 2 deletions deepdiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self,
ignore_type_in_groups=None,
ignore_string_type_changes=False,
ignore_numeric_type_changes=False,
ignore_type_subclasses=False,
verbose_level=1,
view=TEXT_VIEW,
hasher=None,
Expand All @@ -64,8 +65,8 @@ def __init__(self,
"The following parameter(s) are not valid: %s\n"
"The valid parameters are ignore_order, report_repetition, significant_digits, "
"exclude_paths, exclude_types, exclude_regex_paths, ignore_type_in_groups, "
"ignore_string_type_changes, ignore_numeric_type_changes, verbose_level, view, "
"and hasher.") % ', '.join(kwargs.keys()))
"ignore_string_type_changes, ignore_numeric_type_changes, ignore_type_subclasses, "
"verbose_level, view, and hasher.") % ', '.join(kwargs.keys()))

self.ignore_order = ignore_order
self.ignore_type_in_groups = self.get_ignore_types_in_groups(
Expand All @@ -78,6 +79,7 @@ def __init__(self,
self.exclude_types_tuple = tuple(exclude_types) if exclude_types else None # we need tuple for checking isinstance
self.ignore_string_type_changes = ignore_string_type_changes
self.ignore_numeric_type_changes = ignore_numeric_type_changes
self.ignore_type_subclasses = ignore_type_subclasses
self.hashes = {}
self.hasher = hasher

Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --pdbcls=IPython.terminal.debugger:Pdb
8 changes: 5 additions & 3 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-r requirements.txt
pytest==4.0.1
pytest-cov==2.6.0
numpy==1.15.4
pytest-4.3.1
pytest-cov==2.6.1
numpy==1.16.2
mmh3==2.5.1
ipdb==0.11
bump2version==0.5.10
11 changes: 11 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ builtins = json
statistics = true
ignore = E202
exclude = ./data,./src,.svn,CVS,.bzr,.hg,.git,__pycache__

[bumpversion]
current_version = 0.4.2
commit = True
tag = True
tag_name = {new_version}

[bumpversion:file:setup.py]
[bumpversion:file:README.md]
[bumpversion:file:docs/index.rst]
[bumpversion:file:docs/conf.py]
14 changes: 2 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
import sys
from setuptools import setup

Expand All @@ -11,16 +10,7 @@
if os.environ.get('USER', '') == 'vagrant':
del os.link


VERSIONFILE = "deepdiff/__init__.py"
with open(VERSIONFILE, "r") as the_file:
verstrline = the_file.read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
verstr = mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))
version = '0.4.2'


def get_reqs(filename):
Expand All @@ -37,7 +27,7 @@ def get_reqs(filename):


setup(name='deepdiff',
version=verstr,
version=version,
description='Deep Difference and Search of any Python object/data.',
url='https://github.com/seperman/deepdiff',
download_url='https://github.com/seperman/deepdiff/tarball/master',
Expand Down
7 changes: 7 additions & 0 deletions tests/test_diff_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,13 @@ def test_ignore_type_in_groups_numbers_and_strings_when_ignore_order(self):
result = {'iterable_item_added': {'root[2]': 3.3}, 'iterable_item_removed': {'root[2]': 3}}
assert result == ddiff

def test_ignore_type_in_groups_none_and_objects(self):
t1 = [1, 2, 3, 'a', None]
t2 = [1.0, 2.0, 3.3, b'a', 'hello']
ddiff = DeepDiff(t1, t2, ignore_type_in_groups=[(1, 1.0), (None, str, bytes)])
result = {'values_changed': {'root[2]': {'new_value': 3.3, 'old_value': 3}}}
assert result == ddiff

def test_ignore_string_type_changes_when_dict_keys_merge_is_not_deterministic(self):
t1 = {'a': 10, b'a': 20}
t2 = {'a': 11, b'a': 22}
Expand Down

0 comments on commit 60230b6

Please sign in to comment.