Skip to content

Commit

Permalink
enhanced to support ignoring named keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dandroid88 committed Feb 3, 2014
1 parent c3b8ee8 commit d6b87b0
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
File renamed without changes.
30 changes: 15 additions & 15 deletions jsoncompare/jsoncompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _generate_pprint_json(value):
return json.dumps(value, sort_keys=True, indent=4)


def _is_dict_same(a, b):
def _is_dict_same(a, b, ignore_value_of_keys):
for key in a:
if not key in b:
return False, \
Expand All @@ -58,17 +58,17 @@ def _is_dict_same(a, b):
.format(key),
a,
b))

are_same_flag, stack = _are_same(a[key], b[key])
if not are_same_flag:
return False, \
stack.append(StackItem('Different values', a[key], b[key]))
if not key in ignore_value_of_keys:
are_same_flag, stack = _are_same(a[key], b[key], ignore_value_of_keys)
if not are_same_flag:
return False, \
stack.append(StackItem('Different values', a[key], b[key]))
return True, Stack()


def _is_list_same(a, b):
def _is_list_same(a, b, ignore_value_of_keys):
for i in xrange(len(a)):
are_same_flag, stack = _are_same(a[i], b[i])
are_same_flag, stack = _are_same(a[i], b[i], ignore_value_of_keys)
if not are_same_flag:
return False, \
stack.append(
Expand All @@ -91,7 +91,7 @@ def _bottom_up_sort(unsorted_json):
else:
return unsorted_json

def _are_same(a, b):
def _are_same(a, b, ignore_value_of_keys):
# Check for None
if a is None:
return a == b, Stack()
Expand Down Expand Up @@ -119,14 +119,14 @@ def _are_same(a, b):
b))

if isinstance(a, dict):
return _is_dict_same(a, b)
return _is_dict_same(a, b, ignore_value_of_keys)

if isinstance(a, list):
return _is_list_same(a, b)
return _is_list_same(a, b, ignore_value_of_keys)

return False, Stack().append(StackItem('Unhandled Type: {0}'.format(type(a)), a, b))

def are_same(original_a, original_b, ignore_list_order_recursively=False):
def are_same(original_a, original_b, ignore_list_order_recursively=False, ignore_value_of_keys=[]):
a = None
b = None
if ignore_list_order_recursively:
Expand All @@ -135,7 +135,7 @@ def are_same(original_a, original_b, ignore_list_order_recursively=False):
else:
a = original_a
b = original_b
return _are_same(a, b)
return _are_same(a, b, ignore_value_of_keys)

def json_are_same(a, b, ignore_list_order_recursively=False):
return are_same(json.loads(a), json.loads(b), ignore_list_order_recursively)
def json_are_same(a, b, ignore_list_order_recursively=False, ignore_value_of_keys=[]):
return are_same(json.loads(a), json.loads(b), ignore_list_order_recursively, ignore_value_of_keys)
Empty file added jsoncompare/test/__init__.py
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/usr/bin/env python

import unittest
import sys, os

path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if not path in sys.path:
sys.path.insert(1, path)
import jsoncompare

class TestJSONCompare(unittest.TestCase):
Expand Down Expand Up @@ -41,6 +46,17 @@ def test_list_of_hashes_unordered_fail(self):
]
self.assertFalse(jsoncompare.are_same(a, b)[0])

def test_list_of_hashes_ignore_key(self):
a = [
{"wtf1": "omg1"},
{"wtf2": "omg"}
]
b = [
{"wtf1": "omg1"},
{"wtf2": "omg3"}
]
self.assertTrue(jsoncompare.are_same(a, b, True, ["wtf2"])[0])

def test_hash_list_of_hashes_unordered(self):
a = {
"wtf": [
Expand All @@ -55,7 +71,6 @@ def test_hash_list_of_hashes_unordered(self):
]
}
self.assertTrue(jsoncompare.are_same(a, b, True)[0])
print jsoncompare.are_same(a, b, True)[1]

def test_hash_list_of_hashes_unordered_fail(self):
a = {
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def read_readme(fname):

setup(
name = 'jsoncompare',
version = '0.1.0',
version = '0.1.1',
description = 'Json comparison tool',
author = 'Daniel Myers',
author_email = '[email protected]',
url = 'https://github.com/dandroid88/jsoncompare',
packages = ['jsoncompare', 'jsoncompare.tests'],
packages = ['jsoncompare', 'jsoncompare.test'],
test_suite = "jsoncompare.test.test_jsoncompare",
keywords = 'json comparison compare order',
long_description = read_readme('README.md')
)

0 comments on commit d6b87b0

Please sign in to comment.