0

I have a file to read from, that looks like:

  0.0017224129699045  0.0006501069699993  0.9998957781816742
  0.1990018751753198  0.0008531972943402  0.0001365339587167
  0.3985306090674854  0.0004447825187626  0.9994454487115476
  0.5997635306734566  0.0007689624538330  0.0001887505556155
  0.8014083650919446  0.0007156269856168  0.9995317401042954
  0.1999636426048639  0.1995427045657650  0.0017775030876521

Each column shows coordinate of an atom. I want to assign coordinates to the atom defined as an object in python:

# The parser
#!/usr/bin/python3
def get_pos():
    with open("CONTCAR", "r") as finp:
        for line in finp:
            for _ in range(6):
                sdata = finp.readline()
                tpos.append(sdata.split())

    print(tpos)

And the calling function is:

#!/usr/bin/python3
import parsepos

class Atom:
    count = 0

    def __init__(self, name, pos=[], vel=[]):
        self.name = name
        self.pos = pos
        self.vel = vel
        Atom.count += 1
        # self.parse = parsepos.get_pos()
parsepos.get_pos()

This mcwe, shows the atoms are listed properly, in list tpos, but I don't know how to assign those value to atom.pos.

Kindly help.

BaRud
  • 3,055
  • 7
  • 41
  • 89
  • Using `#!/usr/bin/env python3` instead of `#!/usr/bin/python3` will find the Python interpreter no matter where it's installed... – linusg Apr 25 '16 at 12:13
  • 1
    Why not `atom.pos = whatever`? – Daniel Roseman Apr 25 '16 at 12:14
  • `Atom.pos = parsepos.get_pos() print(Atom.pos)` yeilds `none` – BaRud Apr 25 '16 at 12:29
  • 2
    Aside: using mutable default arguments like `pos=[]` can lead to problems, see [here](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument).. – DSM Apr 25 '16 at 12:42

1 Answers1

1

By default, a function in Python returns None. Just make get_pos() returning tpos:

def get_pos():
    with open("CONTCAR", "r") as finp:
        for line in finp:
            for _ in range(6):
                sdata = finp.readline()
                tpos.append(sdata.split())

    # print(tpos)
    return tpos

and then like this:

Atom.pos = parsepos.get_pos()
print(Atom.pos)

Hope this helps!

linusg
  • 6,289
  • 4
  • 28
  • 78