Patchwork [4,of,9] pure: implement dicthelpers in Python

login
register
mail settings
Submitter Siddharth Agarwal
Date March 25, 2013, 12:27 a.m.
Message ID <78f178247f634bac6ff2.1364171268@sid0x220>
Download mbox | patch
Permalink /patch/1180/
State Rejected
Headers show

Comments

Siddharth Agarwal - March 25, 2013, 12:27 a.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1364156266 25200
#      Sun Mar 24 13:17:46 2013 -0700
# Node ID 78f178247f634bac6ff2ffe54b0d06185ef0e670
# Parent  5cd3c7ddca7d3a7c2186608abf65080fbf849d47
pure: implement dicthelpers in Python
Siddharth Agarwal - March 25, 2013, 6:18 p.m.
On 03/25/2013 10:22 AM, Kevin Bullock wrote:
>> This one should come first, and should include some baseline tests
> ...which I now see are in patch 5...

Yeah, that probably makes more sense.

Patch

diff --git a/mercurial/pure/dicthelpers.py b/mercurial/pure/dicthelpers.py
new file mode 100644
--- /dev/null
+++ b/mercurial/pure/dicthelpers.py
@@ -0,0 +1,35 @@ 
+# parsers.py - Python implementation of dicthelpers.py
+#
+# Copyright 2013 Facebook
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+def _diffjoin(d1, d2, default, compare):
+    res = {}
+    if d1 is d2 and compare:
+        # same dict, so diff is empty
+        return res
+
+    for k1, v1 in d1.iteritems():
+        if k1 in d2:
+            v2 = d2[k1]
+            if not compare or v1 != v2:
+                res[k1] = (v1, v2)
+        else:
+            res[k1] = (v1, default)
+
+    if d1 is d2:
+        return res
+
+    for k2 in d2:
+        if k2 not in d1:
+            res[k2] = (default, d2[k2])
+
+    return res
+
+def diff(d1, d2, default=None):
+    return _diffjoin(d1, d2, default, True)
+
+def join(d1, d2, default=None):
+    return _diffjoin(d1, d2, default, False)