Advertisement
VisualPaul

A3 - дз 1

Oct 5th, 2014
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import tokenize
  2. import sys
  3. import collections
  4.  
  5. CommentDescription = collections.namedtuple('CommentDescription',
  6.                                             ('indent', 'comment',
  7.                                              'row', 'column'))
  8.  
  9. comments = []
  10. program = []
  11. current_indent = ''
  12.  
  13.  
  14. def readline_from_input_and_store():
  15.     string_i_have_just_read = sys.stdin.buffer.readline()
  16.     program.append(string_i_have_just_read.decode())
  17.     return string_i_have_just_read
  18.  
  19. for (token_type, token_string, (token_row, token_col),
  20.      *_) in tokenize.tokenize(readline_from_input_and_store):
  21.     if token_type == tokenize.COMMENT:
  22.         desc = CommentDescription(indent=current_indent, comment=token_string,
  23.                                   row=token_row - 1, column=token_col)
  24.         comments.append(desc)
  25.     elif token_type == tokenize.INDENT:
  26.         current_indent += token_string
  27.     elif token_type == tokenize.DEDENT:
  28.         current_indent = current_indent[:-1 - len(token_string)]
  29. for desc in comments:
  30.     if program[desc.row][:desc.column].isspace() or desc.column == 0:
  31.         continue
  32.     result = program[desc.row][:desc.column].rstrip()
  33.     result += '\n' + desc.indent + program[desc.row][desc.column:]
  34.     program[desc.row] = result
  35. print(*program, sep='', end='')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement