From 0d5ba8b9213db96702b755ef740a1520f55f698d Mon Sep 17 00:00:00 2001 From: juk0de Date: Fri, 18 Aug 2023 12:21:06 +0200 Subject: [PATCH] added module 'message.py' --- chatmastermind/message.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 chatmastermind/message.py diff --git a/chatmastermind/message.py b/chatmastermind/message.py new file mode 100644 index 0000000..01482c0 --- /dev/null +++ b/chatmastermind/message.py @@ -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 tag string does not contain the default separator. + """ + if cls.prefix in string: + raise MessageError(f"Question '{string}' contains the prefix '{cls.prefix}'") + instance = super().__new__(cls, string) + return instance