Skip to content

Commit

Permalink
diff: duplicate isinstance test removal
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Kuncar <[email protected]>
  • Loading branch information
jirikuncar committed Nov 3, 2014
1 parent 81c6dfd commit b9beedb
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions dictdiffer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def diff(first, second, node=None, ignore=None):
else:
dotted_node = list(node)

differ = False

if isinstance(first, dict) and isinstance(second, dict):
# dictionaries are not hashable, we can't use sets
def check(key):
Expand All @@ -60,6 +62,8 @@ def check(key):
addition = [k for k in second if k not in first and check(k)]
deletion = [k for k in first if k not in second and check(k)]

differ = True

elif isinstance(first, list) and isinstance(second, list):
len_first = len(first)
len_second = len(second)
Expand All @@ -68,12 +72,13 @@ def check(key):
addition = list(range(min(len_first, len_second), len_second))
deletion = list(reversed(range(min(len_first, len_second), len_first)))

def diff_dict_list():
"""Compare if object is a dictionary.
differ = True

Call again the parent function as recursive if dictionary have child
objects. Yields `add` and `remove` flags.
"""
if differ:
# Compare if object is a dictionary.
#
# Call again the parent function as recursive if dictionary have child
# objects. Yields `add` and `remove` flags.
for key in intersection:
# if type is not changed, callees again diff function to compare.
# otherwise, the change will be handled as `change` flag.
Expand All @@ -98,15 +103,11 @@ def diff_dict_list():
# and values.
(key, first[key]) for key in deletion]

def diff_otherwise():
"""Compare string and integer types and yield `change` flag."""
else:
# Compare string and integer types and yield `change` flag.
if first != second:
yield CHANGE, dotted_node, (first, second)

if isinstance(second, type(first)) and isinstance(first, (dict, list)):
return diff_dict_list()
return diff_otherwise()


def patch(diff_result, destination):
"""Patch the diff result to the old dictionary."""
Expand Down

1 comment on commit b9beedb

@boxed
Copy link

@boxed boxed commented on b9beedb Nov 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a lot better! The "compare string and integer types" is a bit of a lie though, since it'll compare everything else, like sets, floats, whatever :P

Please sign in to comment.