Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created July 16, 2020 08:39
Show Gist options
  • Save exhuma/63a2c5c83d54546584963d36256da4b7 to your computer and use it in GitHub Desktop.
Save exhuma/63a2c5c83d54546584963d36256da4b7 to your computer and use it in GitHub Desktop.
SA relationship updates
class ScannedPort:
label: str = ""
clsas ScannedDevice:
hostname: str = ""
ports: List[ScannedPort] = []
class Port(Base):
hostname = Column(String, ForeignKey("Device.hostname")
label = Column(String)
class Device(Base):
hostname = Column(String, primary_key=True)
ports = relationship(Port)
def existing_or_new(...): ...
"""
See https://stackoverflow.com/a/52911047
"""
def process(session: Session, from_network: ScannedDevice) -> Device:
device = existing_or_new(Device, hostname=from_network.hostname)
device.ports = [] # not actual code, but representative of the existing logic
for port in from_network.ports:
tmp = existing_or_new(Port, hostname=from_network.hostname, label=from_network.label)
device.ports.append(tmp)
return device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment