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 parse_arguments(arguments): """Parse command line arguments. Returns the tuple (options, (given_name, surname, school, gender)). May raise a ConnectionError.""" option_parser = OptionParser() option_parser.set_usage("%prog [options] GIVEN_NAME SURNAME SCHOOL GENDER") 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)
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.") runner["school"] = options.school else: runner["school"] = Schools.get(runner["school"]) #Gender must be defined if "gender" not in runner: if options.gender is None: raise VerificationError("Required field \"gender\" left blank.") runner["gender"] = options.gender for field in ("year", "nicknames", "competition_year"): if field not in runner: runner[field] = None
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)
"specified schools.") option_parser.add_option("-p", "--previous-years", default=0, type="int") option_parser.add_option("-y", "--year", default=date.today().year, type="int") options, arguments = option_parser.parse_args(arguments[1:]) index = count() try: connect(options.server) gender = arguments[next(index)] except IndexError: option_parser.error("") except ConnectionError, error: option_parser.error(error) if options.conference is not None: options.conference = Conferences.get(options.conference) options.filter = list(Schools.selectBy(conference=options.conference)) else: options.filter = option_parser.parse_schools(options.filter) if options.dist_limit is None: if gender == "M": options.dist_limit = Distances.get(1).mens_distance else: options.dist_limit = Distances.get(1).womens_distance return options, (gender,) @main_function(parse_arguments) def main(options, (gender,)): results = assemble_results(gender, options.dist_limit, options.filter, options.year, options.previous_years) results.score() if options.exclude_scoreless:
from common import connect, ConnectionError, gender_callback, OptionParser def parse_arguments(arguments): """Parse command line arguments. Returns the tuple (options, arguments).""" option_parser = OptionParser() option_parser.add_option("-g", "--gender", action="callback", callback=gender_callback, type="str") option_parser.add_option("--school") options, arguments = option_parser.parse_args(arguments[1:]) try: connect(options.server) except ConnectionError, error: option_parser.error(error) if options.school is not None: options.school = Schools.get(options.school) return options, arguments @main_function(parse_arguments) def main(options, arguments): """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:
def parse_arguments(arguments): """Parse command line arguments. Returns the tuple (options, (school,)). May raise a ConnectionError.""" option_parser = OptionParser("%prog SCHOOL") option_parser.add_option("-g", "--sort-by-gender", action="store_true", default=False) option_parser.add_option("-y", "--year", default=date.today().year, type="int") options, arguments = option_parser.parse_args(arguments[1:]) try: connect(options.server) except ConnectionError, error: option_parser.error(error) index = count() try: school = Schools.get(arguments[next(index)]) except (TooManyObjectsError, SQLObjectNotFound), error: option_parser.error(error) except IndexError: option_parser.error("Required argument not provided.") return options, (school,) @main_function(parse_arguments) def main(options, (school,)): runners = irunners(school, options.year, options.sort_by_gender) pads = [str.rjust, None, None, None] runners = Table(runners, column_seperator=" | ", pads=pads) for runner in runners: print runner def irunners(school, year, sort_by_gender=False):