Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import deepcopy
- class Text:
- def __init__(self, text):
- self.txt = text.split('\n')
- def __len__(self):
- return len(self.txt)
- def __getitem__(self, item):
- return self.txt[item]
- def lenstr(self, item):
- string = self.txt[item].split()
- return f'number of words -> {len(string)}'
- def word(self, item, word):
- return self.txt[item].split()[word]
- def __str__(self):
- a = [f'{self.txt[i]}' for i in range(len(self.txt))]
- return '\n'.join(a) + '\n'
- s = Text('nice hot chilli peppers\nred orange blue')
- print(len(s))
- print(s.lenstr(0))
- print(s.word(0, 0))
- print(s)
- class EditableText(Text):
- def __setitem__(self, key, value):
- self.txt[key] = value
- return self.txt
- def change(self, item, key, value):
- self.txt[item] = self.txt[item].split()
- self.txt[item][key] = value
- self.txt[item] = ' '.join(self.txt[item])
- l = [f'{self.txt[i]}' for i in range(len(self.txt))]
- return '\n'.join(l) + '\n'
- def pos(self, word):
- storage = deepcopy(self.txt)
- for i in range(len(self.txt)):
- self.txt[i] = self.txt[i].split()
- for j in range(len(self.txt[i])):
- if self.txt[i][j] == word:
- self.txt = storage
- return f'title:{word}\nstring:{i + 1}\nword:{j + 1}' + '\n'
- def onestr(self):
- return ' '.join(self.txt) + '\n'
- a = EditableText('good awesome pretty\nbad worst')
- print(a)
- print(a.change(0, 0, 'bad'))
- print(a.pos('awesome'))
- print(a.onestr())
- print(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement