Compose a route assessment from a local module
Reuse a tested selection rule to build a deterministic report for multiple targets.
Keep the rule in one place
The supplied net_rules.py exports select_routes, using the longest-prefix, lowest-metric and equal-cost policy from the routing lesson. Import it into main.py instead of copying its selection logic into another reporting function.
The new function handles a different responsibility: normalizing a list of targets and assembling an output row for each unique address. This separation makes it easier to change report formatting without accidentally changing route selection.
Sort addresses as addresses
Lexical sorting puts the text 192.0.2.100 before 192.0.2.9. Parse targets first and sort by address family and integer value. Keep IPv4 before IPv6, and deduplicate equivalent IPv6 spellings using address objects.
For each target, include the IDs of all selected routes. Use status routed when at least one exported route matches and unrouted otherwise. Avoid calling the status reachable: a matching route does not prove the next hop, forwarding path or remote application works.
The caller supplies an already validated route set and VRF. Invalid target strings still fail clearly. Return a new result list and leave the targets and route export intact.
Keep demonstration code behind a main guard so importing the module does not produce a report. This local import uses a bundled file in the virtual workspace; it requires no package installation or live network connection.
from net_rules import select_routes
print(select_routes("192.0.2.1", [], "blue"))
Put it into practice.
Define build_route_report(targets, routes, vrf), importing the supplied select_routes from net_rules. Routes/VRF are already validated. Require unscoped IP strings; invalid targets raise ValueError. Normalize and deduplicate addresses, sorted by family (IPv4 first) then numeric address. Return target, status, routes for each, where status is routed when select_routes finds matches or unrouted otherwise, and routes contains selected IDs in their defined order. Preserve inputs; do not call a host reachable or send traffic.
See an explained solution
from ipaddress import ip_address
from net_rules import select_routes
def build_route_report(targets, routes, vrf):
addresses = set()
for value in targets:
if not isinstance(value,str) or "%" in value:
raise ValueError("Use unscoped address strings")
addresses.add(ip_address(value.strip()))
result = []
for address in sorted(addresses,key=lambda value:(value.version,int(value))):
matches = select_routes(str(address),routes,vrf)
result.append({"target":str(address),"status":"routed" if matches else "unrouted","routes":[row["id"] for row in matches]})
return result
if __name__ == "__main__":
print(build_route_report(["192.0.2.9", "192.0.2.100"], [], "blue"))
The module owns route policy while the caller owns target normalization and reporting. Numeric ordering is stable, and the result describes route evidence without claiming successful connectivity.