Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tokenize
- import sys
- import collections
- CommentDescription = collections.namedtuple('CommentDescription',
- ('indent', 'comment',
- 'row', 'column'))
- comments = []
- program = []
- current_indent = ''
- def readline_from_input_and_store():
- string_i_have_just_read = sys.stdin.buffer.readline()
- program.append(string_i_have_just_read.decode())
- return string_i_have_just_read
- for (token_type, token_string, (token_row, token_col),
- *_) in tokenize.tokenize(readline_from_input_and_store):
- if token_type == tokenize.COMMENT:
- desc = CommentDescription(indent=current_indent, comment=token_string,
- row=token_row - 1, column=token_col)
- comments.append(desc)
- elif token_type == tokenize.INDENT:
- current_indent += token_string
- elif token_type == tokenize.DEDENT:
- current_indent = current_indent[:-1 - len(token_string)]
- for desc in comments:
- if program[desc.row][:desc.column].isspace() or desc.column == 0:
- continue
- result = program[desc.row][:desc.column].rstrip()
- result += '\n' + desc.indent + program[desc.row][desc.column:]
- program[desc.row] = result
- print(*program, sep='', end='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement