##// END OF EJS Templates
dicthelpers.diff: compare against default for missing values...
Siddharth Agarwal -
r18894:ed46c2b9 default
parent child Browse files
Show More
@@ -1,55 +1,55 b''
1 1 # dicthelpers.py - helper routines for Python dicts
2 2 #
3 3 # Copyright 2013 Facebook
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 def diff(d1, d2, default=None):
9 9 '''Return all key-value pairs that are different between d1 and d2.
10 10
11 11 This includes keys that are present in one dict but not the other, and
12 12 keys whose values are different. The return value is a dict with values
13 13 being pairs of values from d1 and d2 respectively, and missing values
14 represented as default.'''
14 treated as default, so if a value is missing from one dict and the same as
15 default in the other, it will not be returned.'''
15 16 res = {}
16 17 if d1 is d2:
17 18 # same dict, so diff is empty
18 19 return res
19 20
20 21 for k1, v1 in d1.iteritems():
21 if k1 in d2:
22 v2 = d2[k1]
23 if v1 != v2:
24 res[k1] = (v1, v2)
25 else:
26 res[k1] = (v1, default)
22 v2 = d2.get(k1, default)
23 if v1 != v2:
24 res[k1] = (v1, v2)
27 25
28 26 for k2 in d2:
29 27 if k2 not in d1:
30 res[k2] = (default, d2[k2])
28 v2 = d2[k2]
29 if v2 != default:
30 res[k2] = (default, v2)
31 31
32 32 return res
33 33
34 34 def join(d1, d2, default=None):
35 35 '''Return all key-value pairs from both d1 and d2.
36 36
37 37 This is akin to an outer join in relational algebra. The return value is a
38 38 dict with values being pairs of values from d1 and d2 respectively, and
39 39 missing values represented as default.'''
40 40 res = {}
41 41
42 42 for k1, v1 in d1.iteritems():
43 43 if k1 in d2:
44 44 res[k1] = (v1, d2[k1])
45 45 else:
46 46 res[k1] = (v1, default)
47 47
48 48 if d1 is d2:
49 49 return res
50 50
51 51 for k2 in d2:
52 52 if k2 not in d1:
53 53 res[k2] = (default, d2[k2])
54 54
55 55 return res
@@ -1,53 +1,59 b''
1 1 from mercurial.dicthelpers import diff, join
2 2 import unittest
3 3 import silenttestrunner
4 4
5 5 class testdicthelpers(unittest.TestCase):
6 6 def test_dicthelpers(self):
7 7 # empty dicts
8 8 self.assertEqual(diff({}, {}), {})
9 9 self.assertEqual(join({}, {}), {})
10 10
11 11 d1 = {}
12 12 d1['a'] = 'foo'
13 13 d1['b'] = 'bar'
14 14 d1['c'] = 'baz'
15 15
16 16 # same identity
17 17 self.assertEqual(diff(d1, d1), {})
18 18 self.assertEqual(join(d1, d1), {'a': ('foo', 'foo'),
19 19 'b': ('bar', 'bar'),
20 20 'c': ('baz', 'baz')})
21 21
22 22 # vs empty
23 23 self.assertEqual(diff(d1, {}), {'a': ('foo', None),
24 24 'b': ('bar', None),
25 25 'c': ('baz', None)})
26 26 self.assertEqual(diff(d1, {}), {'a': ('foo', None),
27 27 'b': ('bar', None),
28 28 'c': ('baz', None)})
29 29
30 30 d2 = {}
31 31 d2['a'] = 'foo2'
32 32 d2['b'] = 'bar'
33 33 d2['d'] = 'quux'
34 34
35 35 self.assertEqual(diff(d1, d2), {'a': ('foo', 'foo2'),
36 36 'c': ('baz', None),
37 37 'd': (None, 'quux')})
38 38 self.assertEqual(join(d1, d2), {'a': ('foo', 'foo2'),
39 39 'b': ('bar', 'bar'),
40 40 'c': ('baz', None),
41 41 'd': (None, 'quux')})
42 42
43 43 # with default argument
44 44 self.assertEqual(diff(d1, d2, 123), {'a': ('foo', 'foo2'),
45 45 'c': ('baz', 123),
46 46 'd': (123, 'quux')})
47 47 self.assertEqual(join(d1, d2, 456), {'a': ('foo', 'foo2'),
48 48 'b': ('bar', 'bar'),
49 49 'c': ('baz', 456),
50 50 'd': (456, 'quux')})
51 51
52 # check that we compare against default
53 self.assertEqual(diff(d1, d2, 'baz'), {'a': ('foo', 'foo2'),
54 'd': ('baz', 'quux')})
55 self.assertEqual(diff(d1, d2, 'quux'), {'a': ('foo', 'foo2'),
56 'c': ('baz', 'quux')})
57
52 58 if __name__ == '__main__':
53 59 silenttestrunner.main(__name__)
General Comments 0
You need to be logged in to leave comments. Login now