State machines in Python using generators