3

I have a file with the attributes for an object on each line. I want to take the attributes, separated by spaces, and add them to my object. I want the objects in a list but I can't get it to work. I have put a comment next to the line that doesn't do what I think it should be able to do. Alternatively if I just pass the words list, it stores the entire list in the first attribute of the object.

class Brick(object):
    def __init__(self, xopos=None, yopos=None, xcpos=None, ycpos=None, numb=None, prop=None):
        self.xopos = xopos
        self.yopos = yopos
        self.xcpos = xcpos
        self.ycpos = ycpos
        self.numb = numb
        self.prop = prop

bricklist = []

with open('data.txt', 'r') as f:
    data = f.readlines()

for line in data:
    words = line.split()
    bricklist.append(Brick.xopos(words[0])) #line that doesnt work

for i in range(len(bricklist)):
    print (bricklist[i].xopos)

the data is simply

1 13 14 15 16 17
2 21 22 23 24 14
3 3 4 5 6 7
4 1 1 1 1 1
5 5 6 4 1 1 
6 5 6 8 4 2
7 4 9 7 5 6 

I am very new to python, and I am finding alot of my ideas for implementing things just don't work so any help would be much appreciated.

Elwood168
  • 43
  • 1
  • 9
  • fix your indentation please – NDevox Jul 15 '15 at 15:36
  • 2
    What are you *actually trying to achieve*? Does `bricklist.append(Brick(*words))` do the trick? – jonrsharpe Jul 15 '15 at 15:36
  • print out `word[0]` to see if that is the data you need? – Maddy Jul 15 '15 at 15:42
  • That does work! Thanks a bunch, I'm trying to create a list of objects whose attributes are populated by the input file, I will go and research why that works. – Elwood168 Jul 15 '15 at 15:43
  • See e.g. http://stackoverflow.com/q/36901/3001761 – jonrsharpe Jul 15 '15 at 15:46
  • 2
    `Brick.xopos(words[0])` is nonsense given the code that we can see at the moment. First, the class object `Brick` does not have an attribute named `xopos` - your `__init__` function creates instance attributes, not class attributes (i.e `self.xopos`, not `xopos`). Second, chances are that, even if the attribute did exist, it's not a callable (the default `None` valuable certainly isn't), so `Brick.xopos(...)` makes no sense. – twalberg Jul 15 '15 at 15:54
  • Im not clear on the technical difference between instance attributes and class attributes or on what determines call-ability but thanks for a technical summary, this is something I need to develop a good understanding of so I will use your comment to direct my learning, thanks. – Elwood168 Jul 15 '15 at 16:34

2 Answers2

1

Try this:

class Brick(object):
    def __init__(self, values):
        self.xopos = values[0]
        self.yopos = values[1]
        self.xcpos = values[2]
        self.ycpos = values[3]
        self.numb = values[4]
        self.prop = values[5]

bricklist = []

with open('data.txt', 'r') as f:
    for line in f.readlines()
        bricklist.append(Brick(line.split())

for brick in bricklist:
    print (brick.xopos)

Instead of passing each attribute individually, read each line from the file, split it into a list and pass that to the constructor of your Brick object.

You can improve the __init__ method by verifying the content of values before using it.

Mike P
  • 742
  • 11
  • 26
0

I recommend introducing a function which takes in a string (a line of text in this case) and creates a Brick object from it:

class Brick(object):
    def __init__(self, xopos=None, yopos=None, xcpos=None, ycpos=None, numb=None, prop=None):
        self.xopos = xopos
        self.yopos = yopos
        self.xcpos = xcpos
        self.ycpos = ycpos
        self.numb = numb
        self.prop = prop

    @classmethod
    def from_string(cls, s):
        values = [int(v) for v in s.split()]
        return cls(*values)


with open('data.txt', 'r') as f:
    bricklist = [Brick.from_string(line) for line in f]
Mike P
  • 742
  • 11
  • 26
Hai Vu
  • 37,849
  • 11
  • 66
  • 93