networkx.utils.decorators.nodes_or_number

nodes_or_number(which_args)[source]

Decorator to allow number of nodes or container of nodes.

With this decorator, the specified argument can be either a number or a container of nodes. If it is a number, the nodes used are range(n). This allows nx.complete_graph(50) in place of nx.complete_graph(list(range(50))). And it also allows nx.complete_graph(any_list_of_nodes).

Parameters
which_argsstring or int or sequence of strings or ints

If string, the name of the argument to be treated. If int, the index of the argument to be treated. If more than one node argument is allowed, can be a list of locations.

Returns
_nodes_or_numbersfunction

Function which replaces int args with ranges.

Examples

Decorate functions like this:

@nodes_or_number("nodes")
def empty_graph(nodes):
    # nodes is converted to a list of nodes

@nodes_or_number(0)
def empty_graph(nodes):
    # nodes is converted to a list of nodes

@nodes_or_number(["m1", "m2"])
def grid_2d_graph(m1, m2, periodic=False):
    # m1 and m2 are each converted to a list of nodes

@nodes_or_number([0, 1])
def grid_2d_graph(m1, m2, periodic=False):
    # m1 and m2 are each converted to a list of nodes

@nodes_or_number(1)
def full_rary_tree(r, n)
    # presumably r is a number. It is not handled by this decorator.
    # n is converted to a list of nodes