Extract comments in your python code

Comments in your code are always good for new person to understand the logic and flow of program. I guess, people likes programming more, if they comment properly, same way as i do. Proper commenting will not allow you to read code line by line. The best way to comment is to comment in such a way, you can extract it easily. I do comments in my python code using '##' which makes me easy to extract them from script. Following code extracts my comment:


1
2
3
4
5
6
7
8
9
10
11
12
13
import os
import sys
## python extract_comment.py filename comment_delimeter
filename = sys.argv[1]
sepr = sys.argv[2]
fl = open(filename,'r')
fl_con = fl.readlines()
for row in fl_con:
    if sepr in row:
        ind = row.find(sepr)
        print row[ind:]
 
fl.close()



copy the code in file extract_comment.py, filename is the python script from where you need to extract comments and comment_delimeter is '##' in my case.




Leave comments

authimage
  • Thats true, proper commenting really helps.

    • alex
  • For this to work, it would need to parse and track strings, docstrings - as it is, it outputs anything that contains the "sepr" character string, which will sometimes be a comment.

    • anon
  • Usually I do not learn article on blogs, however I wish to say that this write-up very forced me to take a look at and do it! Your writing taste has been amazed me. Thanks, very great post.


Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved