Python 3 copy.deepcopy

时间:2017-03-03 04:24:34

标签: python python-3.x recursion biopython

我目前正在使用BioPython Bio.PDB模块来解析和读取蛋白质结构文件(PDB文件)。尝试创建structure方法返回的PDBParser().get_structure(...)对象的深层副本时,我遇到了递归问题。

我的印象是copy.deepcopy函数是为了处理递归而设计的?奇怪的是,这只是使用Python 3的一个问题。在Python 2.7中运行等效代码工作正常(使用urllib2而不是urllib)。

这是copy.deepcopy中的错误吗?或Biopython的问题?

最小的例子( Python 3.6,Biopython 1.68 )是:

import urllib.request
from io import StringIO
from copy import deepcopy
import Bio.PDB

pdb_name = '1zro'
#Download file
pdb_url = "http://www.rcsb.org/pdb/files/" + pdb_name + ".pdb"
pdb_file = urllib.request.urlopen(pdb_url)
#Create string stream for Bio.PDB to read:
pdb_input_stream = StringIO(pdb_file.read().decode('utf-8'))

#Make Bio.PDB structure object
parser = Bio.PDB.PDBParser()
structure = parser.get_structure(pdb_name, pdb_input_stream)

#Attempt to perform a deepcopy on Bio.PDB Structure:
structure_copy = deepcopy(structure)

完整追溯:

  File "<stdin>", line 2, in <module>
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 280, in _reconstruct
    state = deepcopy(state, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 240, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 150, in deepcopy
    y = copier(x, memo)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 215, in _deepcopy_list
    append(deepcopy(a, memo))
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 180, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/copy.py", line 281, in _reconstruct
    if hasattr(y, '__setstate__'):
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
    if not hasattr(self, 'selected_child'):
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
    if not hasattr(self, 'selected_child'):
  File "/home/andrew/Programs/anaconda3/envs/py36/lib/python3.6/site-packages/Bio/PDB/Entity.py", line 207, in __getattr__
    if not hasattr(self, 'selected_child'):
  [Previous line repeated 318 more times]
RecursionError: maximum recursion depth exceeded

1 个答案:

答案 0 :(得分:1)

安德鲁亲切地将此作为Biopython问题https://github.com/biopython/biopython/issues/787提交了 - 尽管我们仍然不确定Python 3中出现了什么问题。