Python tool to assemble CodeReview posts from source files
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
7
down vote
favorite
I hate having to format questions around here. Adding four spaces to each source file and the correct name at the top of each source file becomes a headache when I have to post longer questions.
This is a simple python question parser that takes in arguments from the console and prints out correct format for a CodeReview question. Any suggestions on how to make this code more readable, concise and pythonic would be appreciated.
Question Parser.py
import os
import sys
import argparse
def format_string(PATH,EXTENSION):
source_files=[f for f in os.listdir(PATH) if os.path.isfile(os.path.join(PATH,f))]
source_files=[f for f in source_files if f.endswith(EXTENSION)]
indentation=4
formatted_string=""
for source_file in source_files:
formatted_string+= ("**%s**nn" % source_file)
with open(os.path.join(PATH,source_file)) as source:
for line in source.readlines():
formatted_string+=(" "*indentation+"".join(line))
return formatted_string
if __name__=="__main__":
parser=argparse.ArgumentParser(description="Automatic formatter for CodeReview Stackexchange Questions")
parser.add_argument("-p","--path",help="Path for the source files. Default is the current directory",nargs="?",default=os.getcwd(),const=os.getcwd())
parser.add_argument("header",help="The file that is added to the top of the question")
parser.add_argument("-e","--extension",help="Filter by the type of source files that is present in the directory",nargs="+",default="")
parser.add_argument("-o","--out",help="Output the result to a file. By default outputs to the console.",nargs="?")
args=parser.parse_args()
if not os.path.isfile(args.header):
raise parser.error("Header file doesnot exist.nPlease specify a correct header file path")
if os.path.exists(args.path):
question="".join(open(args.header).readlines())
question+="n"+format_string(args.path,tuple(args.extension,))
if not (args.out==None):
with open(args.out,"w") as outfile:
outfile.write(question)
else:
print(question)
else:
raise parser.error("Path doesnot exist.")
python file formatting stackexchange
add a comment |Â
up vote
7
down vote
favorite
I hate having to format questions around here. Adding four spaces to each source file and the correct name at the top of each source file becomes a headache when I have to post longer questions.
This is a simple python question parser that takes in arguments from the console and prints out correct format for a CodeReview question. Any suggestions on how to make this code more readable, concise and pythonic would be appreciated.
Question Parser.py
import os
import sys
import argparse
def format_string(PATH,EXTENSION):
source_files=[f for f in os.listdir(PATH) if os.path.isfile(os.path.join(PATH,f))]
source_files=[f for f in source_files if f.endswith(EXTENSION)]
indentation=4
formatted_string=""
for source_file in source_files:
formatted_string+= ("**%s**nn" % source_file)
with open(os.path.join(PATH,source_file)) as source:
for line in source.readlines():
formatted_string+=(" "*indentation+"".join(line))
return formatted_string
if __name__=="__main__":
parser=argparse.ArgumentParser(description="Automatic formatter for CodeReview Stackexchange Questions")
parser.add_argument("-p","--path",help="Path for the source files. Default is the current directory",nargs="?",default=os.getcwd(),const=os.getcwd())
parser.add_argument("header",help="The file that is added to the top of the question")
parser.add_argument("-e","--extension",help="Filter by the type of source files that is present in the directory",nargs="+",default="")
parser.add_argument("-o","--out",help="Output the result to a file. By default outputs to the console.",nargs="?")
args=parser.parse_args()
if not os.path.isfile(args.header):
raise parser.error("Header file doesnot exist.nPlease specify a correct header file path")
if os.path.exists(args.path):
question="".join(open(args.header).readlines())
question+="n"+format_string(args.path,tuple(args.extension,))
if not (args.out==None):
with open(args.out,"w") as outfile:
outfile.write(question)
else:
print(question)
else:
raise parser.error("Path doesnot exist.")
python file formatting stackexchange
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I hate having to format questions around here. Adding four spaces to each source file and the correct name at the top of each source file becomes a headache when I have to post longer questions.
This is a simple python question parser that takes in arguments from the console and prints out correct format for a CodeReview question. Any suggestions on how to make this code more readable, concise and pythonic would be appreciated.
Question Parser.py
import os
import sys
import argparse
def format_string(PATH,EXTENSION):
source_files=[f for f in os.listdir(PATH) if os.path.isfile(os.path.join(PATH,f))]
source_files=[f for f in source_files if f.endswith(EXTENSION)]
indentation=4
formatted_string=""
for source_file in source_files:
formatted_string+= ("**%s**nn" % source_file)
with open(os.path.join(PATH,source_file)) as source:
for line in source.readlines():
formatted_string+=(" "*indentation+"".join(line))
return formatted_string
if __name__=="__main__":
parser=argparse.ArgumentParser(description="Automatic formatter for CodeReview Stackexchange Questions")
parser.add_argument("-p","--path",help="Path for the source files. Default is the current directory",nargs="?",default=os.getcwd(),const=os.getcwd())
parser.add_argument("header",help="The file that is added to the top of the question")
parser.add_argument("-e","--extension",help="Filter by the type of source files that is present in the directory",nargs="+",default="")
parser.add_argument("-o","--out",help="Output the result to a file. By default outputs to the console.",nargs="?")
args=parser.parse_args()
if not os.path.isfile(args.header):
raise parser.error("Header file doesnot exist.nPlease specify a correct header file path")
if os.path.exists(args.path):
question="".join(open(args.header).readlines())
question+="n"+format_string(args.path,tuple(args.extension,))
if not (args.out==None):
with open(args.out,"w") as outfile:
outfile.write(question)
else:
print(question)
else:
raise parser.error("Path doesnot exist.")
python file formatting stackexchange
I hate having to format questions around here. Adding four spaces to each source file and the correct name at the top of each source file becomes a headache when I have to post longer questions.
This is a simple python question parser that takes in arguments from the console and prints out correct format for a CodeReview question. Any suggestions on how to make this code more readable, concise and pythonic would be appreciated.
Question Parser.py
import os
import sys
import argparse
def format_string(PATH,EXTENSION):
source_files=[f for f in os.listdir(PATH) if os.path.isfile(os.path.join(PATH,f))]
source_files=[f for f in source_files if f.endswith(EXTENSION)]
indentation=4
formatted_string=""
for source_file in source_files:
formatted_string+= ("**%s**nn" % source_file)
with open(os.path.join(PATH,source_file)) as source:
for line in source.readlines():
formatted_string+=(" "*indentation+"".join(line))
return formatted_string
if __name__=="__main__":
parser=argparse.ArgumentParser(description="Automatic formatter for CodeReview Stackexchange Questions")
parser.add_argument("-p","--path",help="Path for the source files. Default is the current directory",nargs="?",default=os.getcwd(),const=os.getcwd())
parser.add_argument("header",help="The file that is added to the top of the question")
parser.add_argument("-e","--extension",help="Filter by the type of source files that is present in the directory",nargs="+",default="")
parser.add_argument("-o","--out",help="Output the result to a file. By default outputs to the console.",nargs="?")
args=parser.parse_args()
if not os.path.isfile(args.header):
raise parser.error("Header file doesnot exist.nPlease specify a correct header file path")
if os.path.exists(args.path):
question="".join(open(args.header).readlines())
question+="n"+format_string(args.path,tuple(args.extension,))
if not (args.out==None):
with open(args.out,"w") as outfile:
outfile.write(question)
else:
print(question)
else:
raise parser.error("Path doesnot exist.")
python file formatting stackexchange
python file formatting stackexchange
edited Sep 2 at 5:45
asked Sep 2 at 5:26
Gnik
15010
15010
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
PEP 8
I would review PEP 8. Some of your style choices are not considered best practice by the Python community. Some quick observations:
- Constants are given
CAPS_WITH_UNDERSCORES
. Not function/method parameters.PATH
should bepath
for instance. - There is a space between the binary operators (there are, however, some exceptions: see Other Recommendations). So, for instance:
indentation=4
should beindentation = 4
. - Instead of
if not (args.out==None):
, You should doif args.out is not None:
(See Programming Recommendations) - I would advise making a
main
function. Yesif __name__ == '__main__':
is a start but I would recommend having a main function (see this answer, in particular the section "An Even Better Way"):
--
def main():
parser=argparse.ArgumentParser(...)
...
if __name__ == '__main__':
main()
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
PEP 8
I would review PEP 8. Some of your style choices are not considered best practice by the Python community. Some quick observations:
- Constants are given
CAPS_WITH_UNDERSCORES
. Not function/method parameters.PATH
should bepath
for instance. - There is a space between the binary operators (there are, however, some exceptions: see Other Recommendations). So, for instance:
indentation=4
should beindentation = 4
. - Instead of
if not (args.out==None):
, You should doif args.out is not None:
(See Programming Recommendations) - I would advise making a
main
function. Yesif __name__ == '__main__':
is a start but I would recommend having a main function (see this answer, in particular the section "An Even Better Way"):
--
def main():
parser=argparse.ArgumentParser(...)
...
if __name__ == '__main__':
main()
add a comment |Â
up vote
7
down vote
accepted
PEP 8
I would review PEP 8. Some of your style choices are not considered best practice by the Python community. Some quick observations:
- Constants are given
CAPS_WITH_UNDERSCORES
. Not function/method parameters.PATH
should bepath
for instance. - There is a space between the binary operators (there are, however, some exceptions: see Other Recommendations). So, for instance:
indentation=4
should beindentation = 4
. - Instead of
if not (args.out==None):
, You should doif args.out is not None:
(See Programming Recommendations) - I would advise making a
main
function. Yesif __name__ == '__main__':
is a start but I would recommend having a main function (see this answer, in particular the section "An Even Better Way"):
--
def main():
parser=argparse.ArgumentParser(...)
...
if __name__ == '__main__':
main()
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
PEP 8
I would review PEP 8. Some of your style choices are not considered best practice by the Python community. Some quick observations:
- Constants are given
CAPS_WITH_UNDERSCORES
. Not function/method parameters.PATH
should bepath
for instance. - There is a space between the binary operators (there are, however, some exceptions: see Other Recommendations). So, for instance:
indentation=4
should beindentation = 4
. - Instead of
if not (args.out==None):
, You should doif args.out is not None:
(See Programming Recommendations) - I would advise making a
main
function. Yesif __name__ == '__main__':
is a start but I would recommend having a main function (see this answer, in particular the section "An Even Better Way"):
--
def main():
parser=argparse.ArgumentParser(...)
...
if __name__ == '__main__':
main()
PEP 8
I would review PEP 8. Some of your style choices are not considered best practice by the Python community. Some quick observations:
- Constants are given
CAPS_WITH_UNDERSCORES
. Not function/method parameters.PATH
should bepath
for instance. - There is a space between the binary operators (there are, however, some exceptions: see Other Recommendations). So, for instance:
indentation=4
should beindentation = 4
. - Instead of
if not (args.out==None):
, You should doif args.out is not None:
(See Programming Recommendations) - I would advise making a
main
function. Yesif __name__ == '__main__':
is a start but I would recommend having a main function (see this answer, in particular the section "An Even Better Way"):
--
def main():
parser=argparse.ArgumentParser(...)
...
if __name__ == '__main__':
main()
answered Sep 2 at 5:54
Dair
4,208727
4,208727
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f202968%2fpython-tool-to-assemble-codereview-posts-from-source-files%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password