Advertisement
maxim_shlyahtin

6.2

Nov 8th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3.  
  4. class Text:
  5.     def __init__(self, text):
  6.         self.txt = text.split('\n')
  7.  
  8.     def __len__(self):
  9.         return len(self.txt)
  10.  
  11.     def __getitem__(self, item):
  12.         return self.txt[item]
  13.  
  14.     def lenstr(self, item):
  15.         string = self.txt[item].split()
  16.         return f'number of words -> {len(string)}'
  17.  
  18.     def word(self, item, word):
  19.         return self.txt[item].split()[word]
  20.  
  21.     def __str__(self):
  22.         a = [f'{self.txt[i]}' for i in range(len(self.txt))]
  23.         return '\n'.join(a) + '\n'
  24.  
  25.  
  26. s = Text('nice hot chilli peppers\nred orange blue')
  27. print(len(s))
  28. print(s.lenstr(0))
  29. print(s.word(0, 0))
  30. print(s)
  31.  
  32.  
  33. class EditableText(Text):
  34.  
  35.     def __setitem__(self, key, value):
  36.         self.txt[key] = value
  37.         return self.txt
  38.  
  39.     def change(self, item, key, value):
  40.         self.txt[item] = self.txt[item].split()
  41.         self.txt[item][key] = value
  42.         self.txt[item] = ' '.join(self.txt[item])
  43.         l = [f'{self.txt[i]}' for i in range(len(self.txt))]
  44.         return '\n'.join(l) + '\n'
  45.  
  46.     def pos(self, word):
  47.         storage = deepcopy(self.txt)
  48.         for i in range(len(self.txt)):
  49.             self.txt[i] = self.txt[i].split()
  50.             for j in range(len(self.txt[i])):
  51.                 if self.txt[i][j] == word:
  52.                     self.txt = storage
  53.                     return f'title:{word}\nstring:{i + 1}\nword:{j + 1}' + '\n'
  54.  
  55.     def onestr(self):
  56.         return ' '.join(self.txt) + '\n'
  57.  
  58.  
  59. a = EditableText('good awesome pretty\nbad worst')
  60. print(a)
  61. print(a.change(0, 0, 'bad'))
  62. print(a.pos('awesome'))
  63. print(a.onestr())
  64. print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement