jenkins-bot submitted this change.

View Change

Approvals: Matěj Suchánek: Looks good to me, but someone else must approve Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] remove code duplication in page.__init__.py

Introduce a common BaseDataDict as parent for:
- LanguageDict
- AliasesDict

Change-Id: Ie4f01d57f9274d5f68a1224303f87bb79906a885
---
M pywikibot/page/__init__.py
1 file changed, 20 insertions(+), 42 deletions(-)

diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index 9979bdc..63cb14a 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -3356,13 +3356,13 @@
return self.isRegistered() and 'bot' not in self.groups()


-class LanguageDict(MutableMapping):
+class BaseDataDict(MutableMapping):

"""
- A structure holding language data for a Wikibase entity.
+ Base structure holding data for a Wikibase entity.

- Language data are mappings from a language to a string. It can be
- labels, descriptions and others.
+ Data are mappings from a language to a value. It will be
+ specialised in subclasses.
"""

def __init__(self, data=None):
@@ -3372,11 +3372,6 @@
self.update(data)

@classmethod
- def fromJSON(cls, data, repo=None):
- this = cls({key: value['value'] for key, value in data.items()})
- return this
-
- @classmethod
def new_empty(cls, repo):
return cls()

@@ -3409,6 +3404,21 @@
key = key.lang
return key

+
+class LanguageDict(BaseDataDict):
+
+ """
+ A structure holding language data for a Wikibase entity.
+
+ Language data are mappings from a language to a string. It can be
+ labels, descriptions and others.
+ """
+
+ @classmethod
+ def fromJSON(cls, data, repo=None):
+ this = cls({key: value['value'] for key, value in data.items()})
+ return this
+
@classmethod
def normalizeData(cls, data):
norm_data = {}
@@ -3436,7 +3446,7 @@
return data


-class AliasesDict(MutableMapping):
+class AliasesDict(BaseDataDict):

"""
A structure holding aliases for a Wikibase entity.
@@ -3444,12 +3454,6 @@
It is a mapping from a language to a list of strings.
"""

- def __init__(self, data=None):
- super().__init__()
- self._data = {}
- if data:
- self.update(data)
-
@classmethod
def fromJSON(cls, data, repo=None):
this = cls()
@@ -3458,32 +3462,6 @@
return this

@classmethod
- def new_empty(cls, repo):
- return cls()
-
- def __getitem__(self, key):
- key = LanguageDict.normalizeKey(key)
- return self._data[key]
-
- def __setitem__(self, key, value):
- key = LanguageDict.normalizeKey(key)
- self._data[key] = value
-
- def __delitem__(self, key):
- key = LanguageDict.normalizeKey(key)
- del self._data[key]
-
- def __iter__(self):
- return iter(self._data)
-
- def __len__(self):
- return len(self._data)
-
- def __contains__(self, key):
- key = LanguageDict.normalizeKey(key)
- return key in self._data
-
- @classmethod
def normalizeData(cls, data):
norm_data = {}
for key, values in data.items():

To view, visit change 642653. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie4f01d57f9274d5f68a1224303f87bb79906a885
Gerrit-Change-Number: 642653
Gerrit-PatchSet: 1
Gerrit-Owner: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged