I would like this to happen every time I run a program from the command line. Is my approach Pythonic? I'm interested to know whether there is any problem with readability or correctness. Any general advice on better ways of doing things or potential problems are also welcome.
main function is just an example, I'm looking for a review of the rest of the code, which I intend to append to all my existing programs and is therefore not just example code. I have included the example main function here as I used this to observe the different behaviour when including a passing or failing doctest. If any doctest fails, this will be printed to stdout and the program will not run. If all doctests pass, there is no output from the testing and the program runs as normal.
"""Auto doctest. Code to run the doctests before the main code every time the code is run from the command line. Uses assert so that running the code in optimised mode will skip the testing. """ def main(arguments): """Perform the functions of the program. Example doctest: >>> 1 + 1 2 """ print('The main function received the following arguments:') print(arguments) def _doctests_pass(): """Return True if all doctests pass, otherwise False.""" import doctest failure_count, test_count = doctest.testmod() return failure_count == 0 if __name__ == '__main__': assert _doctests_pass() import sys main(sys.argv)