35 lines
983 B
Python
35 lines
983 B
Python
|
|
class TopicMessage():
|
|
"Object containing both a topic and message string attribute. Can convert itself to a string representation"
|
|
def __init__(self, topic:str = "", message:str = ""):
|
|
self.topic = topic
|
|
self.message = message
|
|
def to_string(self) -> str:
|
|
return f"{self.topic}: {self.message}"
|
|
|
|
class TMQueue():
|
|
"""Simple queue (FIFO) of TopicMessage objects"""
|
|
def __init__(self):
|
|
self.data: list[TopicMessage] = list()
|
|
|
|
def get(self):
|
|
if not self.is_empty():
|
|
item = self.data.pop(0)
|
|
return item
|
|
else:
|
|
raise IndexError
|
|
|
|
def add(self, item):
|
|
self.data.append(item)
|
|
|
|
def is_empty(self):
|
|
if len(self.data) == 0:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def string_to_topicmessage(s: str):
|
|
"""Convert string to a TopicMessage object"""
|
|
topic, message = str.split(":", maxsplit = 1)
|
|
return TopicMessage(topic, message)
|