def check_parent(self, raw_person, parent): if parent == 'mother': if raw_person.mother_id != LoadPeople.NULL_ID: if raw_person.mother_id not in self.people_index: raise RelatedPersonError("{} missing mother {}".format(raw_person.id, raw_person.mother_id)) elif parent == 'father': if raw_person.father_id != LoadPeople.NULL_ID: if raw_person.father_id not in self.people_index: raise RelatedPersonError("{} missing father {}".format(raw_person.id, raw_person.father_id))
def phase2(self, filename, logfile, log): ## Phase 2 testing row = 0 errors = [] bad_ids = set() f = filename for line in f: row += 1 try: self.buffer.append(RawPersonRecord.make_from_line(line, row)) except ValueError as e: errors.append(str(e)) ## he wrote this for raw_person in self.buffer: try: if raw_person.id in self.people_index: bad_ids.add(raw_person.id) del self.people_index[raw_person.id] if raw_person.id in bad_ids: raise RelatedPersonError("duplicate ID: {}".format(raw_person.id)) ## i wrote this raw_person_gender = raw_person.gender correct_constant = Gender.get_gender(raw_person_gender) gender = correct_constant related_person = RelatedPerson(raw_person.id, raw_person.name, raw_person.gender) self.people_index[raw_person.id] = related_person except RelatedPersonError as e: errors.append("row {}: {}".format(raw_person.row, str(e))) if errors: if logfile != None: for rpe in errors: log.logger.error(rpe) elif logfile == None: log.logger.error('- individual errors -') log.logger.error('\n'.join(errors))
def phase2(self): # Phase 2: row = 0 errors = [] bad_ids = set() # todo: report failure to open and quit, or have file opened by command line parsing args = self.phase1() filename = args.infile with filename as f: for line in f: row += 1 try: self.buffer.append( RawPersonRecord.make_from_line(line, row)) except ValueError as e: errors.append(str(e)) # check IDs, genders, & create RelatedPersons for raw_person in self.buffer: try: # check for dupes if raw_person.id in self.people_index: bad_ids.add(raw_person.id) del self.people_index[raw_person.id] if raw_person.id in bad_ids: raise RelatedPersonError("duplicate ID: {}".format( raw_person.id)) # todo: get and check gender gender = Gender.get_gender(raw_person.gender) # make RelatedPerson related_person = RelatedPerson(raw_person.id, raw_person.name, gender) self.people_index[raw_person.id] = related_person except RelatedPersonError as e: errors.append("row {}: {}".format(raw_person.row, str(e))) bad_ids.add(raw_person.id) if errors: # todo: write to output determined by command line input text_1 = '\n- individual errors -' text_2 = '\n'.join(errors) if args.outfile: with args.outfile as o: o.write(text_1 + text_2) else: print(text_1, text_2)
def phase2(self, filename): # Phase 2: row = 0 errors = [] bad_ids = set() # todo: report failure to open and quit, or have file opened by command line parsing with open(filename, 'r') as f: for line in f: row += 1 try: self.buffer.append( RawPersonRecord.make_from_line(line, row)) except ValueError as e: errors.append(str(e)) # check IDs, genders, & create RelatedPersons for raw_person in self.buffer: try: # check for dupes if raw_person.id in self.people_index: bad_ids.add(raw_person.id) del self.people_index[raw_person.id] if raw_person.id in bad_ids: raise RelatedPersonError("duplicate ID: {}".format( raw_person.id)) # todo: get and check gender # make RelatedPerson related_person = RelatedPerson(raw_person.id, raw_person.name, gender) self.people_index[raw_person.id] = related_person except RelatedPersonError as e: errors.append("row {}: {}".format(raw_person.row, str(e))) if errors: # todo: write to output determined by command line input print('\n- individual errors -') print('\n'.join(errors))
def phase2(self, people_data): # Phase 2: row = 0 errors = [] bad_ids = set() for line in people_data: row += 1 try: self.buffer.append(RawPersonRecord.make_from_line(line, row)) except ValueError as e: errors.append(str(e)) # check IDs, genders, & create RelatedPersons for raw_person in self.buffer: try: # check for dupes if raw_person.id in self.people_index: bad_ids.add(raw_person.id) del self.people_index[raw_person.id] if raw_person.id in bad_ids: raise RelatedPersonError("duplicate ID: {}".format( raw_person.id)) # get and check gender gender = Gender.get_gender(raw_person.gender) # make RelatedPerson related_person = RelatedPerson(raw_person.id, raw_person.name, gender) self.people_index[raw_person.id] = related_person except RelatedPersonError as e: errors.append("row {}: {}".format(raw_person.row, str(e))) if errors: sys.stderr.write('\n- individual errors -\n') sys.stderr.write('\n'.join(errors)) sys.stderr.write('\n')