State Machine implementation in Python