from math import floor from typing import Optional from pydantic import BaseModel class Message(BaseModel): sender_name: str timestamp_ms: int content: Optional[str] = None is_geoblocked_for_viewer: Optional[bool] = None def get_counted_value(self): """ The content of the message should be (or contain) a number """ value = None # Remove any number that is not a digit # TODO parse potential math expressions in content cleaned_content = ''.join( [c for c in self.content if c.isdigit() or c in ['.', ',']]).replace(',', '.') try: value = floor(float(cleaned_content)) except Exception as e: raise ValueError( f"Message {cleaned_content} does not contain a number ({e})") return value