added module 'message.py'

This commit is contained in:
juk0de 2023-08-18 12:21:06 +02:00
parent 604e5ccf73
commit 9b61a4ab05

26
chatmastermind/message.py Normal file
View File

@ -0,0 +1,26 @@
"""
Module implementing message related functions and classes.
"""
from typing import Type, TypeVar
QuestionInst = TypeVar('QuestionInst', bound='Question')
class MessageError(Exception):
pass
class Question(str):
"""
A single question with a defined prefix.
"""
prefix = '=== QUESTION ==='
def __new__(cls: Type[QuestionInst], string: str) -> QuestionInst:
"""
Make sure the question string does not contain the prefix.
"""
if cls.prefix in string:
raise MessageError(f"Question '{string}' contains the prefix '{cls.prefix}'")
instance = super().__new__(cls, string)
return instance