blob: 6f7c10337a4e78ea8341c798ef8c2e0a465c281b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
try:
import secrets as random
except ImportError:
import random
import string
POP = string.ascii_letters + string.digits
def generate(length: int = 1) -> str:
"""Generates a random string of alphanumeric characters of the given length
If no length is specified, a single character is returned.
On Python 3.5, this string is pseudo-randomly generated using the random
module. With 3.6 and later, the randomness is generated with the secrets
module, making the randomization cryptographically strong.
Args:
length (:obj:`int`, optional): Desired string length. Defaults to 1.
Returns:
str: A pseudo-random alphanumeric string.
Examples:
>>> print(alphanum.generate())
'G'
>>> print(alphanum.generate(10))
'a93jfDjdA0'
"""
return ''.join(random.SystemRandom().choice(POP) for i in range(length))
|