Spaces:
Running on Zero
Running on Zero
| """Deterministic who/where/when queries over the structured timeline.""" | |
| from __future__ import annotations | |
| from ..models import Timeline, TimelineSlice | |
| class TimelineIndex: | |
| def __init__(self, timeline: Timeline) -> None: | |
| self.timeline = timeline | |
| def where(self, character: str, time_slice: str) -> list[TimelineSlice]: | |
| return [ | |
| s | |
| for s in self.timeline.slices | |
| if s.character.lower() == character.lower() | |
| and s.time_slice == time_slice | |
| ] | |
| def who_was_in(self, location: str, time_slice: str) -> list[str]: | |
| return [ | |
| s.character | |
| for s in self.timeline.slices | |
| if s.location.lower() == location.lower() | |
| and s.time_slice == time_slice | |
| ] | |
| def witnessed_by(self, character: str) -> list[TimelineSlice]: | |
| """Slices this character observed (present at, or named in observed_by).""" | |
| out: list[TimelineSlice] = [] | |
| for s in self.timeline.slices: | |
| if s.character.lower() == character.lower() or any( | |
| o.lower() == character.lower() for o in s.observed_by | |
| ): | |
| out.append(s) | |
| return out | |
| def slices_for(self, character: str) -> list[TimelineSlice]: | |
| return self.timeline.for_character(character) | |