Show More
@@ -5,20 +5,46 b'' | |||||
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 |
def |
|
8 | def diff(d1, d2, default=None): | |
|
9 | '''Return all key-value pairs that are different between d1 and d2. | |||
|
10 | ||||
|
11 | This includes keys that are present in one dict but not the other, and | |||
|
12 | keys whose values are different. The return value is a dict with values | |||
|
13 | being pairs of values from d1 and d2 respectively, and missing values | |||
|
14 | represented as default.''' | |||
9 | res = {} |
|
15 | res = {} | |
10 |
if d1 is d2 |
|
16 | if d1 is d2: | |
11 | # same dict, so diff is empty |
|
17 | # same dict, so diff is empty | |
12 | return res |
|
18 | return res | |
13 |
|
19 | |||
14 | for k1, v1 in d1.iteritems(): |
|
20 | for k1, v1 in d1.iteritems(): | |
15 | if k1 in d2: |
|
21 | if k1 in d2: | |
16 | v2 = d2[k1] |
|
22 | v2 = d2[k1] | |
17 |
if |
|
23 | if v1 != v2: | |
18 | res[k1] = (v1, v2) |
|
24 | res[k1] = (v1, v2) | |
19 | else: |
|
25 | else: | |
20 | res[k1] = (v1, default) |
|
26 | res[k1] = (v1, default) | |
21 |
|
27 | |||
|
28 | for k2 in d2: | |||
|
29 | if k2 not in d1: | |||
|
30 | res[k2] = (default, d2[k2]) | |||
|
31 | ||||
|
32 | return res | |||
|
33 | ||||
|
34 | def join(d1, d2, default=None): | |||
|
35 | '''Return all key-value pairs from both d1 and d2. | |||
|
36 | ||||
|
37 | This is akin to an outer join in relational algebra. The return value is a | |||
|
38 | dict with values being pairs of values from d1 and d2 respectively, and | |||
|
39 | missing values represented as default.''' | |||
|
40 | res = {} | |||
|
41 | ||||
|
42 | for k1, v1 in d1.iteritems(): | |||
|
43 | if k1 in d2: | |||
|
44 | res[k1] = (v1, d2[k1]) | |||
|
45 | else: | |||
|
46 | res[k1] = (v1, default) | |||
|
47 | ||||
22 | if d1 is d2: |
|
48 | if d1 is d2: | |
23 | return res |
|
49 | return res | |
24 |
|
50 | |||
@@ -27,20 +53,3 b' def _diffjoin(d1, d2, default, compare):' | |||||
27 | res[k2] = (default, d2[k2]) |
|
53 | res[k2] = (default, d2[k2]) | |
28 |
|
54 | |||
29 | return res |
|
55 | return res | |
30 |
|
||||
31 | def diff(d1, d2, default=None): |
|
|||
32 | '''Return all key-value pairs that are different between d1 and d2. |
|
|||
33 |
|
||||
34 | This includes keys that are present in one dict but not the other, and |
|
|||
35 | keys whose values are different. The return value is a dict with values |
|
|||
36 | being pairs of values from d1 and d2 respectively, and missing values |
|
|||
37 | represented as default.''' |
|
|||
38 | return _diffjoin(d1, d2, default, True) |
|
|||
39 |
|
||||
40 | def join(d1, d2, default=None): |
|
|||
41 | '''Return all key-value pairs from both d1 and d2. |
|
|||
42 |
|
||||
43 | This is akin to an outer join in relational algebra. The return value is a |
|
|||
44 | dict with values being pairs of values from d1 and d2 respectively, and |
|
|||
45 | missing values represented as default.''' |
|
|||
46 | return _diffjoin(d1, d2, default, False) |
|
General Comments 0
You need to be logged in to leave comments.
Login now