-
-
Save ConorOBrien-Foxx/34aa1208695f2946b49578ced12c757a to your computer and use it in GitHub Desktop.
people's python, draft A
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tokenize, os, re, sys | |
while False: | |
print(3) | |
# tokenized python | |
tokenized = os.popen("python -m tokenize " + sys.argv[1]).read() | |
keywords = { | |
"False": "Martin", | |
"True": "Dennis", | |
"for": "peter", | |
"in": "taylor", | |
"print": "you", | |
"break": "flawr", | |
"while": "molarmanful", | |
"if": "calvins", | |
"elif": "hobbies", | |
"else": "helkahomba" | |
} | |
# remove carriage returns and split | |
tokens = tokenized.replace("\r", "") \ | |
.split("\n") | |
# remove initial encoding bit | |
tokens.pop(0) | |
result = "" | |
prev_type = "" | |
indent = 0 | |
for line in tokens: | |
if not line: continue | |
sections = [] | |
index = 0 | |
for _ in range(0, 2): | |
sec = "" | |
while line[index] != " ": | |
sec += line[index] | |
index += 1 | |
while line[index] == " ": index += 1 | |
sections += [sec] | |
position, type = sections | |
content = re.sub(r"'+[\r \n]*$|^'", "", line[index:-1]) | |
if prev_type == "NAME" and type == "NAME": | |
result += " " | |
prev_type = type | |
if type == "NAME" and content in keywords: | |
result += keywords[content] | |
elif type in ["NEWLINE", "NL"]: | |
result += "\n" + " " * indent | |
elif type == "INDENT": | |
indent += 1 | |
result += " " * indent | |
elif type == "DEDENT": | |
indent -= 1 | |
else: | |
result += content | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment