Add support for Python 3.x

This change makes the mnemonic and test code compatible with Python 3. It also fixes a minor issue with opened files not being closed.

Note that the use of the `print_function` in the test suite requires Python 2.6 or later.