The class below allows to create and edit a text object has three methods - write()
adds new text to the existing one, set_font
adds name of the new font in square brackets to the beginning and the end of the text, and show()
simply prints the text:
import re
class Text:
def __init__(self):
self.content = ''
def write(self, text):
match = re.search(r'\[[^\]]*\]$', self.content, re.IGNORECASE)
if match:
font_name = match.group(0)
self.content = font_name + self.content.replace(font_name, '') + text + font_name
else:
self.content += text
def set_font(self, font_name):
new_font = "[{}]".format(font_name)
match = re.match(r'\[[^\]]*\]', self.content, re.IGNORECASE)
if match:
old_font = match.group(0)
self.content = self.content.replace(old_font, new_font)
else:
self.content = new_font + self.content + new_font
def show(self):
print(self.content)
When I create and manipulate the object in the code below it seems to work as it's supposed to but it doesn't pass the assertion-test below even thought it seems to output the identical result string to the one in the assert
statement.
Can you help me see what I'm missing?
text = Text()
text.write("At the very beginning ")
text.set_font("Arial")
text.write("there was nothing.")
assert text.show() == "[Arial]At the very beginning there was nothing.[Arial]"