def parse_runner(runner): """Parse a runner from the provided string.""" try: return Runners.get(int(runner)) except ValueError: if "," in runner: surname, given_name = runner.split(",") elif " " in runner: given_name, surname = runner.split(" ") else: return Runners.get(runner) return Runners.get(surname.strip(), given_name.strip())
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)
option_parser.add_option("--competition-year", default=date.today().year, type="int") option_parser.add_option("-n", "--nicknames") option_parser.add_option("-y", "--year", help="Graduation year", type="int") options, arguments = option_parser.parse_args(arguments[1:]) index = count(0) try: connect(options.server) given_name = arguments[index.next()] surname = arguments[index.next()] school_id = Schools.get(arguments[index.next()]) gender = parse_gender(arguments[index.next()]) except ConnectionError, error: option_parser.error(error) except IndexError: option_parser.error("Exactly 4 positional arguments must be provided.") except InvalidGenderError, error: option_parser.error(error) except SQLObjectNotFound, error: option_parser.error(error) return options, (given_name, surname, school_id, gender) @main_function(parse_arguments) def main(options, (given_name, surname, school, gender)): """Add a new runner to the database from the command-line arguments.""" Runners.create(given_name, surname, school, gender, options.year, options.nicknames, options.competition_year) if __name__ == "__main__": main()
def main(options, arguments): runners = irows(Runners.select()) for runner in Table(runners, column_seperator=" | "): print runner
"""Read a YAML description of the runners from stdin, and save it to the database.""" try: runners = load(stdin.read()) verify(runners, options) except KeyboardInterrupt: return 1 except ScannerError, error: print >> stderr, error return 1 except (SQLObjectNotFound, VerificationError), error: print >> stderr, error return 1 for runner in runners: Runners.create(runner["given_name"], runner["surname"], runner["school"], runner["gender"], runner["year"], runner["nicknames"], runner["competition_year"]) def verify(runners, options): """Check that all runners have the fields required by Runners.create(). Raises a VerificationError if any problems are encountered.""" for runner in runners: #Leaving out the names is not allowed for field in ("given_name", "surname"): if field not in runner: raise VerificationError("Required field \"%s\" left blank." % field) #School must be defined if "school" not in runner: if options.school is None: raise VerificationError("Required field \"school\" left blank.")