You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sequence.py 1018B

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import annotations
  2. from pydantic import BaseModel
  3. import pydantic
  4. from million.model.message import Message
  5. import million.analyze.message_evaluation as msg_val
  6. class Sequence(BaseModel):
  7. start_message: Message
  8. end_message: Message | None = None
  9. @pydantic.validator('end_message', pre=True, always=True)
  10. def default_end_message(cls, v, *, values):
  11. return v or values['start_message']
  12. def start(self) -> int:
  13. return msg_val.get(self.start_message)
  14. def end(self) -> int:
  15. return msg_val.get(self.end_message)
  16. def length(self) -> int:
  17. return self.end() - self.start() + 1
  18. def merge(self, other: Sequence) -> None:
  19. if other.start() < self.start(): self.start_message = other.start_message
  20. if other.end() > self.end(): self.end_message = other.end_message
  21. def overlaps(self, other: Sequence) -> bool:
  22. return self.start() <= other.end() + 1 and \
  23. other.start() <= self.end() + 1