command line params

For too long I’ve been letting the main executable limp along in “developer mode,” where you have to edit the file to change some params. I bit the bullet and read up on python3 argument parsing and of course it wasn’t that bad. Here’s the diff.

I used the argparse module, billed in the official docs as “the recommended command-line parsing module in the Python standard library.” It’s pretty clean, this is the sum of the argument code:

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action = 'store_true')
parser.add_argument('--start', '-s', type = int, default = this_year, help = "First year of range")
parser.add_argument('--end', '-e', type = int, default = this_year, help = "Last year of range")
args = parser.parse_args()

This also has the immediate advantage of making the executable somewhat self-documenting and easier to bootstrap into using.

$ python3 ./mcc_schedule.py --help
usage: mcc_schedule.py [-h] [--verbose] [--start START] [--end END]

optional arguments:
  -h, --help            show this help message and exit
  --verbose, -v
  --start START, -s START
                        First year of range
  --end END, -e END     Last year of range

Running it with no args now takes the reasonable action of doing the current year. Verbosity is controlled with the unix standard verbose/v flag.

$ python3 ./mcc_schedule.py --verbose
San José State 7 at USC 30 on Sep 04, 2021
Stanford 42 at USC 28 on Sep 11, 2021
Fresno State 40 at UCLA 37 on Sep 18, 2021
UCLA 35 at Stanford 24 on Sep 25, 2021
San Diego State 19 at San José State 13 on Oct 15, 2021
Fresno State at San Diego State on Oct 30, 2021
USC at California on Nov 12, 2021
California at Stanford on Nov 19, 2021
UCLA at USC on Nov 19, 2021
Fresno State at San José State on Nov 24, 2021
California at UCLA on Nov 26, 2021

San Diego State         1-0
Fresno State            1-0
UCLA                    1-1
Stanford                1-1
USC                     1-1
San José State          0-2
2021, 11, ,

Running a custom range is done with the start and end params

$ python3 ./mcc_schedule.py --start 2000 --end 2009
2000, 11, Fresno State, 2-1
2001, 9, Stanford, 4-0
2002, 10, USC, 3-0
disqualifying insufficient wins (1) from Fresno State
looks like a tie for the cup
Tie broken by head-to-head
2003, 9, California, 2-1
2004, 9, USC, 3-0
2005, 10, USC, 4-0
2006, 9, San José State, 3-0
disqualifying insufficient wins (1) from Fresno State
2007, 8, Stanford, 3-1
2008, 10, USC, 3-0
disqualifying insufficient wins (1) from Fresno State
looks like a tie for the cup
Tie broken by head-to-head
2009, 10, Stanford, 3-1

One thing nagging at me is the use of separate ‘start’ and ‘end’ arguments for a year range. It seems like there’s a slicker option to have; some kind of mega-parse there that would take range arguments like ‘1960-1980’, ‘1999,2001’ etc. Ctrl-f ‘range’ on the argparse docs doesn’t show anything but there are some stackoverflow examples showing how you can overload argparse’s actual argument parse for individual args. (Passing functions as params is super clean in python.) That’s a pretty nice feature. But I would need to figure out what the real standard for range text is rather than just roll my own. There’s also more that can go wrong when you’re parsing stuff yourself. The nice thing about telling argparse you want some ints is that it barfs out type errors on its own and you’re essentially behind a wall of exception handling for free.

$ python3 ./mcc_schedule.py --start 1970 --end cruft
usage: mcc_schedule.py [-h] [--verbose] [--start START] [--end END]
mcc_schedule.py: error: argument --end/-e: invalid int value: 'cruft'

What else is on the board?

  • more than two teams tiebreakers…. 2013 Texas is the test case
  • unit testing
  • revisit ties and standings for weird years

Testing is the obvious ugly one here. If I want to get real about constantly tinkering with the code I need to have some unit test suite that will use a reference corpus of canned json and check results against expected.

Published
Categorized as code