-
Notifications
You must be signed in to change notification settings - Fork 5
/
【14】Text Editor.py
66 lines (47 loc) · 1.56 KB
/
【14】Text Editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# =====================================
# --*-- coding: utf-8 --*--
# @Author : TRHX
# @Blog : www.itrhx.com
# @CSDN : itrhx.blog.csdn.net
# @FileName: 【14】Text Editor.py
# =====================================
import copy
class Text:
def __init__(self):
self.text = ''
self.font = ''
def write(self, text):
self.text += text
def set_font(self, font):
self.font = font
def show(self):
if self.font != '':
font = '[' + self.font + ']'
else:
font = ''
return '{}{}{}'.format(font, self.text, font)
def restore(self, text):
self.text = text.text
self.font = text.font
class SavedText:
def __init__(self):
self.text2 = {}
self.last_version = 0
def save_text(self, text):
self.text2[self.last_version] = copy.copy(text)
self.last_version += 1
def get_version(self, version):
return self.text2[version]
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
text = Text()
saver = SavedText()
text.write("At the very beginning ")
saver.save_text(text)
text.set_font("Arial")
saver.save_text(text)
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"
text.restore(saver.get_version(0))
assert text.show() == "At the very beginning "
print("Coding complete? Let's try tests!")