-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabet.py
73 lines (55 loc) · 1.97 KB
/
alphabet.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# GNU AFFERO GENERAL PUBLIC LICENSE
# Version 3, 19 November 2007
from string import ascii_letters
from random import choice
def update(d: dict, func: str, attr: str) -> None:
""" Updates dictionary with new char instances
Tries the complete "attr" (attribute) (either
__doc__ or __name__) from "func" (function) to find
alphabet character instances, updating "d" (dict)
in-place.
Args:
d : An alphabet dictionary instance
func : The selected function
attr : Attribute of the function
Returns:
None
"""
try:
string = eval(f"{func}.{attr}")
for index in range(len(string)):
char = string[index]
if char in d:
d[char] = d[char].copy() + \
[f"{func}.{attr}[{index}]"]
except:
print(f"{func} : has no .{attr}")
def choose(d: dict, c: str) -> str:
""" Chooses a random generator for a char
Uses random choice or yields key if no option
was found before.
Args:
d : An alphabet dictionary instance
c : Character to select
Returns:
Random generator function or character key
"""
return choice(d[c]) if d[c] else c
def alphabet(chars: list = []) -> dict:
""" Generates a random ascii letters alphabet using
Uses .__doc__ and .__name__ instances from __builtins__
to generate a custom dictionary with each character
from ascii.letters.
Args:
chars (optional) : Additional character list
Returns:
An alphabet dictionary
"""
# Build initial dictionary with ascii letters and additional chars
alphabet = dict.fromkeys(ascii_letters, []) | dict.fromkeys(chars, [])
# Update all function instances from __builtins__
for f in __builtins__.__dict__:
update(alphabet, f, "__name__")
update(alphabet, f, "__doc__")
# Choose a random instance for each char
return {char: choose(alphabet, char) for char, _ in alphabet.items()}