-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizzy.py
More file actions
94 lines (66 loc) · 2.45 KB
/
Copy pathfizzy.py
File metadata and controls
94 lines (66 loc) · 2.45 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Fizzy - Fizz and Defizz your text, by Kreusada."""
CHARS = [
"𖫩", "᧐", "O", "꯰", "°", "О", "о", "ₒ", "Ⓞ", "𐓂",
"𐓪", "𐔖", "𐖮", "𐤬", "𛱄", "ⓞ", "Ⲟ", "ⲟ", "ꓳ", "ꝏ",
"Ꝏ", "O", "o", "𐊫", "𐌏", "᳃", "߀", "०", "০", "*",
"੦", "૦", "🇴", "𑵐", "᛫", "୦", "௦", "౦", "೦", "൦",
"๐", "໐", "၀", "᥆", "᠐", "0", "᪀", "᪐", "᭐", "᮰",
"᱀", "᱐", "⁰", "₀", "〇", "꣐", "꤀", "꧐", "꩐", ".",
"𐒠", "𐴰", "0", "𑃰"
]
class Error(Exception):
pass
def fizz(text: str):
"""Make your text fizzy."""
ret = []
for char in text:
code = ord(char)
if code in range(32):
raise Error("Control characters are forbidden")
current = ""
while code != 0:
code, rem = divmod(code, 64)
current += CHARS[rem]
ret.append(current)
return "o".join(ret)
def defizz(text: str) -> str:
"""Defizz your text. Valid fizzy characters are expected."""
if not isfizzy(text):
raise Error("Given text isn't fizzy.")
ret = []
for chars in text.split("o"):
rems = [CHARS.index(char) for char in chars]
n = 0
for rem in reversed(rems):
n = (n * 64) + rem
if n in range(32):
raise Error("Forbidden control character detected during defizzing")
ret.append(chr(n))
return "".join(ret)
def isfizzy(text: str) -> bool:
"""Check whether the text is fizzy."""
return (
not text.startswith("o")
and not text.endswith("o")
and all(c in CHARS for c in text.replace("o", ""))
)
def main():
import argparse
import sys
parser = argparse.ArgumentParser(description=__doc__)
subparsers = parser.add_subparsers(title="Commands", dest="command")
fizz_parser = subparsers.add_parser("fizz", help=fizz.__doc__)
fizz_parser.add_argument("text", type=str, help="Text to fizz", nargs="+")
defizz_parser = subparsers.add_parser("defizz", help=defizz.__doc__)
defizz_parser.add_argument("text", type=str, help="Text to defizz")
args = parser.parse_args()
if not args.command:
return parser.print_help()
arg = " ".join(args.text) if args.command == "fizz" else args.text
try:
print(globals()[args.command](arg))
except Error as e:
print("Error:", e)
sys.exit(2)
if __name__ == "__main__":
main()