[Python-modules-commits] [python-pyld] 54/276: Modified mergedContexts to work with a list of contexts.

Wolfgang Borgert debacle at moszumanska.debian.org
Wed Oct 8 23:47:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-pyld.

commit 5d6038ac82ae9522bb6267754fb524bf0166cc6a
Author: Françoise Conil <fcodvpt at gmail.com>
Date:   Wed Feb 1 15:02:54 2012 +0100

    Modified mergedContexts to work with a list of contexts.
---
 lib/pyld/jsonld.py | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)

diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py
index f7d0518..aec3b74 100644
--- a/lib/pyld/jsonld.py
+++ b/lib/pyld/jsonld.py
@@ -1903,6 +1903,27 @@ def compact(ctx, input):
 
     return rval
 
+def mergeDicts(merged, dict):
+    """
+    Merges dict to the merged dictionnary.
+
+    :param merged: the dictionnay to overwrite/append to.
+    :param dict: the new dict to merge onto merged.
+
+    :return: the merged dictionary.
+    """
+    for key in dict:
+        # ignore special keys starting with '@'
+        if key.find('@') != 0:
+            # If key exists, value is replaced
+            # Otherwise, value is created
+
+            # Following comment was in main PyLD branch
+            # FIXME: update related coerce rules
+            merged[key] = dict[key]
+
+    return merged
+
 def mergeContexts(ctx1, ctx2):
     """
     Merges one context with another.
@@ -1917,18 +1938,14 @@ def mergeContexts(ctx1, ctx2):
 
     # if the new context contains any IRIs that are in the merged context,
     # remove them from the merged context, they will be overwritten
-    for key in ctx2:
-        # ignore special keys starting with '@'
-        if key.find('@') != 0:
-            for mkey in merged:
-                if merged[mkey] == ctx2[key]:
-                    # FIXME: update related coerce rules
-                    del merged[mkey]
-                    break
-
-    # merge contexts
-    for key in ctx2:
-        merged[key] = ctx2[key]
+    if isinstance(ctx2, dict):
+        merged = mergeDicts(merged, ctx2)
+    elif isinstance(ctx2, list):
+        for ctx2Elt in ctx2:
+            # TODO : What if ctx2Elt is an IRI to an external context
+            #        Can ctx2Elt be a list again ?
+            if isinstance(ctx2Elt, dict):
+                merged = mergeDicts(merged, ctx2Elt)
 
     return merged
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-pyld.git



More information about the Python-modules-commits mailing list