State Machine python implementation