Mastering Python Design Patterns by Kamon Ayeva
 and Sakis Kasampalis

Mastering Python Design Patterns by Kamon Ayeva
 and Sakis Kasampalis

Author:Kamon Ayeva
 and Sakis Kasampalis
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2024-05-24T00:00:00+00:00


Here’s what we get when executing the Python ch05/state.py command:

state of process1: created state of process2: created process1 entered waiting mode Transition of process2 from created to terminated failed state of process1: waiting state of process2: created process1 is running process2 entered waiting mode state of process1: running state of process2: waiting process2 is running state of process1: running state of process2: running process1 is blocked process2 is blocked state of process1: blocked state of process2: blocked Transition of process1 from blocked to terminated failed Transition of process2 from blocked to terminated failed state of process1: blocked state of process2: blocked

Indeed, the output shows that illegal transitions such as created → terminated and blocked → terminated fail gracefully. We don’t want the application to crash when an illegal transition is requested, and this is handled properly by the except block.

Notice how using a good module such as state_machine eliminates conditional logic. There’s no need to use long and error-prone if…else statements that check for each and every state transition and react to them.

To get a better feeling for the state pattern and state machines, I strongly recommend you implement your own example. This can be anything: a simple video game (you can use state machines to handle the states of the main hero and the enemies), an elevator, a parser, or any other system that can be modeled using state machines.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.