From aa627faa502aa101537100568f606fbea2c5e44e Mon Sep 17 00:00:00 2001 From: vahid Date: Sun, 15 Dec 2024 03:45:53 +0330 Subject: [PATCH] feat: Add special characters to `random_string` util If you pass argument: special=True, special characters will be added to the generated random string. Close #1177 --- tutor/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tutor/utils.py b/tutor/utils.py index cdb082f38a..e8193e9bb4 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -66,9 +66,12 @@ def ensure_directory_exists(path: str) -> None: os.makedirs(path) -def random_string(length: int) -> str: +def random_string(length: int, special=False) -> str: + characters = string.ascii_letters + string.digits + if special: + characters += re.sub(r'[\'"]', '', string.punctuation) return "".join( - [random.choice(string.ascii_letters + string.digits) for _ in range(length)] + [random.choice(characters) for _ in range(length)] )