Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RaceData:
- time = None
- pace = None
- age = None
- year = None
- def __init__(self,time,pace,age,year):
- self.time = time
- self.pace = pace
- self.age = age
- self.year = year
- def __str__(self):
- return "Year: " + self.year + " -- Age Cat: " + self.age + " -- Time: " + self.time + " -- Pace: " + self.pace
- def timeInSeconds(self):
- data = self.time.split(":")
- time = 60*60*int(data[0]) + 60*int(data[1]) + int(data[2])
- return time
- class Racer:
- name = ""
- racerID = None
- sex = ""
- raceList = None
- def __init__(self,name,racerID,sex):
- self.name = name
- self.racerID = racerID
- self.sex = sex
- self.raceList = []
- def addRace(self,race):
- self.raceList.append(race)
- def __str__(self):
- retStr = "ID: " + self.racerID + " -- Name: " + self.name + " -- Sex: " + self.sex
- for race in self.raceList:
- retStr += "\n " + str(race)
- return retStr
- def addFileToDict(filename,racerDict,allowDupes=1):
- # Allow Dupes should be 1 for files that include
- # multiple races like Project1_data.csv but should
- # be 0 for files that incldue only one race, like the
- # 2013 one will..
- file = open(filename)
- myDict = {}
- # Calling read line once, this moves the file
- # cursor passed the first line which isn't real
- # data but rather the column titles.
- # that way our for loop doesn't grab that shit.
- # ****************
- # Jordan because we trash the first line of the file
- # make sure your 2013 file has the trash line at the start.
- # can be anything but I suggest why not make it the same as
- # Project1_data.csv which is as follows:
- # Id,Name,Age Category,Sex,Rank,Time,Pace,Year
- # ****************
- garbageLine = file.readline()
- for line in file:
- # Line is a string that corresponds to each
- # line in the file sequentially, python is
- # super nice with shit like this.
- # By calling split with "," as the delimiter
- # we get a list of all the columns for that row.
- data = line.split(",")
- # We know by inspection of our CSV file that column
- # 0 is the racer ID. Similarly we know column 5
- # is the time (with 0 indexing ofc).
- racerID= data[0]
- # myDict.get(racerID) gets the value for that key.
- # if the key isn't in the dictionary this returns none
- # none in python also works as false.
- if myDict.get(racerID):
- # Only add race data for an existing racer if
- # we're allowing dupes.
- if allowDupes:
- myDict[racerID].addRace(RaceData(data[5].strip(),data[6].strip(),data[2].strip(),data[7].strip()))
- else:
- # Our dict doesn't ahve this racer,
- # so we must add them by indexing the
- # dictionary with their ID and assigning
- # it to the list with their time in it!
- myDict[racerID] = Racer(data[1].strip(),data[0].strip(),data[3].strip())
- myDict[racerID].addRace(RaceData(data[5].strip(),data[6].strip(),data[2].strip(),data[7].strip()))
- # Now add the data from our dictionary to the passed dictionary
- # of racers.
- for key in myDict:
- if racerDict.get(key):
- for race in myDict[key].raceList:
- racerDict[key].addRace(race)
- else:
- racerDict[key] = myDict[key]
- myDict = {}
- addFileToDict("Project1_data_updated.csv",myDict)
- for key in myDict:
- print myDict[key]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement