nodes_or_number#
- nodes_or_number(which_args)[source]#
允许使用节点数量或节点容器的装饰器。
使用此装饰器,指定参数可以是数字或节点容器。如果参数是数字,则使用的节点将是
range(n)
。这允许使用nx.complete_graph(50)
代替nx.complete_graph(list(range(50)))
。它还允许使用nx.complete_graph(any_list_of_nodes)
。- 参数:
- which_args字符串或整数,或字符串/整数序列
如果是字符串,表示要处理的参数的名称。如果是整数,表示要处理的参数的索引。如果允许有多个节点参数,则可以是位置列表。
- 返回:
- _nodes_or_numbers函数
一个将整数参数替换为范围的函数。
示例
像这样装饰函数
@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