def main(options, arguments): if options.pretty: for school in Schools.select(): print school.id, school.full_name else: for school in Schools.names(): print school
def parse_schools(string): """Parse a comma-separated list of numbers and integers into a list of School instances.""" schools = set(school for school in Schools.select()) try: string = string.lstrip() except AttributeError: return list(schools) if string[:3] == "NOT": avoid = set() for label in string[3:].split(","): label = label.strip() try: avoid.update(Schools.get(label)) except TooManyObjectsError: pass except SQLObjectNotFound: pass return list(schools - avoid) else: include = [] for label in string.split(","): label = label.strip() try: include.append(Schools.get(label)) except TooManyObjectsError: pass except SQLObjectNotFound: pass return include
def transform(string, year): """Where possible, replace all runner's with their database ID numbers. May raise a key error.""" result_list = load(string) schools = [(school, Regex("|".join(school.names()))) for school in Schools.select()] for (school, pattern) in schools: for item in result_list: if pattern.match(item["school"]): item["school_id"] = school.id del item["school"] join = INNERJOINOn(Runners, Affiliations, Runners.q.id == Affiliations.q.runner) for runner in Runners.select(Affiliations.q.year == year, join=join): for name in runner.given_names: last_first = r"%s,\s*%s" % (runner.surname, name) first_last = r"%s\s*%s" % (name, runner.surname) pattern = Regex(last_first + "|" + first_last, IGNORECASE) for item in result_list: if pattern.match(item["name"]): item["runner_id"] = runner.id del item["name"] del item["school_id"] return dump(result_list)