Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #приведи к PEP8
- #потестируй, найдешь багу - скажи
- #коменты убери
- #да, быдлокод, согласен
- import sys
- import argparse
- signs = set(",.?!-:'") #вродь, ничего не забыл
- letters = set("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890")
- #переименуй как-нить, а то называть цифру буквой - не комильфо
- def is_letter(c):
- return (c in letters)
- def is_sign(c):
- return (c in signs)
- def reformat(text_in, line_len, indent_len):
- cur_lexem = ""
- was_space = False
- lexems = list()
- for c in text_in:
- if is_letter(c):
- if(was_space == False and (cur_lexem == "" or is_letter(cur_lexem[-1]))):
- cur_lexem += c
- else:
- lexems.append(cur_lexem)
- cur_lexem = c
- was_space = False
- elif is_sign(c):
- if(cur_lexem != "" and (is_letter(cur_lexem[-1]) or is_sign(cur_lexem[-1]))):
- cur_lexem += c
- else:
- raise RuntimeError("Идите нах*й, в условии такого не было")#эти исключения можешь убрать
- was_space = False
- elif c == '\n':
- if(not (cur_lexem == '\n' or cur_lexem == "")):
- lexems.append(cur_lexem)
- cur_lexem = '\n'
- was_space = False
- elif c == ' ':
- if(not(cur_lexem == "")):
- was_space = True
- else:
- raise RuntimeError("Идите нах*й, в условии такого не было") #эти исключения можешь убрать
- if(cur_lexem != ""):
- lexems.append(cur_lexem)
- text_out = ""
- cur_line = " " * indent_len
- was_newline = True
- #костыль
- if(len(lexems) == 0):
- return ""
- #еще костыль
- while(lexems[-1] == '\n'):
- lexems.pop()
- for lexem in lexems:
- if(len(lexem) > line_len):
- raise RuntimeError("Не форматируется") # а это нельзя, можно сообщение другое вписать
- if(lexem == "\n"):
- text_out += cur_line
- text_out += '\n'
- cur_line = ""
- cur_line += ' ' * indent_len
- was_newline = True
- elif(len(lexem) + len(cur_line) + 1 - int(was_newline) > line_len):
- text_out += cur_line
- text_out += '\n'
- cur_line = lexem
- was_newline = False
- else:
- print(was_newline)
- print(int(was_newline))
- cur_line += ' ' * (1 - int(was_newline))
- cur_line += lexem
- was_newline = False
- text_out += cur_line
- return text_out
- parser = argparse.ArgumentParser()
- parser.add_argument('--input', '-i', dest = 'input_file', type = str)
- parser.add_argument('--output', '-o', dest = 'output_file', type = str)
- parser.add_argument( '--line-length', '-l', dest = 'line_len', type=int)
- parser.add_argument('--paragraph-spaces', '-p', dest = 'indent_len', type=int)
- argv = parser.parse_args (sys.argv[1:])
- if(argv.input_file == None):
- filein = sys.stdin
- else:
- filein = open(argv.input_file, 'r')
- if(argv.output_file == None):
- fileout = sys.stdout
- else:
- fileout = open(argv.output_file, 'w')
- if(argv.line_len == None or argv.indent_len==None):
- print ("Wrong arguments")
- exit(0)
- inputtext = ""
- for line in filein.readlines():
- inputtext += line + '\n'
- print(reformat(inputtext, argv.line_len, argv.indent_len), file = fileout)
- filein.close()
- fileout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement