John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/203512

Change subject: Simplify claim removal in ItemPage.toJSON
......................................................................

Simplify claim removal in ItemPage.toJSON

Also removes incorrect indent level on line 3346.

Change-Id: I16f67dbbb3686e18cee387d2abc6d6239d05d11c
---
M pywikibot/page.py
1 file changed, 37 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/12/203512/1

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 4e29b24..5ec3459 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -3173,6 +3173,17 @@
             data[type_key] = source
 
     def toJSON(self, diffto=None):
+        """
+        Create JSON suitable for Wikibase API.
+
+        When diffto is provided, JSON representing differences
+        to the provided data is created.
+
+        @param diffto: JSON containing claim data
+        @type diffto: dict
+
+        @return: dict
+        """
         data = {}
         self._diff_to('labels', 'language', 'value', diffto, data)
 
@@ -3507,6 +3518,17 @@
         return self.__class__(target.site, target.title(), target.namespace())
 
     def toJSON(self, diffto=None):
+        """
+        Create JSON suitable for Wikibase API.
+
+        When diffto is provided, JSON representing differences
+        to the provided data is created.
+
+        @param diffto: JSON containing claim data
+        @type diffto: dict
+
+        @return: dict
+        """
         data = super(ItemPage, self).toJSON(diffto=diffto)
 
         self._diff_to('sitelinks', 'site', 'title', diffto, data)
@@ -3518,6 +3540,8 @@
 
         if diffto and 'claims' in diffto:
             temp = {}
+            claim_ids = set()
+
             for prop in claims:
                 for claim in claims[prop]:
                     if (prop not in diffto['claims'] or
@@ -3525,15 +3549,19 @@
                         if prop not in temp:
                             temp[prop] = []
                         temp[prop].append(claim)
-            for prop in diffto['claims']:
-                if prop not in claims:
-                    claims[prop] = []
-                for claim1 in diffto['claims'][prop]:
-                    if 'id' in claim1 and claim1['id'] not in \
-                    [claim2['id'] for claim2 in claims[prop] if 'id' in 
claim2]:
-                        if prop not in temp:
-                            temp[prop] = []
-                        temp[prop].append({'id': claim1['id'], 'remove': ''})
+
+                    claim_ids.add(claim['id'])
+
+            removed_claims = [(prop, claim['id'])
+                              for prop in diffto['claims']
+                              for claim in diffto['claims'][prop]
+                              if 'id' in claim and
+                              claim['id'] not in claim_ids]
+
+            for prop, claim_id in removed_claims:
+                temp.setdefault(prop, []).append(
+                    {'id': claim_id, 'remove': ''})
+
             claims = temp
 
         if claims:

-- 
To view, visit https://gerrit.wikimedia.org/r/203512
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I16f67dbbb3686e18cee387d2abc6d6239d05d11c
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to