36 lines
911 B
Python
36 lines
911 B
Python
# Copyright (C) 2022 Cetmix OÜ
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
|
from random import choices
|
|
|
|
CHARS = "23456789acefhjkmnprtvwxyz"
|
|
|
|
|
|
def generate_random_id(sections=1, population=4, separator="-"):
|
|
"""Generates random id
|
|
eg 'ahj2-jer83'
|
|
|
|
Args:
|
|
sections (int, optional): number of sections. Defaults to 1.
|
|
population (int, optional): number of symbols per section. Defaults to 4.
|
|
separator (str, optional): section separator. Defaults to "-".
|
|
|
|
Returns:
|
|
Str: generated id
|
|
"""
|
|
if sections < 1 or population < 0:
|
|
return None
|
|
|
|
def get_section():
|
|
return "".join(choices(CHARS, k=population))
|
|
|
|
# Single section
|
|
if sections == 1:
|
|
return get_section()
|
|
|
|
# Multiple sections
|
|
result = []
|
|
for _ in range(sections):
|
|
result.append(get_section())
|
|
|
|
return separator.join(result)
|