A while ago, a user on Programming Puzzles and Code Golf had an idea:
How about a language where all of the core commands are PPCG usernames? -- Helka Homba
A list was made of username/(keyword|builtin function) pairs, but nobody really wanted to actually make the code, so it kinda went on hold.
Recently (read: yesterday) another user suggested using the tokenize
module, so I did. With his help, I finished some py2 code for replacing the names.
Here is my code:
import tokenize
import ast
import sys
def handle_token(type, token, (srow, scol), (erow, ecol), line):
# Return the info about the tokens, if it's a NAME token then replace it
if tokenize.tok_name[type] == "NAME":
token = token_names.get(token, token)
return (type, token, (srow, scol), (erow, ecol), line)
def run(assignments="assignments.txt",open_from="peoples.txt"):
with open(assignments, "r") as f:
# Read the replacements into token_names
global token_names
token_names = ast.literal_eval(f.read())
with open(open_from) as source:
# Get the tokenized version of the input, replace it, and untokenize into pretty output
tokens = tokenize.generate_tokens(source.readline)
handled_tokens = (handle_token(*token) for token in tokens)
output = tokenize.untokenize(handled_tokens)
with open(open_from[:-4]+"-output.txt",'w') as outfile:
# Write to the output file
outfile.write(output)
return output
if __name__ == "__main__":
if len(sys.argv) > 1:
if len(sys.argv) > 2:
try:exec run(assignments=sys.argv[1],open_from=sys.argv[2])
except:pass
else:
try:exec run(assignments=sys.argv[1])
except:pass
else:
try:exec run()
except:pass
This is the content of main.py
. It uses assignments.txt
, which is a file containing a python dict of the replacement pairs.
{"Martin":"False",
"Geobits":"None",
"Dennis":"True",
"adnan":"and",
"rainbolt":"as",
"buttner":"assert",
"flawr":"break",
"aditsu":"class",
"katenkyo":"continue",
"quill":"def",
"nathan":"del",
"hobbies":"elif",
"helkahomba":"else",
"irk":"except",
"ender":"finally",
"peter":"for",
"conor":"from",
"gnibbler":"global",
"calvins":"if",
"obrien":"import",
"taylor":"in",
"fryamtheeggman":"is",
"starman":"lambda",
"sp3000":"nonlocal",
"phinotpi":"not",
"xnor":"or",
"maltysen":"pass",
"mego":"raise",
"alex":"return",
"easterly":"try",
"molarmanful":"while",
"minxomat":"with",
"optimizer":"yield",
"mbomb007":"abs",
"digital":"all",
"trauma":"any",
"asciionly":"ascii",
"zyabin":"bin",
"bkul":"bool",
"chris":"chr",
"jesteryoung":"classmethod",
"elendia":"enumerate",
"gcampbell":"eval",
"fatalize":"filter",
"sandbox":"help",
"zgarb":"id",
"phase":"input",
"loovjo":"int",
"minibits":"issubclass",
"lynn":"len",
"doorknob":"map",
"upgoat":"max",
"briantompsett":"memoryview",
"downgoat":"min",
"jimmy23013":"open",
"destructiblewatermelon":"ord",
"ninjabearmonkey":"pow",
"you":"print",
"djmcmayhem":"range",
"qwerpderp":"round",
"orlp":"sorted",
"timmyd":"staticmethod",
"muddyfish":"sum",
"balint":"super",
"trichoplax":"tuple",
"quartata":"zip"}
An example peoples.txt
:
peter i taylor djmcmayhem(10):
you(list(set(i)))
Should output:
for i in range(10):
print(list(set(i)))
What does the code do? It takes in an input file (by default peoples.txt
in the same dir as main.py
) and translates it from "People's Python" to standard python, and executes the result. It also writes the compiled "normal" code into <input_file_name>-output.txt
, by default peoples-output.txt
Run as python main.py custom/assignments.txt path/to/inputfile.txt
(inputfile.txt
is the People's Python code and assignments.txt
is the dict of assignments you're using)
In terms of style, how can I improve this?
The things I notice:
Probably could some more comments.
Replace
ast.literal_eval
with another method that doesn't involve using any eval. Not sure if this is even possible though. Would best practice say to just include the massive dictionary inmain.py
?Is using blank
except:
s with no special error (i.e.except SyntaxError:
) okay in this situation?
djmcmayhem
should be speltdrhamjam
\$\endgroup\$