Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created Jul 16, 2020
Embed
What would you like to do?
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