Coverage for /usr/local/lib/python3.12/site-packages/prefect/utilities/names.py: 28%
19 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 13:38 +0000
1from typing import Any 1a
3import coolname # type: ignore # the version after coolname 2.2.0 should have stubs. 1a
5OBFUSCATED_PREFIX = "****" 1a
7IGNORE_LIST = { 1a
8 "sexy",
9 "demonic",
10 "kickass",
11 "heretic",
12 "godlike",
13 "booby",
14 "chubby",
15 "gay",
16 "sloppy",
17 "funky",
18 "juicy",
19 "beaver",
20 "curvy",
21 "fat",
22 "flashy",
23 "flat",
24 "thick",
25 "nippy",
26}
29def generate_slug(n_words: int) -> str: 1a
30 """
31 Generates a random slug.
33 Args:
34 - n_words (int): the number of words in the slug
35 """
36 words = coolname.generate(n_words)
38 # regenerate words if they include ignored words
39 while IGNORE_LIST.intersection(words):
40 words = coolname.generate(n_words)
42 return "-".join(words)
45def obfuscate(s: Any, show_tail: bool = False) -> str: 1a
46 """
47 Obfuscates any data type's string representation. See `obfuscate_string`.
48 """
49 if s is None:
50 return OBFUSCATED_PREFIX + "*" * 4
52 return obfuscate_string(str(s), show_tail=show_tail)
55def obfuscate_string(s: str, show_tail: bool = False) -> str: 1a
56 """
57 Obfuscates a string by returning a new string of 8 characters. If the input
58 string is longer than 10 characters and show_tail is True, then up to 4 of
59 its final characters will become final characters of the obfuscated string;
60 all other characters are "*".
62 "abc" -> "********"
63 "abcdefgh" -> "********"
64 "abcdefghijk" -> "*******k"
65 "abcdefghijklmnopqrs" -> "****pqrs"
66 """
67 result = OBFUSCATED_PREFIX + "*" * 4
68 # take up to 4 characters, but only after the 10th character
69 suffix = s[10:][-4:]
70 if suffix and show_tail:
71 result = f"{result[: -len(suffix)]}{suffix}"
72 return result