Mpaa has uploaded a new change for review. https://gerrit.wikimedia.org/r/320849
Change subject: Improve for loop in TextfilePageGenerator - remove control variable using for ... else ... - use context manager for file handling ...................................................................... Improve for loop in TextfilePageGenerator - remove control variable using for ... else ... - use context manager for file handling Change-Id: I5214d10e480ce6583f3890e51cc781bb65b3bd5b --- M pywikibot/pagegenerators.py 1 file changed, 16 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/49/320849/1 diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index e60c98f..1e70e95 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -1304,24 +1304,22 @@ filename = pywikibot.input(u'Please enter the filename:') if site is None: site = pywikibot.Site() - f = codecs.open(filename, 'r', config.textfile_encoding) - linkmatch = None - for linkmatch in pywikibot.link_regex.finditer(f.read()): - # If the link is in interwiki format, the Page object may reside - # on a different Site than the default. - # This makes it possible to work on different wikis using a single - # text file, but also could be dangerous because you might - # inadvertently change pages on another wiki! - yield pywikibot.Page(pywikibot.Link(linkmatch.group("title"), site)) - if linkmatch is None: - f.seek(0) - for title in f: - title = title.strip() - if '|' in title: - title = title[:title.index('|')] - if title: - yield pywikibot.Page(site, title) - f.close() + with codecs.open(filename, 'r', config.textfile_encoding) as f: + for linkmatch in pywikibot.link_regex.finditer(f.read()): + # If the link is in interwiki format, the Page object may reside + # on a different Site than the default. + # This makes it possible to work on different wikis using a single + # text file, but also could be dangerous because you might + # inadvertently change pages on another wiki! + yield pywikibot.Page(pywikibot.Link(linkmatch.group("title"), site)) + else: + f.seek(0) + for title in f: + title = title.strip() + if '|' in title: + title = title[:title.index('|')] + if title: + yield pywikibot.Page(site, title) def PagesFromTitlesGenerator(iterable, site=None): -- To view, visit https://gerrit.wikimedia.org/r/320849 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5214d10e480ce6583f3890e51cc781bb65b3bd5b Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Mpaa <mpaa.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits