1

I am fairly new to python. I have tried to define a class, I then want to create an instance from a file, then refer to specific pieces of it, but cannot seem to. This is Python 3.3.0

Here's the class....

class Teams():

    def __init__(self, ID = None, Team = None, R = None, W = None, L = None):
        self._items = [ [] for i in range(5) ]
        self.Count = 0


    def addTeam(self, ID, Team, R=None, W = 0, L = 0):
        self._items[0].append(ID)
        self._items[1].append(Team)
        self._items[2].append(R)
        self._items[3].append(W)
        self._items[4].append(L)
        self.Count += 1

    def addTeamsFromFile(self, filename):
        inputFile = open(filename, 'r')
        for line in inputFile:
            words = line.split(',')
            self.addTeam(words[0], words[1], words[2], words[3], words[4])

    def __len__(self):
        return self.Count

Here's the code in Main

startFileName = 'file_test.txt'
filename = startFileName

###########
myTestData = Teams()

myTestData.addTeamsFromFile(startFileName)

sample data in file

100,AAAA,106,5,0
200,BBBB,88,3,2
300,CCCC,45,1,4
400,DDDD,67,3,2
500,EEEE,90,4,1

I think I am good to here (not 100% sure), but now how do I reference this data to see... am i not creating the class correctly? How do I see if one instance is larger than another...

ie, myTestData[2][2] > myTestData[3][2] <----- this is where I get confused, as this doesn't work

1
  • 1
    Don’t forget to close your file handle after using it; or use the with open() as f: syntax to make Python do it for you. Commented Dec 9, 2012 at 16:23

2 Answers 2

1

Why don't you create a Team class like this :

class Team():
    def __init__(self, ID, Team, R=None, W = 0, L = 0)
      # set up fields here

Then in Teams

class Teams():
   def __init__(self):
       self._teams = []
   def addTeam (self, ID, Team, R=None, W = 0, L = 0)
       team = Team (ID, Team, R=None, W = 0, L = 0)
       self._teams.append (team)

Now If i got it right you want to overwrite the > operator's behaviour.

To do that overload __gt__(self, other) [link]

So it will be

class Team ():
   # init code from above for Team

    def __gt__ (self, otherTeam):
        return self.ID > otherTeam.ID # for example

Also be sure to convert those strings to numbers because you compare strings not numbers. Use int function for that.

Sign up to request clarification or add additional context in comments.

Comments

0

The immediate problem you're running into is that your code to access the team data doesn't account for your myTestData value being an object rather than a list. You can fix it by doing:

myTestData._items[2][2] > myTestData._items[3][2]

Though, if you plan on doing that much, I'd suggest renaming _items to something that's obviously supposed to be public. You might also want to make the addTeamsFromFile method convert some of the values it reads to integers (rather than leaving them as strings) before passing them to the addTeam method.

An alternative would be to make your Teams class support direct member access. You can do that by creating a method named __getitem__ (and __setitem__ if you want to be able to assign values directly). Something like:

def __getitem__(self, index):
    return self._items[index]

@Aleksandar's answer about making a class for the team data items is also a good one. In fact, it might be more useful to have a class for the individual teams than it is to have a class containing several. You could replace the Teams class with a list of Team instances. It depends on what you're going to be doing with it I guess.

4 Comments

Thanks. I have combined both of the suggestions. First a class aTeam() (there were too many items named 'Team') then a class Teams()... I can add from a file. I added a method getitem, but how do I call that from main? everything I try says it is undefined.
@user1889575: The __getitem__ method is called automatically when you do obj[index]. That is, your original code (like myTestData[2][2]) should work.
Thanks. I had to create __getitem__ for both classes.
Oh, I see. yeah, if you're still storing the data in a list inside the aTeam class, then you'd need a __getitem__ method there too. However, I'd suggest using attributes instead. That is, put the W value in self.W rather than in a certain index in a list. Then you'd do myTestData[2].W (or whatever).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.