Coverage for src/fastoai/models/_utils.py: 100%
11 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-06 09:34 +0800
« prev ^ index » next coverage.py v7.6.8, created at 2024-12-06 09:34 +0800
1import secrets
2from datetime import UTC, datetime
3from string import ascii_letters, digits
6def get_random_string(length, allowed_chars=ascii_letters + digits):
7 """Return a securely generated random string.
9 The bit length of the returned value can be calculated with the formula:
10 log_2(len(allowed_chars)^length)
12 For example, with default `allowed_chars` (26+26+10), this gives:
13 * length: 12, bit length =~ 71 bits
14 * length: 22, bit length =~ 131 bits
15 """
16 return "".join(secrets.choice(allowed_chars) for _ in range(length))
19def random_id_with_prefix(prefix: str):
20 def _inner():
21 return f"{prefix}{get_random_string(24)}" # (26 * 2 + 10) ^ 24 > uuid4
23 return _inner
26def now():
27 return datetime.now(UTC)