|
4 | 4 | from typing import Any, List, Sequence, TypeAlias, cast |
5 | 5 |
|
6 | 6 | import yatiml |
| 7 | +from typing_extensions import Self |
7 | 8 |
|
8 | 9 | from ymmsl.v0_2.component import Component |
9 | 10 | from ymmsl.v0_2.identity import Identifier, Reference |
@@ -331,6 +332,35 @@ def __init__( |
331 | 332 | def __contains__(self, timeline: Timeline) -> bool: |
332 | 333 | return timeline in self.matches |
333 | 334 |
|
| 335 | + def __copy__(self) -> "MatchingTimelines": |
| 336 | + """Shallow-copy the object. |
| 337 | +
|
| 338 | + Since this class effectively models a container, a shallow copy should create a |
| 339 | + new container with the same items in it, to which items can be added or removed |
| 340 | + without affecting the original. By default, copy.copy() won't do that however, |
| 341 | + as it will return a new MatchingTimelines containing the same self.matches. So |
| 342 | + we override it to give more natural semantics. |
| 343 | + """ |
| 344 | + return MatchingTimelines(self.head, list(self.matches)) |
| 345 | + |
| 346 | + def __ior__(self, other: "MatchingTimelines") -> Self: |
| 347 | + """Merge another MatchingTimelines into this one. |
| 348 | +
|
| 349 | + This takes all of the matching timelines from other and adds them to this |
| 350 | + object. If the head timeline of other is nested less deeply than the current |
| 351 | + head, then it will replace the current head, otherwise the head remains |
| 352 | + unchanged. |
| 353 | + """ |
| 354 | + |
| 355 | + self.matches |= other.matches |
| 356 | + |
| 357 | + this_depth = max([len(ref) for ref in self.head]) |
| 358 | + other_depth = max([len(ref) for ref in other.head]) |
| 359 | + if other_depth < this_depth: |
| 360 | + self.head = other.head |
| 361 | + |
| 362 | + return self |
| 363 | + |
334 | 364 | def _yatiml_attributes(self) -> OrderedDict: |
335 | 365 | matches: str | list[str] = list(map(str, sorted(self.matches - {self.head}))) |
336 | 366 | if len(matches) < 6 and sum(len(m) for m in matches) < 60: |
|
0 commit comments