Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. class RaceData:
  2.     time = None
  3.     pace = None
  4.     age = None
  5.     year = None
  6.     def __init__(self,time,pace,age,year):
  7.         self.time = time
  8.         self.pace = pace
  9.         self.age = age
  10.         self.year = year
  11.        
  12.     def __str__(self):
  13.         return "Year: " + self.year + " -- Age Cat: " + self.age + " -- Time: " + self.time + " -- Pace: " + self.pace
  14.        
  15.     def timeInSeconds(self):
  16.         data = self.time.split(":")
  17.         time = 60*60*int(data[0]) + 60*int(data[1]) + int(data[2])
  18.         return time
  19.  
  20. class Racer:
  21.     name = ""
  22.     racerID = None
  23.     sex = ""
  24.     raceList = None
  25.    
  26.     def __init__(self,name,racerID,sex):
  27.         self.name = name
  28.         self.racerID = racerID
  29.         self.sex = sex
  30.         self.raceList = []
  31.    
  32.     def addRace(self,race):
  33.         self.raceList.append(race)
  34.        
  35.     def __str__(self):
  36.         retStr = "ID: " + self.racerID + " -- Name: " + self.name + " -- Sex: " + self.sex
  37.         for race in self.raceList:
  38.             retStr += "\n    " + str(race)
  39.         return retStr
  40.    
  41.  
  42. def addFileToDict(filename,racerDict,allowDupes=1):
  43.     # Allow Dupes should be 1 for files that include
  44.     # multiple races like Project1_data.csv but should
  45.     # be 0 for files that incldue only one race, like the
  46.     # 2013 one will..
  47.     file = open(filename)
  48.     myDict = {}
  49.    
  50.     # Calling read line once, this moves the file
  51.     # cursor passed the first line which isn't real
  52.     # data but rather the column titles.
  53.     # that way our for loop doesn't grab that shit.
  54.    
  55.     # ****************
  56.     # Jordan because we trash the first line of the file
  57.     # make sure your 2013 file has the trash line at the start.
  58.     # can be anything but I suggest why not make it the same as
  59.     # Project1_data.csv which is as follows:
  60.     # Id,Name,Age Category,Sex,Rank,Time,Pace,Year
  61.     # ****************
  62.     garbageLine = file.readline()
  63.    
  64.    
  65.     for line in file:
  66.         # Line is a string that corresponds to each
  67.         # line in the file sequentially, python is
  68.         # super nice with shit like this.
  69.         # By calling split with "," as the delimiter
  70.         # we get a list of all the columns for that row.
  71.         data = line.split(",")
  72.         # We know by inspection of our CSV file that column
  73.         # 0 is the racer ID. Similarly we know column 5
  74.         # is the time (with 0 indexing ofc).
  75.         racerID= data[0]
  76.        
  77.         # myDict.get(racerID) gets the value for that key.
  78.         # if the key isn't in the dictionary this returns none
  79.         # none in python also works as false.
  80.         if myDict.get(racerID):
  81.             # Only add race data for an existing racer if
  82.             # we're allowing dupes.
  83.             if allowDupes:            
  84.                 myDict[racerID].addRace(RaceData(data[5].strip(),data[6].strip(),data[2].strip(),data[7].strip()))
  85.         else:
  86.             # Our dict doesn't ahve this racer,
  87.             # so we must add them by indexing the
  88.             # dictionary with their ID and assigning
  89.             # it to the list with their time in it!
  90.             myDict[racerID] = Racer(data[1].strip(),data[0].strip(),data[3].strip())
  91.             myDict[racerID].addRace(RaceData(data[5].strip(),data[6].strip(),data[2].strip(),data[7].strip()))
  92.     # Now add the data from our dictionary to the passed dictionary
  93.     # of racers.
  94.     for key in myDict:
  95.         if racerDict.get(key):
  96.             for race in myDict[key].raceList:
  97.                 racerDict[key].addRace(race)
  98.         else:
  99.             racerDict[key] = myDict[key]
  100.        
  101. myDict = {}
  102. addFileToDict("Project1_data_updated.csv",myDict)
  103. for key in myDict:
  104.     print myDict[key]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement