String manipulation
import logging
import re
LOGGER = logging.getLogger(__name__)
Do a series of global replacements on a string.
def replace(text: str, *pairs: str) -> str:
while pairs:
text = pairs[1].join(text.split(pairs[0]))
pairs = pairs[2:]
return text
Omit part of a string if needed to make it fit in a maximum length.
def cram(text: str, maxlen: int) -> str:
if len(text) > maxlen:
pre = max(0, (maxlen - 3) // 2)
post = max(0, maxlen - 3 - pre)
return text[:pre] + "..." + text[len(text) - post :]
return text
_re_stripid = re.compile(r" at 0x[0-9a-f]{6,16}(>+)$", re.IGNORECASE)
Remove the hexadecimal id from a Python object representation.
def stripid(text: str) -> str:
The behaviour of %p is implementation-dependent in terms of case.
return _re_stripid.sub(r"\1", text)