add generate_random_string

This commit is contained in:
Daniel 2023-12-10 18:00:52 +01:00
parent ebac7f49fd
commit a8479a56e3

View file

@ -1,3 +1,5 @@
import random
import string
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
@ -35,3 +37,12 @@ def rmtree(root_dir: Path):
child.unlink()
root_dir.rmdir()
def generate_random_string(length: int, punctuation=False) -> str:
"""returns a random string of the given length"""
characters = string.ascii_letters + string.digits
if punctuation:
characters += string.punctuation
random_string = "".join(random.choice(characters) for _ in range(length))
return random_string