Skip to content

Commit

Permalink
Normalise to (str, int) to avoid unnecessary transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Sep 20, 2023
1 parent f86cf07 commit c88478b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,19 @@ def set_ports(self, *ports: Union[int, 'Port']) -> None:
ports: The ports to open. Provide an int to open a TCP port, or
a :class:`Port` to open a port for another protocol.
"""
existing_ports = self._backend.opened_ports()
# Normalise to get easier comparisons.
existing_ports = {
(port.protocol, port.port)
for port in self._backend.opened_ports()
}
desired_ports = {
Port('tcp', port) if isinstance(port, int) else port
('tcp', port) if isinstance(port, int) else (port.protocol, port.port)
for port in ports
}
for port in existing_ports.difference(desired_ports):
self._backend.close_port(port.protocol, port.port)
for port in desired_ports.difference(existing_ports):
self._backend.open_port(port.protocol, port.port)
for protocol, port in existing_ports.difference(desired_ports):
self._backend.close_port(protocol, port)
for protocol, port in desired_ports.difference(existing_ports):
self._backend.open_port(protocol, port)


@dataclasses.dataclass(frozen=True)
Expand Down

0 comments on commit c88478b

Please sign in to comment.