私のメンターであるバリーは、コンマや等号の後にスペースを入れるのを忘れたこと、およびファイルの最後に行が多すぎることでいつも私を蹴っています。Pythonを練習し、ファイルを提示する前にファイルをチェックするパーサーを作成したかったのです。
#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()
Pythonファイルを解析します。問題は、==と=を同じ方法で処理するためにequalsビットを取得する方法を理解できないことです。