Skip to content

prefect.utilities.names

generate_slug

Generates a random slug.

Parameters:

Name Type Description Default
- n_words (int

the number of words in the slug

required
Source code in prefect/utilities/names.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def generate_slug(n_words: int) -> str:
    """
    Generates a random slug.

    Args:
        - n_words (int): the number of words in the slug
    """
    words = coolname.generate(n_words)

    # regenerate words if they include ignored words
    while IGNORE_LIST.intersection(words):
        words = coolname.generate(n_words)

    return "-".join(words)

obfuscate

Obfuscates any data type's string representation. See obfuscate_string.

Source code in prefect/utilities/names.py
45
46
47
48
49
50
51
52
def obfuscate(s: Any, show_tail=False) -> str:
    """
    Obfuscates any data type's string representation. See `obfuscate_string`.
    """
    if s is None:
        return OBFUSCATED_PREFIX + "*" * 4

    return obfuscate_string(str(s), show_tail=show_tail)

obfuscate_string

Obfuscates a string by returning a new string of 8 characters. If the input string is longer than 10 characters and show_tail is True, then up to 4 of its final characters will become final characters of the obfuscated string; all other characters are "*".

"abc" -> "*" "abcdefgh" -> "*" "abcdefghijk" -> "*k" "abcdefghijklmnopqrs" -> "****pqrs"

Source code in prefect/utilities/names.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def obfuscate_string(s: str, show_tail=False) -> str:
    """
    Obfuscates a string by returning a new string of 8 characters. If the input
    string is longer than 10 characters and show_tail is True, then up to 4 of
    its final characters will become final characters of the obfuscated string;
    all other characters are "*".

    "abc"      -> "********"
    "abcdefgh" -> "********"
    "abcdefghijk" -> "*******k"
    "abcdefghijklmnopqrs" -> "****pqrs"
    """
    result = OBFUSCATED_PREFIX + "*" * 4
    # take up to 4 characters, but only after the 10th character
    suffix = s[10:][-4:]
    if suffix and show_tail:
        result = f"{result[:-len(suffix)]}{suffix}"
    return result