From d76f01c50b5dae57dc61cd22b3b24071dc4da84f Mon Sep 17 00:00:00 2001 From: Stefan Krawczyk Date: Fri, 13 Dec 2024 13:06:33 -0800 Subject: [PATCH] Fixes cowsay example --- docs/examples/simple/cowsay.ipynb | 280 +++++++++--------------------- 1 file changed, 78 insertions(+), 202 deletions(-) diff --git a/docs/examples/simple/cowsay.ipynb b/docs/examples/simple/cowsay.ipynb index edf53656..a65cb543 100644 --- a/docs/examples/simple/cowsay.ipynb +++ b/docs/examples/simple/cowsay.ipynb @@ -10,19 +10,17 @@ }, { "cell_type": "code", - "execution_count": 1, "id": "initial_id", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:12:50.561804Z", - "start_time": "2024-03-27T00:12:50.531039Z" - }, "collapsed": true, "jupyter": { "outputs_hidden": true + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:01:40.623161Z", + "start_time": "2024-12-13T21:01:39.314706Z" } }, - "outputs": [], "source": [ "import random\n", "import time\n", @@ -33,23 +31,23 @@ "from burr.core import Action, Application, ApplicationBuilder, State, default, expr\n", "from burr.core.action import action\n", "from burr.lifecycle import PostRunStepHook" - ] + ], + "outputs": [], + "execution_count": 1 }, { "cell_type": "code", - "execution_count": 3, "id": "9f5b9bfc78851a59", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:13:03.529875Z", - "start_time": "2024-03-27T00:13:03.524449Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:01:40.637573Z", + "start_time": "2024-12-13T21:01:40.632290Z" } }, - "outputs": [], "source": [ "# create some hooks!\n", "class PrintWhatTheCowSaid(PostRunStepHook):\n", @@ -66,28 +64,28 @@ " def post_run_step(self, *, state: \"State\", action: \"Action\", **future_kwargs):\n", " if action.name != \"cow_should_say\": # no need to print if we're not saying anything\n", " time.sleep(self.sleep_time)" - ] + ], + "outputs": [], + "execution_count": 2 }, { "cell_type": "code", - "execution_count": 4, "id": "5b29e9de2ca536b9", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:13:12.756137Z", - "start_time": "2024-03-27T00:13:12.750764Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:02:32.928090Z", + "start_time": "2024-12-13T21:02:32.923286Z" } }, - "outputs": [], "source": [ "# instantiate actions\n", "@action(reads=[], writes=[\"cow_said\"])\n", "def cow_said(state: State, say_what: list[str]) -> Tuple[dict, State]:\n", - " said = random.choice(say_what)\n", + " said = random.choice(say_what) if say_what is not None else None\n", " result = {\"cow_said\": cowsay.get_output_string(\"cow\", said) if say_what is not None else None}\n", " return result, state.update(**result)\n", "\n", @@ -96,23 +94,23 @@ "def cow_should_speak(state: State) -> Tuple[dict, State]:\n", " result = {\"cow_should_speak\": random.randint(0, 3) == 0}\n", " return result, state.update(**result)\n" - ] + ], + "outputs": [], + "execution_count": 7 }, { "cell_type": "code", - "execution_count": 5, "id": "271b868317fbbdf4", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:14:10.836543Z", - "start_time": "2024-03-27T00:14:10.829258Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:02:33.629009Z", + "start_time": "2024-12-13T21:02:33.613724Z" } }, - "outputs": [], "source": [ "# build application\n", "hooks = [\n", @@ -139,233 +137,111 @@ " .with_hooks(*hooks)\n", " .build()\n", ")" - ] + ], + "outputs": [], + "execution_count": 8 }, { "cell_type": "code", - "execution_count": 6, "id": "794bc0abda9e48ad", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:14:29.829383Z", - "start_time": "2024-03-27T00:14:29.511355Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:02:34.796777Z", + "start_time": "2024-12-13T21:02:34.484247Z" } }, + "source": [ + "app.visualize(include_conditions=True)" + ], "outputs": [ { "data": { - "image/svg+xml": [ - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "say_nothing\n", - "\n", - "say_nothing\n", - "\n", - "\n", - "\n", - "cow_should_speak\n", - "\n", - "cow_should_speak\n", - "\n", - "\n", - "\n", - "say_nothing->cow_should_speak\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "say_hello\n", - "\n", - "say_hello\n", - "\n", - "\n", - "\n", - "say_hello->cow_should_speak\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "cow_should_speak->say_nothing\n", - "\n", - "\n", - "not cow_should_speak\n", - "\n", - "\n", - "\n", - "cow_should_speak->say_hello\n", - "\n", - "\n", - "cow_should_speak\n", - "\n", - "\n", - "\n" - ], + "image/svg+xml": "\n\n\n\n\n\n\n\n\nsay_nothing\n\nsay_nothing\n\n\n\ncow_should_speak\n\ncow_should_speak\n\n\n\nsay_nothing->cow_should_speak\n\n\n\n\n\nsay_hello\n\nsay_hello\n\n\n\nsay_hello->cow_should_speak\n\n\n\n\n\ncow_should_speak->say_nothing\n\n\nnot cow_should_speak\n\n\n\ncow_should_speak->say_hello\n\n\ncow_should_speak\n\n\n\n", "text/plain": [ - "" + "" ] }, - "execution_count": 6, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], - "source": [ - "app.visualize(include_conditions=True)" - ] + "execution_count": 9 }, { "cell_type": "code", - "execution_count": 11, "id": "2facfb753233cd7d", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:16:40.234406Z", - "start_time": "2024-03-27T00:16:32.211734Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:02:43.025443Z", + "start_time": "2024-12-13T21:02:35.007367Z" } }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n" - ] - } - ], "source": [ "# run things!\n", "for i in range(0, 4):\n", " # step through 4 times\n", " a_obj, result, state = app.step()" - ] + ], + "outputs": [], + "execution_count": 10 }, { "cell_type": "code", - "execution_count": 16, "id": "fed65721e7cc457a", "metadata": { - "ExecuteTime": { - "end_time": "2024-03-27T00:17:18.640978Z", - "start_time": "2024-03-27T00:17:10.619878Z" - }, "collapsed": false, "jupyter": { "outputs_hidden": false + }, + "ExecuteTime": { + "end_time": "2024-12-13T21:02:51.055716Z", + "start_time": "2024-12-13T21:02:43.033412Z" } }, + "source": [ + "# Run some more\n", + "for i in range(0, 4):\n", + " # step through 4 times\n", + " a_obj, result, state = app.step()" + ], "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - " ________________________\n", - "| Are you Aaron Burr, sir? |\n", - " ========================\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ________________________\n", - "| Are you Aaron Burr, sir? |\n", - " ========================\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n", - " ____________\n", - "| Hello world! |\n", - " ============\n", - " \\\n", - " \\\n", - " ^__^\n", - " (oo)\\_______\n", - " (__)\\ )\\/\\\n", - " ||----w |\n", - " || ||\n" + " __________\n", + "| What's up? |\n", + " ==========\n", + " \\\n", + " \\\n", + " ^__^\n", + " (oo)\\_______\n", + " (__)\\ )\\/\\\n", + " ||----w |\n", + " || ||\n", + " __________\n", + "| What's up? |\n", + " ==========\n", + " \\\n", + " \\\n", + " ^__^\n", + " (oo)\\_______\n", + " (__)\\ )\\/\\\n", + " ||----w |\n", + " || ||\n" ] } ], - "source": [ - "# Run some more\n", - "for i in range(0, 4):\n", - " # step through 4 times\n", - " a_obj, result, state = app.step()" - ] + "execution_count": 11 }, { "cell_type": "code",