​fly-in /
src / connection.py
63 commits
09-08-2026
60 lines 1.8 kB
 1"""Connections: the bidirectional edges of a network."""
 2
 3from dataclasses import dataclass
 4
 5
 6@dataclass(frozen=True)
 7class Connection:
 8    """A bidirectional link between two zones.
 9
10    Endpoints are stored by name rather than by Zone object: a
11    connection line can be read before its zones are looked up, and
12    names keep the two frozen dataclasses free of cycles.
13
14    Attributes:
15        a: Name of the first endpoint, as written in the map.
16        b: Name of the second endpoint, as written in the map.
17        max_link_capacity: Drones that may traverse it in one turn.
18    """
19
20    a: str
21    b: str
22    max_link_capacity: int = 1
23
24    @property
25    def key(self) -> tuple[str, str]:
26        """Order-independent identity of this connection.
27
28        a-b and b-a name the same link, so both yield the same
29        key and duplicates collapse in a dictionary.
30
31        Returns:
32            The endpoint names in alphabetical order.
33        """
34        return (self.a, self.b) if self.a < self.b else (self.b, self.a)
35
36    def other(self, name: str) -> str:
37        """Return the endpoint opposite to the given one.
38
39        Args:
40            name: Name of one of the two endpoints.
41
42        Returns:
43            The name of the far endpoint.
44
45        Raises:
46            ValueError: If the name is not an endpoint of this link.
47        """
48        if name == self.a:
49            return self.b
50        if name == self.b:
51            return self.a
52        raise ValueError(f"{name!r} is not an endpoint of {self.a}-{self.b}")
53
54    def __str__(self) -> str:
55        """Format the connection the way the output lines spell it.
56
57        Returns:
58            The two endpoint names joined by a dash.
59        """
60        return f"{self.a}-{self.b}"