1

My mentor Barry is always kicking me for forgetting to put spaces after my commas, equals signs, and for leaving too many lines at the end of a file. I wanted to practice some python and to write a parser to check my files before presenting them to him.

#BarryParser v0.1

from re import findall, search

def comma_checker(line, lineno):
    """ Checks commas have a space after them """
    split_line =  line.split(', ')
    for string in split_line:
        found_error = findall('.*,.*', string)
        if found_error:
            print "BARRY ISSUE DETECTED: COMMA ERROR LINE: %s: %s" % (lineno, line)

def equals_checker(line, lineno):
    split_line =  line.split(' = ')
    for string in split_line:
    found_error = findall('.*==?.*', string)
    if found_error:
        print "BARRY ISSUE DETECTED: EQUALS ERROR LINE: %s: %s" % (lineno, line)

def too_many_blank_lines(lines):
    """if the last line is a new line and the line before is also a new line,
       rasises barry issue over too many blank lines
    """
    last_line = lines[len(lines)-1]
    second_to_last_line = lines[len(lines)-2]
    if last_line == "\n" and second_to_last_line == "\n":
        print "BARRY ISSUE DETECTED: TOO MANY BLANK LINES AT END OF TEXT"
    elif search('\t*\n{1}', last_line)and search('\t*\n{1}', second_to_last_line):
        print "BARRY ISSUE DETECTED: TOO MANY BLANK LINES AT END OF TEXT"
    elif search('\t*\n{1}', second_to_last_line) and last_line == "\n":
        print "BARRY ISSUE DETECTED: TOO MANY BLANK LINES AT END OF TEXT"

def main():
    file = open("test.txt")
    line_no = 0
    lines = file.readlines(100000) 
    too_many_blank_lines(lines) #CHECK FOR BLANK LINES AT END OF TEXT
    for line in lines:
        line_no +=1 
        if not line == "\n":
            if not line[:1] == "#":
                comma_checker(line, line_no) #CHECK COMMAS ARE DONE RIGHT
                equals_checker(line, line_no) #CHECK EQUALS HAVE SPACES AFTER & BEFORE

if __name__ == '__main__':
    main()

It will be parsing python files. The question being, I can't figure out how to get the equals bit to handle == and = the same way.

Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • 2
    There are several tools that do a great job of this kind of thing for you: [PyLint, PyChecker or PyFlakes?](http://stackoverflow.com/q/1428872). Look at http://pypi.python.org/pypi/pep8 to do these tests for you, for example. – Martijn Pieters Sep 11 '12 at 15:51
  • WHat are you trying to solve? –  Sep 11 '12 at 15:52
  • You can use [PythonTidy](http://pypi.python.org/pypi/PythonTidy/). So, no need to re-invent the wheel. You use it like `python PythonTidy test.py barryfile.py`. – RanRag Sep 11 '12 at 15:53
  • If this is an exercise for learning python probably your question would fit better on [CodeReview](http://codereview.stackexchange.com/questions/tagged/python) – joaquin Sep 11 '12 at 15:59

2 Answers2

1

Have a look at the pep8 module. This checks your code for compliance against the pep8 coding standards.

See: http://www.python.org/dev/peps/pep-0008/

Here is sample output

thenh@localhost:~> pep8 *.py
1.py:11:1: W293 blank line contains whitespace
1.py:14:80: E501 line too long (81 characters)
1.py:24:1: E302 expected 2 blank lines, found 1
1.py:37:23: W291 trailing whitespace
1.py:90:27: E201 whitespace after '['
1.py:116:36: E701 multiple statements on one line (colon)
1.py:144:9: E303 too many blank lines (2)
2.py:22:1: W391 blank line at end of file
3.py:75:29: E231 missing whitespace after ','
Hans Then
  • 10,935
  • 3
  • 32
  • 51
0

Use Pylint to check the program for you.

Pierre GM
  • 19,809
  • 3
  • 56
  • 67
f p
  • 3,165
  • 1
  • 27
  • 35