Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple two classes ... solution in Clear category for Text Editor by maksimus
class Text:
def __init__(self):
self.all_text = ""
self.font = ""
def write(self, text):
self.all_text += text
def set_font(self, font_name):
self.font = "["+font_name+"]"
def show(self):
return f"{self.font}{self.all_text}{self.font}"
def restore(self, cls_saver_num):
self.all_text = cls_saver_num[0]
self.font = cls_saver_num[1]
self.show()
class SavedText:
def __init__(self):
self.base = list()
def save_text(self, cls_text):
self.base.append((cls_text.all_text, cls_text.font))
def get_version(self, number):
return self.base[number]
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!")
June 4, 2020
Comments: