backports — Python Backports#

This module contains backports to support older Python versions.

Caution

This module is not part of the public pywikibot API. Breaking changes may be made at any time, and the module is not subject to deprecation requirements.

Changed in version 10.0: This module is ‘private’.

backports.batched(iterable, n, *, strict=False)[source]#

Batch data from the iterable into tuples of length n.

Note

The last batch may be shorter than n if strict is True or raise a ValueError otherwise.

Example:

>>> i = batched(range(25), 10)
>>> print(next(i))
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> print(next(i))
(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
>>> print(next(i))
(20, 21, 22, 23, 24)
>>> print(next(i))
Traceback (most recent call last):
 ...
StopIteration
>>> list(batched('ABCD', 2))
[('A', 'B'), ('C', 'D')]
>>> list(batched('ABCD', 3, strict=False))
[('A', 'B', 'C'), ('D',)]
>>> list(batched('ABCD', 3, strict=True))
Traceback (most recent call last):
 ...
ValueError: batched(): incomplete batch

See also

itertools.batched, backported from Python 3.12.

Added in version 8.2.

Changed in version 9.0: Added strict option, backported from Python 3.13

Parameters:
  • n (int) – How many items of the iterable to get in one chunk

  • strict (bool) – raise a ValueError if the final batch is shorter than n.

Raises:
  • ValueError – batched(): incomplete batch

  • TypeErrorn cannot be interpreted as an integer

Return type:

Generator[tuple]

backports.pairwise(iterable)[source]#

Return successive overlapping pairs taken from the input iterable.

See also

itertools.pairwise, backported from Python 3.10.

Added in version 7.6.

backports.removeprefix(string, prefix)[source]#

Remove prefix from a string or return a copy otherwise.

>>> removeprefix('TestHook', 'Test')
'Hook'
>>> removeprefix('BaseTestCase', 'Test')
'BaseTestCase'

See also

str.removeprefix, backported from Python 3.9.

Added in version 5.4.

Parameters:
  • string (str)

  • prefix (str)

Return type:

str

backports.removesuffix(string, suffix)[source]#

Remove suffix from a string or return a copy otherwise.

>>> removesuffix('MiscTests', 'Tests')
'Misc'
>>> removesuffix('TmpDirMixin', 'Tests')
'TmpDirMixin'

See also

str.removesuffix, backported from Python 3.9.

Added in version 5.4.

Parameters:
  • string (str)

  • suffix (str)

Return type:

str