python finite state machine