您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

message.py 842B

123456789101112131415161718192021222324252627
  1. from math import floor
  2. from typing import Optional
  3. from pydantic import BaseModel
  4. class Message(BaseModel):
  5. sender_name: str
  6. timestamp_ms: int
  7. content: Optional[str] = None
  8. is_geoblocked_for_viewer: Optional[bool] = None
  9. def get_counted_value(self):
  10. """
  11. The content of the message should be (or contain) a number
  12. """
  13. value = None
  14. # Remove any number that is not a digit
  15. # TODO parse potential math expressions in content
  16. cleaned_content = ''.join(
  17. [c for c in self.content if c.isdigit() or c in ['.', ',']]).replace(',', '.')
  18. try:
  19. value = floor(float(cleaned_content))
  20. except Exception as e:
  21. raise ValueError(
  22. f"Message {cleaned_content} does not contain a number ({e})")
  23. return value