frostfs-testlib/src/frostfs_testlib/utils/string_utils.py
a.berezin a04eba8aec
Some checks failed
DCO action / DCO (pull_request) Has been cancelled
[#302] Autoadd marks for frostfs
Signed-off-by: a.berezin <a.berezin@yadro.com>
2024-10-11 12:23:32 +03:00

50 lines
1.4 KiB
Python

import itertools
import random
import re
import string
from datetime import datetime
ONLY_ASCII_LETTERS = string.ascii_letters
DIGITS_AND_ASCII_LETTERS = string.ascii_letters + string.digits
NON_DIGITS_AND_LETTERS = string.punctuation
# if unique_name is called multiple times within the same microsecond, append 0-4 to the name so it surely unique
FUSE = itertools.cycle(range(5))
def unique_name(prefix: str = "", postfix: str = ""):
"""
Generate unique short name of anything with prefix.
This should be unique in scope of multiple runs
Args:
prefix: prefix for unique name generation
Returns:
unique name string
"""
return f"{prefix}{hex(int(datetime.now().timestamp() * 1000000))}{next(FUSE)}{postfix}"
def random_string(length: int = 5, source: str = ONLY_ASCII_LETTERS):
"""
Generate random string from source letters list
Args:
length: length for generated string
source: source string with letters for generate random string
Returns:
(str): random string with len == length
"""
return "".join(random.choice(source) for i in range(length))
def is_str_match_pattern(error: Exception, status_pattern: str) -> bool:
"""
Determines whether exception matches specified status pattern.
We use re.search() to be consistent with pytest.raises.
"""
match = re.search(status_pattern, str(error))
return match is not None