jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/660809 )

Change subject: [IMPR] Create a SiteLink with __getitem__ method
......................................................................

[IMPR] Create a SiteLink with __getitem__ method

SiteLinkCollection initializer updates data contents to self._data
dict. The update method calls __setitem__ for each data entry; for
each entry a SiteLink is created which needs a siteinfo content for
every affected site and leads to a huge loading time.

Now with this patch the SiteLink object is created when the link
is fetched from self._data via __getitem__ or __iter__ method.
This prevents siteinfo content for each site from loading even
a link is currently not used and decrease loading time a lot.

Bug: T245809
Bug: T226157
Bug: T238471
Bug: T273386
Change-Id: Icc92cfd5d200fde47eb127e2fe601f812427624a
---
M pywikibot/page/__init__.py
M tests/wikibase_tests.py
2 files changed, 20 insertions(+), 12 deletions(-)

Approvals:
  Matěj Suchánek: Looks good to me, but someone else must approve
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index 912f619..be8b2ce 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -3528,23 +3528,33 @@
         @rtype: pywikibot.page.SiteLink
         """
         key = self.getdbName(key)
-        return self._data[key]
+        val = self._data[key]
+        if isinstance(val, str):
+            val = SiteLink(val, key)
+        elif isinstance(val, dict):
+            val = SiteLink.fromJSON(val, self.repo)
+        else:
+            return val
+        self._data[key] = val
+        return val

     def __setitem__(self, key, val):
         """
         Set the SiteLink for a given key.

+        This only sets the value given as str, dict or SiteLink. If a
+        str or dict is given the SiteLink object is created later in
+        __getitem__ method.
+
         @param key: site key as Site instance or db key
         @type key: pywikibot.Site or str
-        @param val: page name as a string or JSON containing SiteLink data
-        @type val: dict or str
-        @rtype: pywikibot.page.SiteLink
+        @param val: page name as a string or JSON containing SiteLink
+            data or a SiteLink object
+        @type val: Union[str, dict, SiteLink]
         """
-        if isinstance(val, str):
-            val = SiteLink(val, key)
-        else:
-            val = SiteLink.fromJSON(val, self.repo)
         key = self.getdbName(key)
+        if isinstance(val, SiteLink):
+            assert val.site.dbName() == key
         self._data[key] = val

     def __delitem__(self, key):
@@ -3851,8 +3861,8 @@
         self.latest_revision_id = self._content.get('lastrevid')

         data = {}
-        # todo: this initializes all data,
-        # make use of lazy initialization (T245809)
+
+        # This initializes all data,
         for key, cls in self.DATA_ATTRIBUTES.items():
             value = cls.fromJSON(self._content.get(key, {}), self.repo)
             setattr(self, key, value)
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index f77af864..6a9740b 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -2352,8 +2352,6 @@

     """Test cases to test toJSON() functions."""

-    dry = True
-
     def setUp(self):
         """Setup test."""
         super().setUp()

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/660809
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Icc92cfd5d200fde47eb127e2fe601f812427624a
Gerrit-Change-Number: 660809
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Bugreporter <[email protected]>
Gerrit-Reviewer: JAn Dudík <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to