Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Text Editor by Sarina
class Text:
def __init__(self):
self._text = ''
self._font = ''
@property
def text(self):
return self._text
@property
def font(self):
return self._font
def write(self, text):
self._text += text
def set_font(self, font):
self._font = font
def show(self):
if self._font:
return '[{0}]{1}[{0}]'.format(self._font, self._text)
else:
return self._text
def restore(self, memento):
self._text = memento.text
self._font = memento.font
class SavedText:
def __init__(self):
self._versions = dict()
self._num_version = 0
def get_version(self, version):
return self._versions.get(version)
"""
Second version is 1 and not -1 !!!!!
Class SavedText should have the next methods:
save_text(Text) - saves the current text and font. The first saved version has the number 0, the second - 1, and so on;
get_version(number) - this method works with the 'restore' method and is used for choosing the needed version of the text.
"""
def save_text(self, o):
self._versions[self._num_version] = Memento(o.text, o.font)
self._num_version += 1
class Memento:
def __init__(self, text, font):
self._text = text
self._font = font
@property
def text(self):
return self._text
@property
def font(self):
return self._font
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!")
Dec. 31, 2019
Comments: