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

message.py 792B

1234567891011121314151617181920212223242526
  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([c for c in self.content if c.isdigit()])
  17. try:
  18. value = floor(float(cleaned_content))
  19. except Exception as e:
  20. raise ValueError(
  21. f"Message {cleaned_content} does not contain a number ({e})")
  22. return value