Пример #1
0
def raise_if_tag_is_invalid(tag: str) -> None:
    """Raise a :class:`htmap.exceptions.InvalidTag` if the ``tag`` contains any invalid characters."""
    if len(tag) < 1:
        raise exceptions.InvalidTag("The tag must be a non-empty string")
    invalid_chars = set(tag).intersection(INVALID_TAG_CHARACTERS)
    if len(invalid_chars) != 0:
        raise exceptions.InvalidTag(f'These characters in tag {tag} are not valid: {invalid_chars}')
Пример #2
0
    'stick',
    'stream',
    'table',
    'tooth',
    'town',
    'tub',
    'year',
    'zebra',
)


def random_tag() -> str:
    existing_tags = set(get_tags())

    for attempt in range(50):
        adj1, adj2 = random.sample(ADJECTIVES, k=2)
        noun = random.choice(NOUNS)
        tag = f'{adj1}-{adj2}-{noun}'
        if tag not in existing_tags:
            return tag

    options = string.ascii_letters + string.digits
    for attempt in range(1_000_000):
        tag = ''.join(random.choices(options, k=6))
        if tag not in existing_tags:
            return tag

    raise exceptions.InvalidTag(
        'Could not find an unused random tag (try cleaning your transient maps)'
    )