Adds some tests that caught the bug in #590 and fixed said bug.  Factored out some common functionality into a _get_valid_format internal function, and did a bit of pep-8 cleanup while I was in there.