mercurial/dicthelpers.py
author Bryan O'Sullivan <bryano@fb.com>
Mon, 08 Apr 2013 15:04:17 -0700
changeset 18889 8c64c4af21a4
parent 18847 40c679748fa9
child 18894 ed46c2b98b0d
permissions -rw-r--r--
templater: fix check-code error
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18820
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     1
# dicthelpers.py - helper routines for Python dicts
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     2
#
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     3
# Copyright 2013 Facebook
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     4
#
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     7
18847
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
     8
def diff(d1, d2, default=None):
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
     9
    '''Return all key-value pairs that are different between d1 and d2.
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    10
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    11
    This includes keys that are present in one dict but not the other, and
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    12
    keys whose values are different. The return value is a dict with values
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    13
    being pairs of values from d1 and d2 respectively, and missing values
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    14
    represented as default.'''
18820
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    15
    res = {}
18847
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    16
    if d1 is d2:
18820
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    17
        # same dict, so diff is empty
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    18
        return res
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    19
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    20
    for k1, v1 in d1.iteritems():
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    21
        if k1 in d2:
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    22
            v2 = d2[k1]
18847
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    23
            if v1 != v2:
18820
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    24
                res[k1] = (v1, v2)
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    25
        else:
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    26
            res[k1] = (v1, default)
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    27
18847
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    28
    for k2 in d2:
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    29
        if k2 not in d1:
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    30
            res[k2] = (default, d2[k2])
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    31
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    32
    return res
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    33
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    34
def join(d1, d2, default=None):
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    35
    '''Return all key-value pairs from both d1 and d2.
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    36
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    37
    This is akin to an outer join in relational algebra. The return value is a
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    38
    dict with values being pairs of values from d1 and d2 respectively, and
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    39
    missing values represented as default.'''
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    40
    res = {}
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    41
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    42
    for k1, v1 in d1.iteritems():
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    43
        if k1 in d2:
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    44
            res[k1] = (v1, d2[k1])
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    45
        else:
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    46
            res[k1] = (v1, default)
40c679748fa9 dicthelpers: inline diff and join code
Siddharth Agarwal <sid0@fb.com>
parents: 18846
diff changeset
    47
18820
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    48
    if d1 is d2:
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    49
        return res
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    50
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    51
    for k2 in d2:
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    52
        if k2 not in d1:
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    53
            res[k2] = (default, d2[k2])
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    54
a45e44d76c81 mercurial: implement diff and join for dicts
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    55
    return res