jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/597271 )
Change subject: [IMPR] WikiStats table may be sorted by any key ......................................................................
[IMPR] WikiStats table may be sorted by any key
- WikiStats table may be sorted by any key. Therefore check for the entry type for the given key in the first row. - Use reverse order for numeric values but use alphabetical sorting order for alphanumeric values - A new reverse option is used to override the defaults above. - Tests added
Change-Id: I5e860e6b35d1988531bc96601a0cba672efef3e6 --- M pywikibot/data/wikistats.py M tests/wikistats_tests.py 2 files changed, 36 insertions(+), 4 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/wikistats.py b/pywikibot/data/wikistats.py index edf811c..5d70500 100644 --- a/pywikibot/data/wikistats.py +++ b/pywikibot/data/wikistats.py @@ -7,6 +7,7 @@ from collections import defaultdict from csv import DictReader from io import StringIO +from typing import Optional
import pywikibot
@@ -168,14 +169,30 @@ """ return {data['prefix']: data for data in self.get(table)}
- def sorted(self, table, key) -> list: + def sorted(self, table: str, key: str, + reverse: Optional[bool] = None) -> list: """ Reverse numerical sort of data.
@param table: name of table of data - @param key: numerical key, such as id, total, good + @param key: data table key + @param reverse: If set to True the sorting order is reversed. + If None the sorting order for numeric keys are reversed whereas + alphanumeric keys are sorted in normal way. + @return: The sorted table """ - return sorted(self.get(table), key=lambda d: int(d[key]), reverse=True) + table = self.get(table) + + # take the first entry to determine the sorting key + first_entry = table[0] + if first_entry[key].isdigit(): + sort_key = lambda d: int(d[key]) # noqa: E731 + reverse = reverse if reverse is not None else True + else: + sort_key = lambda d: d[key] # noqa: E731 + reverse = reverse if reverse is not None else False + + return sorted(table, key=sort_key, reverse=reverse)
def languages_by_size(self, table: str): """Return ordered list of languages by size from WikiStats.""" diff --git a/tests/wikistats_tests.py b/tests/wikistats_tests.py index 0aaf8cf..a734807 100644 --- a/tests/wikistats_tests.py +++ b/tests/wikistats_tests.py @@ -20,7 +20,7 @@
hostname = 'https://wikistats.wmflabs.org/api.php'
- def test_sort(self): + def test_sort_numeric(self): """Test sorted results.""" keys = ('good', 'prefix', 'total')
@@ -46,6 +46,21 @@ self.assertGreater(int(top['good']), int(bottom['good'])) self.assertGreater(int(top['total']), int(bottom['total']))
+ def test_sort_alphabetic(self): + """Test alphabetic sorted results.""" + ws = WikiStats() + for reverse in (True, False): + last = ' ~'[reverse] # first and last printable ASCII + data = ws.sorted('wikisource', 'prefix', reverse=reverse) + with self.subTest(reverse=reverse): + for entry in data: + code = entry['prefix'] + if reverse: + self.assertGreater(last, code) + else: + self.assertLess(last, code) + last = code + def test_sorting_order(self): """Test sorting order of languages_by_size.""" family = 'wikipedia'
pywikibot-commits@lists.wikimedia.org