Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fileinput
- def find_comments(text):
- """Returns an iterable of comments in python source"""
- quote = '' # ' or " if inside string literal
- escape = False # true, if current symbol is escaped
- for line in text:
- # remove trailing carriage return
- line = line.rstrip('\n')
- for i in range(len(line)):
- # if current symbol is '#', and it is not quoted
- if line[i] == '#' and not quote:
- yield line[i:] # return the rest of string
- break
- # update the state of quote flag
- if line[i] == quote and not escape:
- quote = ''
- elif line[i] in '\'"' and not quote:
- quote = line[i]
- # update the state of escape flag
- escape = (line[i] == '\\') and not escape
- for comment in find_comments(fileinput.input()):
- print(comment)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement