Local termination pattern analysis in python