Lab 3 of 5
Β·
Open-Ended
Write main.py β Load, Validate, and Log
Objective: Write main.py that loads devices.json, validates each record, creates NetworkDevice objects, and logs the result of each.
Instructions
Inside capstone/, create devices.json:
[
{"name": "sw-core-01", "ip": "10.1.1.1", "type": "switch", "site": "HQ", "vlan": 10},
{"name": "sw-access-01","ip": "10.1.1.2", "type": "switch", "site": "HQ", "vlan": 20},
{"name": "rtr-edge-01", "ip": "10.1.1.254", "type": "router", "site": "HQ", "vlan": 1},
{"name": "bad-device", "ip": "not-an-ip", "type": "switch", "site": "HQ", "vlan": 10},
{"name": "fw-dmz-01", "ip": "10.1.2.1", "type": "firewall", "site": "DMZ", "vlan": 100}
]
Then write main.py from scratch. It must:
- Configure logging with
format="%(levelname)s %(message)s"at INFO level - Load
devices.jsonwithjson.load() - For each record: validate it, create a
NetworkDeviceon success (log INFO with the device name), log WARNING on failure (with the device name and reason) - Print
Loaded X of Y recordsafter the loop
Your logging output must match exactly:
INFO Loaded sw-core-01
INFO Loaded sw-access-01
INFO Loaded rtr-edge-01
WARNING Skipped bad-device: Invalid IP: not-an-ip
INFO Loaded fw-dmz-01
Loaded 4 of 5 records
Paste your completed main.py below and submit.
π‘ Show Hint
Import NetworkDevice from device and validate_device + InvalidDeviceRecord from validator. Use try/except inside the loop. logging.info(f"Loaded {record['name']}") on success. logging.warning(f"Skipped {record.get('name', '?')}: {e}") on failure. Print the count after the loop.
Submit Your Code