PyBackport: Builtins¶
py_back.builtins ¶
Module to backport 'builtins' classes depending on the system python.
py_back allows using the builtins module just as the original.
# Python version lower than 3.9
>>> from py_back.builtins import str
>>> my_string = str("Hello world!")
>>> print(my_string.removesuffix("!"))
Hello world
Note
Python builtins don't require to be imported, but the backported builtins do not
follow this rule. This results in the inconvenience that backported instances must
be defined with the constructor provided by py_back, even if they interact with
other not backported builtins.
dict ¶
Bases: builtins.dict
Backport for 'dict' class.
dict()-> new empty dictionary.dict(mapping)-> new dictionary initialized from a mapping object's (key, value) pairs.dict(iterable)-> new dictionary initialized as if.dict(**kwargs)-> new dictionary initialized with thename=valuepairs in the keyword argument list. E.G.:dict(one=1, two=2)
Backports
Python 3.9:
Operators d | other
and d |= other.
__or__ ¶
__or__(other: builtins.dict) -> dict
Return self | other, which is equivalent to self.update(other).
Create a new dictionary with the merged keys and values of d and other,
which must both be dictionaries. The values of other take priority when d
and other share keys.
The operation other | d is also supported.
Notes
The dictionary defined as other does not need to be a backported instance.
Examples:
>>> from py_back.builtins import dict
>>> dict({"key_0": 1}) | {"key_1": 2}
{"key_0": 1, "key_1": 2}
str ¶
Bases: builtins.str
Backport for str class.
str(object='')->strstr(bytes_or_buffer[, encoding[, errors]])->str
Backports
Python 3.9:
Methods str.removeprefix(prefix, /) and str.removesuffix(suffix, /)
removeprefix ¶
removeprefix(prefix: str) -> str
Backport logic to remove prefix from str.
If the string starts with the prefix string, return string[len(prefix):].
Otherwise, return a copy of the original string.
Examples:
from py_back.builtins import str
>>> str('TestHook').removeprefix('Test')
'Hook'
>>> str('BaseTestCase').removeprefix('Test')
'BaseTestCase'
removesuffix ¶
removesuffix(suffix: str) -> str
Backport logic to remove suffix from str.
If the string ends with the suffix string and that suffix is not empty,
return string[:-len(suffix)]. Otherwise, return a copy of the original string.
Examples:
from py_back.builtins import str
>>> str('MiscTests').removesuffix('Tests')
'Misc'
>>> str('TmpDirMixin').removesuffix('Tests')
'TmpDirMixin'