backports
— Python Backports#
This module contains backports to support older Python versions.
- backports.batched(iterable, n)[source]#
Batch data from the iterable into tuples of length n.
Note
The last batch may be shorter than n.
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
See also
itertools.batched, backported from Python 3.12.
New in version 8.2.
- Parameters:
n (int) – How many items of the iterable to get in one chunk
- Return type:
Generator[tuple, None, None]
- class backports.nullcontext(enter_result=None)[source]#
Bases:
object
Context manager that does no additional processing.
See also
contextlib.nullcontext, backported from Python 3.7.
- Parameters:
enter_result (Any) –
- backports.pairwise(iterable)[source]#
Return successive overlapping pairs taken from the input iterable.
See also
itertools.pairwise, backported from Python 3.10.
New 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.
New 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.
New in version 5.4.
- Parameters:
string (str) –
suffix (str) –
- Return type:
str