저는 아직 Python에 익숙하지 않지만 파이썬 모듈 (.py 파일의 경우에는 많은 경우)로 작성된 일부 소프트웨어와 인터페이스해야합니다. 나는이 프로그램을 "모듈"이라고 잘못 인식했다.)이 프로그램은 매우 유용하고 복잡한 기능을 가지고 있으며 실제로 해킹 할 수 없다. (소프트웨어를 업데이트 할 때마다 모든 것을 다시 해킹해야하기 때문이다.)Python : setattr에 할당 된 속성과 함께 객체의 인스턴스를 반환하는 방법
내가하려고하면 다음from software import thingfromsoftware
def mything(x,y,someboolean=True,etc)
var=thingfromsoftware(x,y)
#....code....
setattr(var, 'dimensions', somearraything)
return(var)
을 :
나는 다음과 같습니다 파이썬 스크립트가
,691,363,210result=mything(4,5)
다음 result
올바르게 제 thingfromsoftware
통해 할당 된 모든 속성의 값을 포함하지만 result.dimensions
할당되지 않은 (has no attribute "dimensions"
)
목적은 모든 result
의 dimensions
계산하고 myfunctionthing
로 구성된 저장하는 일부 반 응집 방식으로 (요청시)
실제 코드
from ase.structure import nanotube
from ase import Atoms, view, io
from numpy import *
from Avogadro import Molecule, MoleculeFile
import cPickle as pickle
import os
def make_nanotube(n,m,length=1,TYPE='free'):
#This will set up leads so transport occures along the z axis and leads are aligned along the y axis (so they may be separated along the x axis.)
os.chdir("/tmp")
print 'Making ('+str(n)+','+str(m)+') nanotube with '+str(length)+" unit cell as "+str(TYPE)
tube = nanotube(n, m, length=length, bond=1.420, symbol='C')
center=tube.get_center_of_mass()
name='tube:('+str(n)+', '+str(m)+'):unit cells:'+str(length)+':'+str(TYPE)+'.xyz'
io.write(str(name), tube)
print 'Computing bonds'
mol = MoleculeFile.readMolecule(str(name))
RELOAD=0
for atom in mol.atoms[:]:
if len(atom.bonds)<2 and atom.pos[-1]<center[-1]:
print 'Relocating atom '+str(atom.index)+' from '+str(atom.pos[-1])+' to '+str(tube.get_cell()[-1, -1] + atom.pos[-1])
tube.positions[atom.index, -1] += tube.get_cell()[-1, -1]
RELOAD=1
print 'Orienting tube'
tip_atom=tube.get_positions()[:, -1].argmax() #the tip on the right (farther) end
tip=tube.get_positions()[tip_atom]
tube.rotate(append(tip[:-1], 0), append(center[0], zeros(2)), center=center) #rotate so that the tip is slanted toward x-axis (center[0],0,0)
tube.center()
setattr(tube, 'dimensions', [tube.get_cell()[0, 0]/2,tube.get_cell()[-1,-1]])
cell=tube.get_cell()
if TYPE!='bare':
if RELOAD==1:
print 'Recomputing bonds'
io.write(str(name), tube)
mol = MoleculeFile.readMolecule(str(name))
print 'Adding hydrogens'
mol.addHydrogens()
if TYPE=='left lead':
print 'Removing hydrogens from the right side'
for atom in mol.atoms[:]:
if atom.pos[2]<center[2]:
mol.removeHydrogens(atom)
elif TYPE=='right lead':
print 'Removing hydrogens from the left side'
for atom in mol.atoms[:]:
if atom.pos[2]>center[2]:
mol.removeHydrogens(atom)
MoleculeFile.writeMolecule(mol,str(name))
tube=io.read(str(name))
else:
tube.set_cell(cell)
return(tube)
게시 한 코드에 문제가없는 것 같습니다. 실제 코드를 보여줘야합니다. – agf
좋아, 방금 했어. 그것은 오히려 길다. 그래서 처음에 게시하지 않았습니다. –
@agf 이것을 파이썬 대화 형 세션에서 실행합니다 : execfile ('/ home/labgroup/Documents/scripts/make_nanotube.py'); test = make_nanotube (4,4); test.dimensions; –