Python Finite State Machine