Source code for networkx.readwrite.json_graph.node_link

from itertools import chain, count

import networkx as nx

__all__ = ["node_link_data", "node_link_graph"]


_attrs = {
    "source": "source",
    "target": "target",
    "name": "id",
    "key": "key",
    "link": "links",
}


def _to_tuple(x):
    """Converts lists to tuples, including nested lists.

    All other non-list inputs are passed through unmodified. This function is
    intended to be used to convert potentially nested lists from json files
    into valid nodes.

    Examples
    --------
    >>> _to_tuple([1, 2, [3, 4]])
    (1, 2, (3, 4))
    """
    if not isinstance(x, (tuple, list)):
        return x
    return tuple(map(_to_tuple, x))