jenkins-bot submitted this change.
[IMPR] Simplify code using itertools
Use backported pairwise and count.
Bug: T340632
Change-Id: Ieed3741d39e1c2f36f5757f348ce313af5d30381
---
M pywikibot/textlib.py
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 375146c..99ca938 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -4,6 +4,7 @@
#
# Distributed under the terms of the MIT license.
#
+import itertools
import re
from collections import OrderedDict, namedtuple
from collections.abc import Sequence
@@ -21,6 +22,7 @@
List,
Pattern,
Tuple,
+ pairwise,
)
from pywikibot.backports import OrderedDict as OrderedDictType
from pywikibot.backports import Sequence as SequenceType
@@ -1014,14 +1016,11 @@
sections = []
if headings:
# Assign them their contents
- for i, heading in enumerate(headings):
- try:
- next_heading = headings[i + 1]
- except IndexError:
- content = text[heading.end:]
- else:
- content = text[heading.end:next_heading.start]
+ for heading, next_heading in pairwise(headings):
+ content = text[heading.end:next_heading.start]
sections.append(Section(heading.text, content))
+ last = headings[-1]
+ sections.append(Section(last.text, text[last.end:]))
return sections
@@ -1878,7 +1877,7 @@
else:
params = params.split('|')
- numbered_param_identifiers = iter(range(1, len(params) + 1))
+ numbered_param_identifiers = itertools.count(1)
params = OrderedDict(
arg.split('=', 1)
To view, visit change 935688. To unsubscribe, or for help writing mail filters, visit settings.