random_tool — A module to generate things randomly.

NIWLittleUtils.random_tool.get_random_string(length=36, allowed_chars='complex')[source]

Returns a securely generated random string.

Generate a securely random string with length or allowed characters you want.

If allowed_chars is ignored, it’s string.ascii_letters + string.digits + string.punctuation by default (We use Python builtin library string to set value). You can set it to following string values or any allowed_chars else.

Value Meaning
'complex' string.ascii_letters + string.digits + string.punctuation
'middle' string.ascii_letters + string.digits + '-._~'
'simple' string.ascii_letters + string.digits
'lowercase' string.ascii_lowercase
'uppercase' string.ascii_uppercase
'digits' string.digits
>>> get_random_string(8) 
'Qi4Y_fA5'
>>> get_random_string(5, '0123456789abcdef') 
'06aef'
>>> get_random_string(5, 'digits') 
'05154'
Parameters:
  • length (int) – Random string length.
  • allowed_chars (str) – Character allowed to use to generate. There has 6 default setting (case-insensitive).
Returns:

A securely generated random string.

Return type:

str