finite state machine built in python