Skip to content

Commit

Permalink
cleanup, docs, default method names corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Dec 25, 2018
1 parent d3b4be2 commit cc6794c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
56 changes: 30 additions & 26 deletions nested_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ class Differ(object):
`U` represent unchanged item.
Diff metadata alternates with actual data; simple types specified as is,
lists and dicts contain subdiffs for their items with native for such types
addressing: indexes for lists and keys for dictionaries. Each status type,
except `D` and `I`, may be (optionally) omitted during diff computation.
dicts, lists and tuples contain subdiffs for their items with native for
such types addressing: indexes for lists and tuples and keys for
dictionaries. Each status type, except `D` and `I`, may be (optionally)
omitted during diff computation.
Example:
Expand Down Expand Up @@ -79,16 +80,17 @@ class Differ(object):
| +- changes somewhere deeply inside
+- diff is always a dict
Dicts and lists traversed recursively, all other types compared by values.
Dicts, lists and tuples traversed recursively, all other types compared by
values.
"""
def __init__(self, A=True, N=True, O=True, R=True, U=True, trimR=False):
"""
Construct Differ.
Optional arguments:
`A`, `N`, `O`, `R`, `U` are toggles for according diff ops and all set
to True by default.
`A`, `N`, `O`, `R`, `U` are toggles for according diff ops and all
enabled (`True`) by default.
`trimR` when True drops (replaces by `None`) removed data from diff,
default is False.
Expand Down Expand Up @@ -126,7 +128,21 @@ def diff(self, a, b):
if isinstance(a, tuple) and isinstance(a, type(b)):
return self.diff_tuples(a, b)

return self.get_default_diff(a, b)
return self.diff__default(a, b)

def diff__default(self, a, b):
"""
Return default diff.
"""
ret = {}

if self.op_n:
ret['N'] = b
if self.op_o:
ret['O'] = a

return ret

def diff_dicts(self, a, b):
"""
Expand Down Expand Up @@ -159,7 +175,7 @@ def diff_dicts(self, a, b):
if self.op_r:
ret['D'][key] = {'R': None if self.op_trim_r else a[key]}

elif key in b: # added
else: # added
if self.op_a:
ret['D'][key] = {'A': b[key]}

Expand Down Expand Up @@ -246,28 +262,13 @@ def diff_tuples(self, a, b):
>>>
"""

ret = self.diff_lists(a, b)

if 'D' in ret:
ret['D'] = tuple(ret['D'])

return ret

def get_default_diff(self, a, b):
"""
Return default diff.
"""
ret = {}

if self.op_n:
ret['N'] = b
if self.op_o:
ret['O'] = a

return ret


class Patcher(object):
"""
Expand All @@ -278,6 +279,9 @@ def patch(self, target, ndiff):
"""
Return patched object.
This method is a dispatcher and calls `patch_dict` for dicts,
`patch_list` for lists and so forth.
:param target: Object to patch.
:param diff: Nested diff.
Expand All @@ -292,15 +296,15 @@ def patch(self, target, ndiff):
if isinstance(ndiff['D'], tuple):
return self.patch_tuple(target, ndiff)

return self.patch_default(target, ndiff)
return self.patch__default(target, ndiff)

elif 'N' in ndiff:
return ndiff['N']

else:
return target

def patch_default(self, target, ndiff):
def patch__default(self, target, ndiff):
"""
Patch containers without dedicated methods.
Expand Down Expand Up @@ -373,7 +377,7 @@ def diff(a, b, **kwargs):
:param a: First object to diff.
:param b: Second object to diff.
See Differ class for keywords options.
See Differ class for kwargs description.
"""
return Differ(**kwargs).diff(a, b)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_differ_subclassing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class CustomDiffer(Differ):
"""
Diff floats using defined precision
"""
def get_default_diff(self, a, b):
def diff__default(self, a, b):
if isinstance(a, float) and isinstance(a, type(b)):
if round(a, 1) == round(b, 1):
return {'U': a} if self.op_u else {}

return super(CustomDiffer, self).get_default_diff(a, b)
return super(CustomDiffer, self).diff__default(a, b)

differ = CustomDiffer(U=False)

Expand Down

0 comments on commit cc6794c

Please sign in to comment.