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

Change subject: [IMPR] use defaultdict in wikistats.py
......................................................................

[IMPR] use defaultdict in wikistats.py

Change-Id: I84253c44d24f414b370074d481e3c5fc8735bebf
---
M pywikibot/data/wikistats.py
1 file changed, 9 insertions(+), 16 deletions(-)

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



diff --git a/pywikibot/data/wikistats.py b/pywikibot/data/wikistats.py
index 19d5c7c..5c2f0f4 100644
--- a/pywikibot/data/wikistats.py
+++ b/pywikibot/data/wikistats.py
@@ -6,6 +6,7 @@
 # Distributed under the terms of the MIT license.
 import csv

+from collections import defaultdict
 from io import BytesIO, StringIO

 import pywikibot
@@ -74,8 +75,8 @@
     def __init__(self, url='https://wikistats.wmflabs.org/') -> None:
         """Initializer."""
         self.url = url
-        self._raw = {}
-        self._data = {}
+        self._raw = defaultdict(dict)
+        self._data = defaultdict(dict)

     def fetch(self, table: str, format='xml'):
         """
@@ -110,13 +111,10 @@
         @type format: 'xml' or 'csv'.
         @rtype: bytes
         """
-        if format not in self._raw:
-            self._raw[format] = {}
         if table in self._raw[format]:
             return self._raw[format][table]

         data = self.fetch(table, format)
-
         self._raw[format][table] = data
         return data

@@ -127,13 +125,11 @@
         @param table: table of data to fetch
         @rtype: list
         """
-        if table in self._data.setdefault('csv', {}):
+        if table in self._data['csv']:
             return self._data['csv'][table]

-        data = self.raw_cached(table, 'csv')
-
-        f = StringIO(data.decode('utf8'))
-
+        raw = self.raw_cached(table, 'csv')
+        f = StringIO(raw.decode('utf8'))
         reader = csv.DictReader(f)
         data = list(reader)
         self._data['csv'][table] = data
@@ -147,18 +143,16 @@
         @param table: table of data to fetch
         @rtype: list
         """
-        if table in self._data.setdefault('xml', {}):
+        if table in self._data['xml']:
             return self._data['xml'][table]

         from xml.etree import ElementTree

-        data = self.raw_cached(table, 'xml')
-
-        f = BytesIO(data)
+        raw = self.raw_cached(table, 'xml')
+        f = BytesIO(raw)
         tree = ElementTree.parse(f)

         data = []
-
         for row in tree.findall('row'):
             site = {}

@@ -169,7 +163,6 @@
             data.append(site)

         self._data['xml'][table] = data
-
         return data

     def get(self, table: str, format='csv'):

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/612572
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: I84253c44d24f414b370074d481e3c5fc8735bebf
Gerrit-Change-Number: 612572
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: D3r1ck01 <[email protected]>
Gerrit-Reviewer: Dvorapa <[email protected]>
Gerrit-Reviewer: Matěj Suchánek <[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