#!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=Falsedefault=True, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ Do the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) #!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=False, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ Do the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) #!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=True, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ Do the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) NOTE: This is one of multiple formats, but this is able to be parsed by a documentation / helpdoc generator without much issue, at least via my IDE. A lot of my recommendations end up from my IDE :P
def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=Falsedefault=True, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=False, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() NOTE: This is one of multiple formats, but this is able to be parsed by a documentation / helpdoc generator without much issue, at least via my IDE. A lot of my recommendations end up from my IDE :P
def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=True, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() Use proper Docstring Format
Your docstrings don't conform to typical formats. Use this instead for the run docstring:
""" Execute the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ #!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=False, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ - `assignments` isDo the file toconversion openfrom thePeople's listPython ofto namesStandard fromPython -:param `open_from`assignments: isString denoting the filepath to getopen the inputname codelist from. -:param `to_write`open_from: isString fordenoting togglingthe writingfile theto compiledget codePeople's toPython acode fileinput from. -:param `to_exec`to_write: isTrue/False forto togglingdetermine executingwhether we store the converted code to a file. Both `to_write` and `to_exec`:param areto_exec: forTrue/False usingto thisdetermine codewhether inwe another execute the converted code. file by way of:return: importingThe itconverted code. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) #!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=False, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ - `assignments` is the file to open the list of names from - `open_from` is the file to get the input code from - `to_write` is for toggling writing the compiled code to a file - `to_exec` is for toggling executing the code Both `to_write` and `to_exec` are for using this code in another file by way of importing it. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) Use proper Docstring Format
Your docstrings don't conform to typical formats. Use this instead for the run docstring:
""" Execute the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ #!/usr/bin/env python import tokenize import json import os import argparse import subprocess as sp def get_args(): argparser = argparse.ArgumentParser(description="People's Python - Convert 'people' python to actual Python", add_help=True) argparser.add_argument('--assignments', required=False, type=str, dest='assignments', default="assignments.txt", help="Assignments file for names to Python builtins. Default is " "'assignments.txt'") argparser.add_argument('--people', '--peoples', required=False, type=str, dest='peoples', default="peoples.txt", help="People's Python file, for conversion. Default is 'peoples.txt'.") argparser.add_argument('--no-write-output', required=False, dest='write', action="store_false", default=False, help="Do not write output from conversion to standard python to a file, " "if this argument is provided.") argparser.add_argument('--no-exec-output', required=False, dest='execute', action="store_false", default=True, help="Do not execute output from conversion and do whatever it is set to do, " "if this argument is provided.") return argparser.parse_args() 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, open_from, to_write, to_exec): """ Do the conversion from People's Python to Standard Python :param assignments: String denoting the path to open the name list from. :param open_from: String denoting the file to get People's Python code input from. :param to_write: True/False to determine whether we store the converted code to a file. :param to_exec: True/False to determine whether we execute the converted code. :return: The converted code. """ with open(assignments, "r") as f: # Read the replacements into token_names global token_names token_names = json.load(f) 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) writepath = open_from[:-4]+"-output.txt" if to_write: with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) if to_exec: if not to_write: writepath = 'tmp.' + writepath with open(writepath, 'w') as outfile: # Write to the output file outfile.write(output) outfile.close() (execout, execerr) = sp.Popen(str('python %s' % writepath).split(), stdout=sp.PIPE, stdin=sp.PIPE).communicate() if not to_write: os.remove(writepath) if execerr and execerr != '': raise RuntimeError("An issue occurred running the converted code:\n%s" % str(execerr)) print execout return output if __name__ == "__main__": token_names = None args = get_args() try: run(args.assignments, args.peoples, args.write, args.execute) except Exception as e: print "An exception has occurred:\n%s" % str(e) Loading
lang-py