python-checkio/OReilly/text-editor.py

52 lines
1.2 KiB
Python

import copy
class Text:
def __init__(self):
self.lines = ""
self.font = ""
def write(self, lines):
self.lines += lines
def set_font(self, font_name):
self.font = font_name
def show(self):
return "[{}]{}[{}]".format(self.font, self.lines, self.font) if self.font != "" else self.lines
def restore(self, version):
self.font = version.font
self.lines = version.lines
class SavedText:
def __init__(self):
self.texts = []
def save_text(self, texts):
self.texts.append(copy.deepcopy(texts))
def get_version(self, version):
return self.texts[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!")