jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/807501 )
Change subject: [IMPR] Simplify some code ......................................................................
[IMPR] Simplify some code
- remove keys() method if appropriate - remove list() function if appropriate - simplify Request documentation
Change-Id: Ie3b0968b8fe0ae75b2cd0dc27fbe13b78708d20f --- M pywikibot/bot.py M pywikibot/config.py M pywikibot/cosmetic_changes.py M pywikibot/data/api/_generators.py M pywikibot/data/api/_paraminfo.py M pywikibot/data/api/_requests.py 6 files changed, 25 insertions(+), 29 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 3d6fb8b..012319f 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -485,8 +485,6 @@ # config file dir log('CONFIG FILE DIR: {}'.format(pywikibot.config.base_dir))
- all_modules = sys.modules.keys() - # These are the main dependencies of pywikibot. check_package_list = [ 'requests', @@ -495,7 +493,7 @@
# report all imported packages if config.verbose_output: - check_package_list += all_modules + check_package_list += sys.modules
log('PACKAGES:') packages = version.package_versions(check_package_list) diff --git a/pywikibot/config.py b/pywikibot/config.py index 173ff93..5a3626a 100644 --- a/pywikibot/config.py +++ b/pywikibot/config.py @@ -1053,7 +1053,7 @@ _check_user_config_types(_exec_globals, _public_globals, _imports)
# Copy the user config settings into globals -_modified = {_key for _key in _public_globals.keys() +_modified = {_key for _key in _public_globals if _exec_globals[_key] != globals()[_key]}
if 'user_agent_format' in _modified: @@ -1135,7 +1135,7 @@ else: warning('Unknown arg {} ignored'.format(_arg))
- for _name in sorted(globals().keys()): + for _name in sorted(globals()): if _name[0] != '_' \ and type(globals()[_name]) not in [types.FunctionType, types.ModuleType] \ @@ -1154,7 +1154,7 @@ output('{}={}'.format(_name, _value))
# cleanup all locally-defined variables -for __var in list(globals().keys()): +for __var in list(globals()): if __var.startswith('_') and not __var.startswith('__'): del sys.modules[__name__].__dict__[__var]
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 522f153..93c1013 100644 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -810,8 +810,9 @@ def replace_link(match: Match[str]) -> str: """Create a string to replace a single link.""" replacement = '[[' - if re.match(r'(?:' + '|'.join(list(self.site.namespaces[6]) - + list(self.site.namespaces[14])) + '):', + if re.match(r'(?:{}):' + .format('|'.join((*self.site.namespaces[6], + *self.site.namespaces[14]))), match.group('link')): replacement += ':' replacement += match.group('link') diff --git a/pywikibot/data/api/_generators.py b/pywikibot/data/api/_generators.py index 1510429..e2b4a10 100644 --- a/pywikibot/data/api/_generators.py +++ b/pywikibot/data/api/_generators.py @@ -521,7 +521,7 @@ """Get resultdata and verify result.""" resultdata = keys = self.data['query'][self.resultkey] if isinstance(resultdata, dict): - keys = list(resultdata.keys()) + keys = list(resultdata) if 'results' in resultdata: resultdata = resultdata['results'] elif 'pageids' in self.data['query']: @@ -531,7 +531,7 @@ for k in self.data['query']['pageids']] else: resultdata = [resultdata[k] - for k in sorted(resultdata.keys())] + for k in sorted(resultdata)] pywikibot.debug('{name} received {keys}; limit={limit}' .format(name=type(self).__name__, keys=keys, limit=self.limit)) @@ -545,13 +545,12 @@ continue
yield result - if isinstance(item, dict) \ - and set(self.continuekey) & set(item.keys()): + if isinstance(item, dict) and set(self.continuekey) & set(item): # if we need to count elements contained in items in # self.data["query"]["pages"], we want to count # item[self.continuekey] (e.g. 'revisions') and not # self.resultkey (i.e. 'pages') - for key in set(self.continuekey) & set(item.keys()): + for key in set(self.continuekey) & set(item): self._count += len(item[key]) # otherwise we proceed as usual else: diff --git a/pywikibot/data/api/_paraminfo.py b/pywikibot/data/api/_paraminfo.py index 16e6406..36d5e69 100644 --- a/pywikibot/data/api/_paraminfo.py +++ b/pywikibot/data/api/_paraminfo.py @@ -202,7 +202,7 @@ del failed_modules[:] yield batch
- modules = modules - set(self._paraminfo.keys()) + modules = modules - set(self._paraminfo) if not modules: return
diff --git a/pywikibot/data/api/_requests.py b/pywikibot/data/api/_requests.py index cf8eb26..f676a4f 100644 --- a/pywikibot/data/api/_requests.py +++ b/pywikibot/data/api/_requests.py @@ -77,18 +77,18 @@ below) get passed as commands to api.php, and can be get or set using the dict interface. All attributes must be strings. Use an empty string for parameters that don't require a value. For example, - Request(action="query", titles="Foo bar", prop="info", redirects="") + ``Request(action="query", titles="Foo bar", prop="info", redirects="")`` corresponds to the API request - "api.php?action=query&titles=Foo%20bar&prop=info&redirects" + ``api.php?action=query&titles=Foo%20bar&prop=info&redirects``
This is the lowest-level interface to the API, and can be used for any request that a particular site's API supports. See the API documentation (https://www.mediawiki.org/wiki/API) and site-specific settings for details on what parameters are accepted for each request type.
- Uploading files is a special case: to upload, the parameter "mime" must - contain a dict, and the parameter "file" must be set equal to a valid - filename on the local computer, _not_ to the content of the file. + Uploading files is a special case: to upload, the parameter `mime` must + contain a dict, and the parameter `file` must be set equal to a valid + filename on the local computer, *not* to the content of the file.
Returns a dict containing the JSON data returned by the wiki. Normally, one of the dict keys will be equal to the value of the 'action' @@ -104,15 +104,15 @@ >>> # add a new parameter >>> r['siprop'] = "namespaces" >>> # note that "uiprop" param gets added automatically - >>> str(r.action) + >>> r.action 'query' - >>> sorted(str(key) for key in r._params.keys()) + >>> sorted(r._params) ['action', 'meta', 'siprop'] - >>> [str(key) for key in r._params['action']] + >>> r._params['action'] ['query'] - >>> [str(key) for key in r._params['meta']] + >>> r._params['meta'] ['userinfo', 'siteinfo'] - >>> [str(key) for key in r._params['siprop']] + >>> r._params['siprop'] ['namespaces'] >>> data = r.submit() >>> isinstance(data, dict) @@ -121,9 +121,8 @@ True >>> 'query' in data True - >>> sorted(str(key) for key in data['query'].keys()) + >>> sorted(data['query']) ['namespaces', 'userinfo'] - """
# To make sure the default value of 'parameters' can be identified. @@ -383,7 +382,7 @@
def keys(self): """Implement dict interface.""" - return list(self._params.keys()) + return list(self._params)
def __iter__(self): """Implement dict interface.""" @@ -410,8 +409,7 @@ if hasattr(self, '__defaulted'): return
- if self.mime is not None \ - and set(self._params.keys()) & set(self.mime.keys()): + if self.mime is not None and set(self._params) & set(self.mime): raise ValueError('The mime and params shall not share the ' 'same keys.')