X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=string_utils.py;h=6f3cc90ed46f5c238b0887848c1cf7504ec3bcc0;hb=65e6f781dffb268be6ef0a015f4cc89b57b62a5e;hp=bae59068b9063cece6fe401f649401e07efa138d;hpb=ffed473528c4feb836253758e86f7839af98f57b;p=python_utils.git diff --git a/string_utils.py b/string_utils.py index bae5906..6f3cc90 100644 --- a/string_utils.py +++ b/string_utils.py @@ -72,9 +72,7 @@ URLS_RE = re.compile(r"({})".format(URLS_RAW_STRING), re.IGNORECASE) ESCAPED_AT_SIGN = re.compile(r'(?!"[^"]*)@+(?=[^"]*")|\\@') -EMAILS_RAW_STRING = ( - r"[a-zA-Z\d._\+\-'`!%#$&*/=\?\^\{\}\|~\\]+@[a-z\d-]+\.?[a-z\d-]+\.[a-z]{2,4}" -) +EMAILS_RAW_STRING = r"[a-zA-Z\d._\+\-'`!%#$&*/=\?\^\{\}\|~\\]+@[a-z\d-]+\.?[a-z\d-]+\.[a-z]{2,4}" EMAIL_RE = re.compile(r"^{}$".format(EMAILS_RAW_STRING)) @@ -84,13 +82,9 @@ CAMEL_CASE_TEST_RE = re.compile(r"^[a-zA-Z]*([a-z]+[A-Z]+|[A-Z]+[a-z]+)[a-zA-Z\d CAMEL_CASE_REPLACE_RE = re.compile(r"([a-z]|[A-Z]+)(?=[A-Z])") -SNAKE_CASE_TEST_RE = re.compile( - r"^([a-z]+\d*_[a-z\d_]*|_+[a-z\d]+[a-z\d_]*)$", re.IGNORECASE -) +SNAKE_CASE_TEST_RE = re.compile(r"^([a-z]+\d*_[a-z\d_]*|_+[a-z\d]+[a-z\d_]*)$", re.IGNORECASE) -SNAKE_CASE_TEST_DASH_RE = re.compile( - r"([a-z]+\d*-[a-z\d-]*|-+[a-z\d]+[a-z\d-]*)$", re.IGNORECASE -) +SNAKE_CASE_TEST_DASH_RE = re.compile(r"([a-z]+\d*-[a-z\d-]*|-+[a-z\d]+[a-z\d-]*)$", re.IGNORECASE) SNAKE_CASE_REPLACE_RE = re.compile(r"(_)([a-z\d])") @@ -107,9 +101,7 @@ CREDIT_CARDS = { JSON_WRAPPER_RE = re.compile(r"^\s*[\[{]\s*(.*)\s*[\}\]]\s*$", re.MULTILINE | re.DOTALL) -UUID_RE = re.compile( - r"^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$", re.IGNORECASE -) +UUID_RE = re.compile(r"^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$", re.IGNORECASE) UUID_HEX_OK_RE = re.compile( r"^[a-f\d]{8}-?[a-f\d]{4}-?[a-f\d]{4}-?[a-f\d]{4}-?[a-f\d]{12}$", @@ -126,9 +118,7 @@ ANYWHERE_IP_V6_RE = re.compile(r"([a-z\d]{0,4}:){7}[a-z\d]{0,4}", re.IGNORECASE) MAC_ADDRESS_RE = re.compile(r"^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$", re.IGNORECASE) -ANYWHERE_MAC_ADDRESS_RE = re.compile( - r"([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", re.IGNORECASE -) +ANYWHERE_MAC_ADDRESS_RE = re.compile(r"([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", re.IGNORECASE) WORDS_COUNT_RE = re.compile(r"\W*[^\W_]+\W*", re.IGNORECASE | re.MULTILINE | re.UNICODE) @@ -418,9 +408,7 @@ def add_thousands_separator(in_str: str, *, separator_char=',', places=3) -> str if isinstance(in_str, numbers.Number): in_str = f'{in_str}' if is_number(in_str): - return _add_thousands_separator( - in_str, separator_char=separator_char, places=places - ) + return _add_thousands_separator(in_str, separator_char=separator_char, places=places) raise ValueError(in_str) @@ -1368,11 +1356,13 @@ def make_contractions(txt: str) -> str: # Special cases: can't, shan't and won't. txt = re.sub(r'\b(can)\s*no(t)\b', r"\1'\2", txt, count=0, flags=re.IGNORECASE) + txt = re.sub(r'\b(sha)ll\s*(n)o(t)\b', r"\1\2'\3", txt, count=0, flags=re.IGNORECASE) txt = re.sub( - r'\b(sha)ll\s*(n)o(t)\b', r"\1\2'\3", txt, count=0, flags=re.IGNORECASE - ) - txt = re.sub( - r'\b(w)ill\s*(n)(o)(t)\b', r"\1\3\2'\4", txt, count=0, flags=re.IGNORECASE + r'\b(w)ill\s*(n)(o)(t)\b', + r"\1\3\2'\4", + txt, + count=0, + flags=re.IGNORECASE, ) for first_list, second_list in first_second: @@ -1590,9 +1580,7 @@ def chunk(txt: str, chunk_size): yield txt[x : x + chunk_size] -def to_bitstring( - txt: str, *, delimiter='', encoding='utf-8', errors='surrogatepass' -) -> str: +def to_bitstring(txt: str, *, delimiter='', encoding='utf-8', errors='surrogatepass') -> str: """Encode txt and then chop it into bytes. Note: only bitstrings with delimiter='' are interpretable by from_bitstring.