https://github.com/hendrixstring/secret_exercise
https://github.com/hendrixstring/secret_exercise
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/hendrixstring/secret_exercise
- Owner: HendrixString
- Created: 2023-09-06T15:04:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-07T04:49:28.000Z (over 2 years ago)
- Last Synced: 2023-09-07T05:45:14.876Z (over 2 years ago)
- Language: Jupyter Notebook
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# State Machine Framework
An experiement with state machine modeling

## install
`pip3 install -e .`
## run test
`python3 -m unittest tests.test_machine`
## Usage
- **States** and **Events** can be any object.
- If state is a `callable` object, then it will be triggered to call side effects.
- You use states as nodes and events as edges.
Use the `add(from_state: object, event: object, to_state: object)` method of `Machine` class to model the state machine.
```python
from machine.machine import Machine
m = Machine("Start")
m.add("Start", "red", "R1") \
.add("Start", "yellow", "Y1") \
.add("R1", "red", "R2") \
.add("R1", "yellow", "Y1") \
.add("R2", "red", "R3") \
.add("R2", "yellow", "Y1") \
.add("R3", "red", "R4") \
.add("R3", "yellow", "Y1") \
.add("R4", "red", "R4") \
.add("R4", "yellow", "Y1") \
.add("Y1", "red", "R1") \
.add("Y1", "yellow", "Y2") \
.add("Y2", "red", "R1") \
.add("Y2", "yellow", "Y3") \
.add("Y3", "red", "R1") \
.add("Y3", "yellow", "Y4") \
.add("Y4", "red", "R1") \
.add("Y4", "yellow", "Y4")
```
Then, you can iterate a list of events with
```python
for state in m.iterate_input(['red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow']):
print(state)
# prints: R1, Y1, R1, Y1, R1, Y1, R1, Y1
```