Some finite state machine manipulation algorithms in Python