Source code for nrel.hive.model.roadnetwork.link_id

from ast import literal_eval
from typing import Optional, Tuple, Any

from nrel.hive.util.typealiases import LinkId

NodeId = Any





[docs]def extract_node_ids( link_id: LinkId, ) -> Tuple[Optional[Exception], Optional[Tuple[NodeId, NodeId]]]: """ expects the provided string is of the form {src_node_id}-{dst_node_id} :param link_id: a string that is a LinkId :return: an error, or, a tuple containing both node ids """ result = link_id.split("-") if len(result) < 2: return ( Exception(f"LinkId {link_id} does not take the form src_node_id-dst_node_id"), None, ) elif len(result) > 2: return ( Exception( f"LinkId {link_id} can only have one dash (-) character in the form src_node_id-dst_node_id" ), None, ) else: try: src = literal_eval(result[0]) dst = literal_eval(result[1]) except ValueError: return Exception(f"LinkId {link_id} cannot be parsed."), None return None, (src, dst)