AttributeError: 'ListProxy' object has no attribute 'copy'
AttributeError: 'ListProxy' object has no attribute 'copy'
Python 3.5
我正在尝试并行化以下代码并为此使用多处理模块中的 ListProxy 对象,以便工作人员以托管方式访问列表。我知道来自 multiprocessing 模块的 Proxy 对象是不可迭代的,但你可以调用对象上的 copy() 函数 return 该对象的常规版本,即 DictProxy 上的 copy() return是一个普通的字典,你可以遍历它。
def getAllLabels(graph: UndirectedGraph, graphSize: int, probabilityList: list) -> list:
'''Enumerate through ESU algorithm and retrieve all subgraphs of a given size for this graph.'''
process_manager = multiprocessing.Manager()
pool = multiprocessing.Pool(4)
subgraphList = process_manager.list() #looped
exclusionList = list() #looped
for i in range(0, graph.getSize()):
nodeList = list([i])
neighborList = list() #looped
for n in graph.getAdjacencyList(i):
if n not in exclusionList and n not in nodeList and n not in neighborList:
neighborList.append(n)
#pool.apply_async(getSubgraphs, [graph, graphSize, nodeList, neighborList, exclusionList, subgraphList, probabilityList])
getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
exclusionList.append(i)
labels = list()
for g in subgraphList.copy():
labels.append(nodesToGraph(graph, g))
pool.close()
return labels
但是,每次我在列表对象上调用 copy() 时,都会出现以下错误:
Traceback (most recent call last):
File "src/network_motifs.py", line 10, in <module>
subgraphs = esu.getAllLabels(graph, 4, [1, 1, 1, 1])
File "/home/erik/Git/nemolib-python-library/src/nemolib/esu/esu.py", line 39, in getAllLabels
getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
AttributeError: 'ListProxy' object has no attribute 'copy'
尝试使用
subgraphList[:]
而不是 subgraphList.copy(),因为您的 subgraphList 可能没有以这种方式实现 .copy() 方法。
否则,备选方案:
import copy
newsubgraphList = copy.copy(subgraphList)
如果执行 dir(subgraphList),您可以看到 LitProxy 对象可能具有的方法
['_Client', '__add__', '__builtins__', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_address_to_local', '_after_fork', '_authkey', '_callmethod', '_close', '_connect', '_decref', '_exposed_', '_getvalue', '_id', '_idset', '_incref', '_manager', '_mutex', '_owned_by_manager', '_serializer', '_tls', '_token', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
如您所见,它有一个您可以使用的深层复制方法
subgraphList.__deepcopy__({})
还有 deepcopy 确保它甚至复制嵌套的项目
Python 3.5
我正在尝试并行化以下代码并为此使用多处理模块中的 ListProxy 对象,以便工作人员以托管方式访问列表。我知道来自 multiprocessing 模块的 Proxy 对象是不可迭代的,但你可以调用对象上的 copy() 函数 return 该对象的常规版本,即 DictProxy 上的 copy() return是一个普通的字典,你可以遍历它。
def getAllLabels(graph: UndirectedGraph, graphSize: int, probabilityList: list) -> list:
'''Enumerate through ESU algorithm and retrieve all subgraphs of a given size for this graph.'''
process_manager = multiprocessing.Manager()
pool = multiprocessing.Pool(4)
subgraphList = process_manager.list() #looped
exclusionList = list() #looped
for i in range(0, graph.getSize()):
nodeList = list([i])
neighborList = list() #looped
for n in graph.getAdjacencyList(i):
if n not in exclusionList and n not in nodeList and n not in neighborList:
neighborList.append(n)
#pool.apply_async(getSubgraphs, [graph, graphSize, nodeList, neighborList, exclusionList, subgraphList, probabilityList])
getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
exclusionList.append(i)
labels = list()
for g in subgraphList.copy():
labels.append(nodesToGraph(graph, g))
pool.close()
return labels
但是,每次我在列表对象上调用 copy() 时,都会出现以下错误:
Traceback (most recent call last):
File "src/network_motifs.py", line 10, in <module>
subgraphs = esu.getAllLabels(graph, 4, [1, 1, 1, 1])
File "/home/erik/Git/nemolib-python-library/src/nemolib/esu/esu.py", line 39, in getAllLabels
getSubgraphs(graph, graphSize, nodeList, neighborList, exclusionList, subgraphList.copy(), probabilityList)
AttributeError: 'ListProxy' object has no attribute 'copy'
尝试使用
subgraphList[:]
而不是 subgraphList.copy(),因为您的 subgraphList 可能没有以这种方式实现 .copy() 方法。
否则,备选方案:
import copy
newsubgraphList = copy.copy(subgraphList)
如果执行 dir(subgraphList),您可以看到 LitProxy 对象可能具有的方法
['_Client', '__add__', '__builtins__', '__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_address_to_local', '_after_fork', '_authkey', '_callmethod', '_close', '_connect', '_decref', '_exposed_', '_getvalue', '_id', '_idset', '_incref', '_manager', '_mutex', '_owned_by_manager', '_serializer', '_tls', '_token', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
如您所见,它有一个您可以使用的深层复制方法
subgraphList.__deepcopy__({})
还有 deepcopy 确保它甚至复制嵌套的项目