generalize python script for module use and use lambdas to eliminate code duplication