-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname_registry.py
38 lines (29 loc) · 1.04 KB
/
name_registry.py
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
32
33
34
35
36
37
38
"""Whenever a new object is made, register its name.
The principle purpose of this is for debugging. And it has helped me many a time!
"""
import random
NAME_REGISTRY = set()
def get_unique_name(base):
if base not in NAME_REGISTRY: return base
return f"{base}_{len(NAME_REGISTRY)}"
def register_unique_name(base):
unique_name = get_unique_name(base)
register_name(unique_name)
return unique_name
def register_name(name):
# should be called in init function of any object.
if name in NAME_REGISTRY:
raise ValueError(f"Duplicate name '{name}' can't be added to registry!")
NAME_REGISTRY.add(name)
def random_string():
return ''.join(random.choices('qwertyuiopasdfghjklzxcvbnm', k=8))
def reset_name_registry():
global NAME_REGISTRY
NAME_REGISTRY = set()
ADJECTIVES = "Thoraic Magnificent Ineffable".split()
NOUNS = "Teapot Spoon Rock".split()
ANIMALS = "Badger Wombat Ant Human".split()
def get_glorious_name():
name = f"{random.choice(ADJECTIVES)} {random.choice(NOUNS)}-{random.choice(ANIMALS)}"
register_name(name)
return name