def csvReader(iomanager, path): ''' Read data from a csv file and create Student objects from this data. The format of the csv file is currently something that must be static. Current expected layout of csv file: Timestamp, name, email, mon, tues, wed, thurs, fri, sat, sun, lang, mate1, mate2 Maybe this can become more dynamic in the future. @param: path: the path to the target csv file. @returns: A list of Student objects. ''' students = [] with open(path) as csvfile: lines = csv.reader(csvfile, delimiter=',', quotechar='"') num_elements = 0 #len(list(lines)[0]) cols = next(lines) for line in lines: num_elements = (len(line)) if num_elements == 0 else num_elements #format the name, and check the roster for validity splt = line[1].split(' ') if len(splt) > 1: splt[0].strip() splt[1].strip() name = splt[1] + ', ' + splt[0] else: name = splt[0].strip() result = iomanager.nameChecker(name) #if the student is ambiguous or cannot be found, skip them if result[0] == None or result[0] == False: continue student = Student(result[1], line[2]) filters = {} days = [] mates = [] for i in range(3, 10): if line[i] == '': day = Day(cols[i]) days.append(day) continue day = Day(cols[i]) int_times = list( map(lambda x: int(x), iomanager.blockParser(line[i]))) for time in int_times: day.insertTime(time) days.append(day) #TODO: retrieving this data from the c_data is incredibly verbose # and difficult to understand. Lets think about making this # more accessible. #Filters are of the form (list, max list size, weight) filters['Schedule'] = ( days, iomanager.c_data.filter_dictionary['Schedule'][1], iomanager.c_data.filter_dictionary['Schedule'][2]) lang_lst = iomanager.blockParser(line[10]) filters['Languages'] = ( lang_lst, iomanager.c_data.filter_dictionary['Languages'][1], iomanager.c_data.filter_dictionary['Languages'][2]) num_teammates = iomanager.c_data.filter_dictionary['Teammates'][1] start = num_elements - num_teammates for i in range(start, num_elements): mate_tup = iomanager.nameChecker(line[i]) if mate_tup[0]: mates.append(mate_tup[1]) filters['Teammates'] = ( mates, num_teammates, iomanager.c_data.filter_dictionary['Teammates'][2]) student.filters = filters students.append(student) return students
parser.add_argument('csv_path') parser.add_argument('roster_path') parser.add_argument('output_path') args = parser.parse_args() csv_pth = args.csv_path roster_pth = args.roster_path out_path = args.output_path roster = [] #grab the roster file and create a roster list with open(roster_pth) as r_file: for line in r_file: roster.append(line) d1 = Day("Tuesday") d1.insertTime(10) d1.insertTime(13) d2 = Day("Wednesday") d2.insertTime(10) d2.insertTime(14) s1 = Student("Hope, Bob", "*****@*****.**") days = [d1, d2] filters1 = {} filters1['Meeting Times'] = days s1.setFilters(filters1) s2 = Student("Jensen, Emily", "") days = [d2] filters2 = {} filters2['Meeting Times'] = days
def csvReader(self, path): ''' Read data from a csv file and create Student objects from this data. The format of the csv file is currently something that must be static. Current expected layout of csv file: Timestamp, name, email, mon, tues, wed, thurs, fri, sat, sun, lang, mate1, mate2 Maybe this can become more dynamic in the future. @param: path: the path to the target csv file. @returns: A list of Student objects. ''' students = [] with open(path) as csvfile: lines = csv.reader(csvfile, delimiter=',', quotechar='"') #TODO: we should probably ensure that the data is in the # correct order or handle this is some better manner. cols = next(lines) for line in lines: #format the name, and check the roster for validity splt = line[1].split(' ') if len(splt) > 1: splt[0].strip() splt[1].strip() name = splt[1] + ', ' + splt[0] else: name = splt[0].strip() result = self.nameChecker(name) #if the student is ambiguous or cannot be found, skip them if result[0] == None or result[0] == False: continue student = Student(result[1], line[2]) filters = {} days = [] mates = [] for i in range(3, 10): if line[i] == '': day = Day(cols[i]) days.append(day) continue day = Day(cols[i]) int_times = list( map(lambda x: int(x), self.blockParser(line[i]))) for time in int_times: day.insertTime(time) days.append(day) #TODO: lets not hard code these filters. # also, need to dynamically change # weights from user input. #Filters are of the form (list, max list size, weight) filters['Schedule'] = (days, 13, 1) lang_lst = self.blockParser(line[10]) filters['Languages'] = (lang_lst, 3, 1) #TODO: there may be a better way of allowing # extensions on the number of teammates. for i in range(11, 13): mate_tup = self.nameChecker(line[i]) if mate_tup[0]: mates.append(mate_tup[1]) filters['Teammates'] = (mates, 2, 1) student.setFilters(filters) students.append(student) return students