split functionality file across new module as was getting too long on its own. All functions remian the same, but imports will need to be updated.
This commit is contained in:
23
functionality/naming.py
Normal file
23
functionality/naming.py
Normal file
@ -0,0 +1,23 @@
|
||||
"""
|
||||
This file contains functions for dynamic naming of objects.
|
||||
|
||||
Author(s): David Marchant
|
||||
"""
|
||||
|
||||
from typing import List
|
||||
from random import SystemRandom
|
||||
|
||||
from core.correctness.vars import CHAR_LOWERCASE, CHAR_UPPERCASE
|
||||
|
||||
|
||||
#TODO Make this guaranteed unique
|
||||
def generate_id(prefix:str="", length:int=16, existing_ids:List[str]=[],
|
||||
charset:str=CHAR_UPPERCASE+CHAR_LOWERCASE, attempts:int=24):
|
||||
random_length = max(length - len(prefix), 0)
|
||||
for _ in range(attempts):
|
||||
id = prefix + ''.join(SystemRandom().choice(charset)
|
||||
for _ in range(random_length))
|
||||
if id not in existing_ids:
|
||||
return id
|
||||
raise ValueError(f"Could not generate ID unique from '{existing_ids}' "
|
||||
f"using values '{charset}' and length of '{length}'.")
|
Reference in New Issue
Block a user