In the elex2/ directory, we’ve chopped up the original election_results.py code into a bunch of functions and turned this code directory into a package by adding an __init__.py file.
We’ve also added a suite of tests. This way we can methodically change the underlying code in later phases, while having greater confidence that we haven’t corrupted our summary numbers.
Note: We can’t stress this step enough: Testing existing code is THE critical first step in refactoring.
If the code doesn’t have tests, write some, at least for the most important bits of logic. Otherwise you’re just changing shit.
Fortunately, our code has a suite of unit tests for name parsing and, most importantly, the summary logic.
Python has built-in facilities for running tests, but they’re a little raw for our taste. We’ll use the nose library to more easily run our tests:
nosetests -v tests/test_parser.py
# or run all tests in the tests/ directory
nosetests -v tests/*.py
At a high level, this code is an improvement over elex1/, but it could still be much improved. We’ll get to that in Phase 3, when we introduce modules and packages.