Source code for easygraph.readwrite.json_graph.node_link

from itertools import chain
from itertools import count

import easygraph as eg


__all__ = ["node_link_graph"]


_attrs = dict(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))