-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds other examples (less validated)
t
- Loading branch information
1 parent
d7c7f34
commit 18d7c25
Showing
10 changed files
with
678 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Cowsay | ||
|
||
This is an example of a simple infinite state machine. | ||
|
||
We have three files: | ||
|
||
- [application.py](application.py) -- This contains a mainline to run the cowsay app as well as a function to export the app (for later use) | ||
- [requirements.txt](requirements.txt) -- Just the requirements. All this needs is Burr/Streamlit/cowsay | ||
- [streamlit_app.py](streamlit_app.py) -- This contains a simple Streamlit app to interact with the cow | ||
|
||
To run just the application, you can run: | ||
|
||
```bash | ||
python application.py | ||
``` | ||
|
||
Note this is an infinte state machine, so this will run forever! Thus remember to ctrl-c eventually. | ||
To run the streamlit app, you can run: | ||
|
||
```bash | ||
streamlit run streamlit_app.py | ||
``` | ||
|
||
This allows you to press a button and see the cow say something (or see it decide not to speak). | ||
|
||
This will open a chrome window and print out the URL. The state machine this encapsulates takes the following form: | ||
|
||
![State Machine](digraph.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import random | ||
import time | ||
from typing import Tuple | ||
|
||
import cowsay | ||
|
||
from burr.core import Action, Application, ApplicationBuilder, State, default, expr | ||
from burr.core.action import action | ||
from burr.lifecycle import PostRunStepHook | ||
|
||
|
||
class PrintWhatTheCowSaid(PostRunStepHook): | ||
def post_run_step(self, *, state: "State", action: "Action", **future_kwargs): | ||
if action.name != "cow_should_say" and state["cow_said"] is not None: | ||
print(state["cow_said"]) | ||
|
||
|
||
class CowCantSpeakFast(PostRunStepHook): | ||
def __init__(self, sleep_time: float): | ||
super(PostRunStepHook, self).__init__() | ||
self.sleep_time = sleep_time | ||
|
||
def post_run_step(self, *, state: "State", action: "Action", **future_kwargs): | ||
if action.name != "cow_should_say": # no need to print if we're not saying anything | ||
time.sleep(self.sleep_time) | ||
|
||
|
||
@action(reads=[], writes=["cow_said"]) | ||
def cow_said(state: State, say_what: list[str]) -> Tuple[dict, State]: | ||
said = random.choice(say_what) | ||
result = {"cow_said": cowsay.get_output_string("cow", said) if say_what is not None else None} | ||
return result, state.update(**result) | ||
|
||
|
||
@action(reads=[], writes=["cow_should_speak"]) | ||
def cow_should_speak(state: State) -> Tuple[dict, State]: | ||
result = {"cow_should_speak": random.randint(0, 3) == 0} | ||
return result, state.update(**result) | ||
|
||
|
||
def application(in_terminal: bool = False) -> Application: | ||
hooks = ( | ||
[ | ||
PrintWhatTheCowSaid(), | ||
CowCantSpeakFast(sleep_time=2.0), | ||
] | ||
if in_terminal | ||
else [] | ||
) | ||
return ( | ||
ApplicationBuilder() | ||
.with_state(cow_said=None) | ||
.with_actions( | ||
say_nothing=cow_said.bind(say_what=None), | ||
say_hello=cow_said.bind( | ||
say_what=["Hello world!", "What's up?", "Are you Aaron Burr, sir?"] | ||
), | ||
cow_should_speak=cow_should_speak, | ||
) | ||
.with_transitions( | ||
("cow_should_speak", "say_hello", expr("cow_should_speak")), | ||
("say_hello", "cow_should_speak", default), | ||
("cow_should_speak", "say_nothing", expr("not cow_should_speak")), | ||
("say_nothing", "cow_should_speak", default), | ||
) | ||
.with_entrypoint("cow_should_speak") | ||
.with_hooks(*hooks) | ||
.build() | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
app = application(in_terminal=True) | ||
app.visualize(output_file_path="digraph", include_conditions=True, view=True, format="png") | ||
while True: | ||
action, result, state = app.step() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
digraph { | ||
graph [compound=false concentrate=false rankdir=TB ranksep=0.4] | ||
say_nothing [label=say_nothing shape=box style=rounded] | ||
say_hello [label=say_hello shape=box style=rounded] | ||
cow_should_speak [label=cow_should_speak shape=box style=rounded] | ||
cow_should_speak -> say_hello [label=cow_should_speak style=dashed] | ||
say_hello -> cow_should_speak [style=solid] | ||
cow_should_speak -> say_nothing [label="not cow_should_speak" style=dashed] | ||
say_nothing -> cow_should_speak [style=solid] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.