1

I am still very new to python, but I need to interface with some software that is written as a bunch of python modules (.py files in case I incorrectly identified them as "modules.") This program has some very useful and complicated functions that I really cannot feasibly hack (if nothing else, because every time I update the software, I will have to re-hack everything.)

I have a python script that looks like this:

from software import thingfromsoftware
def mything(x,y,someboolean=True,etc)
    var=thingfromsoftware(x,y)
    #....code....
    setattr(var, 'dimensions', somearraything)
    return(var)

Then, if I try:

result=mything(4,5)

then result correctly contains the values of all the attributes first assigned to it via thingfromsoftware but result.dimensions has not been assigned (has no attribute "dimensions")

The goal is to store the dimensions of every result computed and configured by myfunctionthing in some semi-coherent manner.

Real code (upon request)

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
  • 171,228
  • 44
  • 289
  • 238
Alex Eftimiades
  • 2,527
  • 3
  • 24
  • 33

1 Answers1

1

You're doing

tube=io.read(str(name))

if

TYPE!='bare'

So in that case you wouldn't get the dimensions. Could this be why you're having the problem?

Also, have you tried just doing

tube.dimensions = [tube.get_cell()[0, 0] / 2, tube.get_cell()[-1,-1]]

instead of setattr? It's only needed when the name of the attribute to change is stored in a variable name.

agf
  • 171,228
  • 44
  • 289
  • 238
  • That would make sense, let me test it to make sure. If that is the case, thank you, and I apologize for posting a debugging question rather than a global one. I will remove this post if that works (as I would be the only person who would find it useful) – Alex Eftimiades Aug 09 '11 at 19:36
  • No, don't remove it. Questions like this are posted all the time, you never know who might be helped by your code in the future. It doesn't deserve the downvote since your edit; as soon as I get votes back at the end of the day I'll upvote it. – agf Aug 09 '11 at 19:38
  • Ok. thanks. I couldn't remove it anyway (it seems there is no delete function.) – Alex Eftimiades Aug 09 '11 at 19:41