nodes_or_number#
- nodes_or_number(which_args)[源代码]#
decorator允许节点数或节点容器数。
使用此修饰符,指定的参数可以是节点的编号或容器。如果是数字,则使用的节点为
range(n)
。这允许nx.complete_graph(50)
代替nx.complete_graph(list(range(50)))
。而且它还允许nx.complete_graph(any_list_of_nodes)
。- 参数
- which_args字符串或整型或字符串或整型序列
如果为字符串,则为要处理的参数的名称。如果为int,则为要处理的参数的索引。如果允许多个节点参数,则可以是位置列表。
- 返回
- _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