import ipaddress


def _item_to_python_repr(item, definitions):
    """Converts the given Capirca item into a typed Python object."""
    # Capirca comments are just appended to item strings
    s = item.split("#")[0].strip()

    # A reference to another network
    if s in definitions.networks:
        return s

    # IPv4 address / network
    try:
        return ipaddress.IPv4Address(s)
    except ValueError:
        pass
    try:
        return ipaddress.IPv4Network(s, strict=False)
    except ValueError:
        pass

    # IPv6 address / network
    try:
        return ipaddress.IPv6Address(s)
    except ValueError:
        pass
    try:
        return ipaddress.IPv6Network(s, strict=False)
    except ValueError:
        pass

    raise ValueError("Unknown how to convert {s}".format(s=s))