-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptimizations.py
More file actions
42 lines (41 loc) · 1.94 KB
/
Copy pathoptimizations.py
File metadata and controls
42 lines (41 loc) · 1.94 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
# src/optimizations.py
import re
def apply_optimizations(options, text):
if options["remove_comments"].get():
text = re.sub(r'#.*', '', text)
text = re.sub(r'"""[\s\S]*?"""|\'\'\'[\s\S]*?\'\'\'', '', text)
if options["remove_docstrings"].get():
text = re.sub(r'^[\r\n\s]*("""|\'\'\').*?\1', '', text, count=1, flags=re.DOTALL)
if options["remove_blank_lines"].get():
text = "\n".join(line for line in text.splitlines() if line.strip())
if options["remove_extra_spaces"].get():
text = re.sub(r'[ \t]+', ' ', text)
if options["single_line_mode"].get():
text = text.replace("\n", "⏎")
if options["shorten_keywords"].get():
rep = {
"def ": "d ", "return ": "r ", "import ": "i ", "from ": "f ", "as ": "a ",
"if ": "if", "class ": "c ", "lambda ": "λ "
}
for k, v in rep.items():
text = text.replace(k, v)
if options["replace_booleans"].get():
text = text.replace("True", "1").replace("False", "0").replace("None", "~")
if options["use_short_operators"].get():
text = text.replace("==", "≡").replace("!=", "≠")
text = text.replace(" and ", "∧").replace(" or ", "∨")
text = re.sub(r'\band\b', '∧', text)
text = re.sub(r'\bor\b', '∨', text)
if options["remove_type_hints"].get():
text = re.sub(r':\s*[^=\n\->]+', '', text)
text = re.sub(r'->\s*[^:\n]+', '', text)
if options["minify_structures"].get():
text = re.sub(r',\s+', ',', text)
text = re.sub(r':\s+', ':', text)
if options["unicode_shortcuts"].get():
text = re.sub(r'\bnot\s+in\b', '∉', text)
text = re.sub(r'\bin\b', '∈', text)
text = text.replace(" not in ", "∉").replace(" in ", "∈")
if options["shorten_print"].get():
text = re.sub(r'print\s*\(', 'p(', text)
return text.rstrip() + "\n" if text.strip() else text # فقط یه \n آخر