jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1096307?usp=email )
Change subject: Code improvements
......................................................................
Code improvements
- avoid redefinition of local variables within for loops
- use HTTPStatus enum which is more readable
- use dict.items() if keys and values are used
- simplify comparison for multiple values
Change-Id: I43834038aabaa6ae921b0ae7d742e8c9478ef062
---
M pywikibot/bot.py
M pywikibot/cosmetic_changes.py
M pywikibot/data/superset.py
M pywikibot/page/_page.py
M pywikibot/page/_toolforge.py
M pywikibot/page/_wikibase.py
M pywikibot/site/_datasite.py
M pywikibot/site/_generators.py
M pywikibot/tools/__init__.py
M tests/site_tests.py
10 files changed, 29 insertions(+), 27 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 010005c..6e9efa8 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -2042,17 +2042,18 @@
def get_property_by_name(self, property_name: str) -> str:
"""Find given property and return its ID.
- Method first uses site.search() and if the property isn't found, then
- asks user to provide the property ID.
+ Method first uses site.search() and if the property isn't found,
+ then asks user to provide the property ID.
:param property_name: property to find
"""
ns = self.repo.property_namespace
for page in self.repo.search(property_name, total=1, namespaces=ns):
- page = pywikibot.PropertyPage(self.repo, page.title())
+ prop = pywikibot.PropertyPage(self.repo, page.title())
pywikibot.info(
- f'Assuming that {property_name} property is {page.id}.')
- return page.id
+ f'Assuming that {property_name} property is {prop.id}.')
+ return prop.id
+
return pywikibot.input(
f'Property {property_name} was not found. Please enter the '
f'property ID (e.g. P123) of it:').upper()
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index cf3df33..76fdeef 100644
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -462,9 +462,8 @@
else 'User talk']
# lowerspaced and underscored namespaces
for i, item in enumerate(namespaces):
- item = item.replace(' ', '[ _]')
- item = f'[{item[0]}{item[0].lower()}]' + item[1:]
- namespaces[i] = item
+ ns = item.replace(' ', '[ _]')
+ namespaces[i] = f'[{ns[0]}{ns[0].lower()}]{ns[1:]}'
namespaces.append(first_lower(final_ns))
if final_ns and namespaces:
if (self.site.sitename == 'wikipedia:pt'
diff --git a/pywikibot/data/superset.py b/pywikibot/data/superset.py
index 101b96d..7fd0e14 100644
--- a/pywikibot/data/superset.py
+++ b/pywikibot/data/superset.py
@@ -9,6 +9,7 @@
#
from __future__ import annotations
+from http import HTTPStatus
from textwrap import fill
from typing import TYPE_CHECKING, Any
@@ -91,9 +92,9 @@
self.last_response = http.fetch(url)
# Handle error cases
- if self.last_response.status_code == 200:
+ if self.last_response.status_code == HTTPStatus.OK:
self.connected = True
- elif self.last_response.status_code == 401:
+ elif self.last_response.status_code == HTTPStatus.UNAUTHORIZED:
self.connected = False
raise NoUsernameError(fill(
'User not logged in. You need to log in to '
@@ -124,7 +125,7 @@
url = f'{self.superset_url}/api/v1/security/csrf_token/'
self.last_response = http.fetch(url)
- if self.last_response.status_code == 200:
+ if self.last_response.status_code == HTTPStatus.OK:
return self.last_response.json()['result']
status_code = self.last_response.status_code
@@ -147,12 +148,12 @@
url += f'/api/v1/database/{database_id}/schemas/?q=(force:!f)'
self.last_response = http.fetch(url)
- if self.last_response.status_code == 200:
+ if self.last_response.status_code == HTTPStatus.OK:
schemas = self.last_response.json()['result']
if schema_name in schemas:
return database_id
- elif self.last_response.status_code == 404:
+ elif self.last_response.status_code == HTTPStatus.NOT_FOUND:
break
else:
status_code = self.last_response.status_code
diff --git a/pywikibot/page/_page.py b/pywikibot/page/_page.py
index 48c74c8..199d4d6 100644
--- a/pywikibot/page/_page.py
+++ b/pywikibot/page/_page.py
@@ -112,9 +112,9 @@
positional.append(intkeys[i])
continue
- for k in intkeys:
+ for k, v in intkeys.items():
if k < 1 or k >= i:
- named[str(k)] = intkeys[k]
+ named[str(k)] = v
break
for item in named.items():
diff --git a/pywikibot/page/_toolforge.py b/pywikibot/page/_toolforge.py
index 1d7aa8a..19a2782 100644
--- a/pywikibot/page/_toolforge.py
+++ b/pywikibot/page/_toolforge.py
@@ -11,6 +11,7 @@
import collections
import re
+from http import HTTPStatus
from typing import TYPE_CHECKING
import pywikibot
@@ -183,7 +184,7 @@
url = baseurl.format(url=url)
r = pywikibot.comms.http.fetch(url)
- if r.status_code != 200:
+ if r.status_code != HTTPStatus.OK:
r.raise_for_status()
result: list[list[str]] = []
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 34e3f58..4324705 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1616,8 +1616,8 @@
qualifier.on_item = item
for source in self.sources:
for values in source.values():
- for source in values:
- source.on_item = item
+ for val in values:
+ val.on_item = item
def _assert_attached(self) -> None:
if self.on_item is None:
diff --git a/pywikibot/site/_datasite.py b/pywikibot/site/_datasite.py
index d4a645b..8568485 100644
--- a/pywikibot/site/_datasite.py
+++ b/pywikibot/site/_datasite.py
@@ -345,9 +345,9 @@
params['token'] = self.tokens['csrf']
- for arg in kwargs:
+ for arg, param in kwargs.items():
if arg in ['clear', 'summary', 'tags']:
- params[arg] = kwargs[arg]
+ params[arg] = param
elif arg != 'baserevid':
warn(f'Unknown wbeditentity parameter {arg} ignored',
UserWarning, 2)
@@ -1002,9 +1002,9 @@
})
params.update(prepare_data(action, action_data))
- for arg in kwargs:
+ for arg, param in kwargs.items():
if arg in ['summary', 'tags']:
- params[arg] = kwargs[arg]
+ params[arg] = param
else:
warn(f'Unknown parameter {arg} for action {action}, ignored',
UserWarning, 2)
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index d77d675..8f5c7f7 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -209,9 +209,9 @@
# This checks to see if there is a normalized title in
# the response that corresponds to the canonical form
# used in the query.
- for key in cache:
+ for key, value in cache.items():
if self.sametitle(key, pagedata['title']):
- cache[pagedata['title']] = cache[key]
+ cache[pagedata['title']] = value
break
else:
pywikibot.warning(
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index e23a89c..298f095 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -581,9 +581,9 @@
with open(filename, 'rb') as f:
magic_number = f.read(8)
- for pattern in extension_map:
+ for pattern, ext in extension_map.items():
if magic_number.startswith(pattern):
- extension = extension_map[pattern]
+ extension = ext
break
else:
extension = ''
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 7a0505b..b10ef12 100755
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -196,7 +196,7 @@
self.assertLength(mysite.mediawiki_messages(months, lang1), 12)
self.assertLength(mysite.mediawiki_messages(months, lang2), 12)
familyname = mysite.family.name
- if lang1 != lang2 and lang1 != familyname and lang2 != familyname:
+ if lang1 not in (lang2, familyname) and lang2 != familyname:
self.assertNotEqual(mysite.mediawiki_messages(months, lang1),
mysite.mediawiki_messages(months, lang2))
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1096307?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I43834038aabaa6ae921b0ae7d742e8c9478ef062
Gerrit-Change-Number: 1096307
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]