Coverage for src/fastoai/models/_utils.py: 100%

1 import secrets

2 from datetime import UTC, datetime

3 from string import ascii_letters, digits

4

5

6 def get_random_string(length, allowed_chars=ascii_letters + digits):

7 """Return a securely generated random string.

8

9 The bit length of the returned value can be calculated with the formula:

10 log_2(len(allowed_chars)^length)

11

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))

17

18

19 def random_id_with_prefix(prefix: str):

20 def _inner():

21 return f"{prefix}{get_random_string(24)}" # (26 * 2 + 10) ^ 24 > uuid4

22

23 return _inner

24

25

26 def now():

27 return datetime.now(UTC)