Convert main() function to python idiom

It allows the modules to be used either as a library module, or as a standalone script