From 6e1d34b28a1723e743a1040298a70815740b1c5f Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 22 Jul 2024 11:42:00 -0400 Subject: [PATCH 01/25] set python to inherit --- .idea/misc.xml | 2 +- .idea/social-norms-trees.iml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 39365f40..d2e56a2f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,5 +6,5 @@ - + \ No newline at end of file diff --git a/.idea/social-norms-trees.iml b/.idea/social-norms-trees.iml index 524e18fb..af3ee449 100644 --- a/.idea/social-norms-trees.iml +++ b/.idea/social-norms-trees.iml @@ -5,7 +5,7 @@ - + From 19ac2d1559fd3fdd900b3966cf76f98a94bf3e04 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 22 Jul 2024 11:42:15 -0400 Subject: [PATCH 02/25] add start of chat module --- src/social_norms_trees/chat.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/social_norms_trees/chat.py diff --git a/src/social_norms_trees/chat.py b/src/social_norms_trees/chat.py new file mode 100644 index 00000000..d5a4540b --- /dev/null +++ b/src/social_norms_trees/chat.py @@ -0,0 +1,5 @@ + + + +if __name__ == "__main__": + pass \ No newline at end of file From 8eaccc217c8b00ee4d9d2d03b14407bc1ef5c679 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 22 Jul 2024 12:46:58 -0400 Subject: [PATCH 03/25] =?UTF-8?q?add=20framework=20for=20chat=20module=20?= =?UTF-8?q?=E2=80=93=20can=20do=20updates=20to=20behavior=20list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 4 +- src/social_norms_trees/chat.py | 99 +++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index de793197..2373b03e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -78,7 +78,9 @@ classifiers = [ # Optional dependencies = [ # Optional "py_trees", - "websockets" + "websockets", + "click", + "typer", ] # List additional groups of dependencies here (e.g. development diff --git a/src/social_norms_trees/chat.py b/src/social_norms_trees/chat.py index d5a4540b..6f5fc199 100644 --- a/src/social_norms_trees/chat.py +++ b/src/social_norms_trees/chat.py @@ -1,5 +1,102 @@ +import time +from dataclasses import dataclass, replace +import itertools +from collections import namedtuple +from pprint import pprint +from typing import Callable, List, Iterable, Optional +import click +@dataclass +class World: + state: int + behavior: List["Behavior"] + + +@dataclass +class Behavior: + name: str + message: str + callback: Callable[["World"], "World"] + + +all_behaviors = [ + Behavior("add_one", "Add one to the state", add_one := lambda w: replace(w, state=w.state + 1)), + Behavior("add_two", "Add two to the state", add_two := lambda w: replace(w, state=w.state + 2)), +] + + +def get_behavior_name(callback: Callable[["World"], "World"]) -> str: + name = callback.__name__ + return name + + +def get_behavior_message(callback: Callable[["World"], "World"]) -> str: + try: + message = str.split(callback.__doc__, "\n")[0] + assert message != "" + except (IndexError, TypeError, AssertionError): # docstring is empty or non-existent + message = get_behavior_name(callback) + + return message + + +def register_behavior(callback: Callable[["World"], "World"]): + """Register a behavior to the list of all behaviors + + Introspects to get the message to be included with the behavior from the top line of the + docstring. + """ + name = get_behavior_name(callback) + message = get_behavior_message(callback) + all_behaviors.append(Behavior(name, message, callback)) + return callback + + +@register_behavior +def add_behavior(world: World, behavior: Optional[Behavior] = None, index: Optional[int] = None): + """Add a behavior""" + if behavior is None: + behavior_text = click.prompt(text="Which behavior would you like to add?", + type=click.Choice([b.message for b in all_behaviors])) + behavior = next(b for b in all_behaviors if b.message == behavior_text) + if index is None: + index = click.prompt(text="Where would you like to insert it?", type=int) + + new_behavior = world.behavior[:index] + [behavior.callback] + world.behavior[index:] + new_world = replace(world, behavior=new_behavior) + return new_world + + +@register_behavior +def remove_behavior(world: World, index: Optional[int] = None): + """Remove a behavior""" + if index is None: + behavior_listing = "\n".join([f"{i}: {get_behavior_message(b)}" for i, b in enumerate( + world.behavior)]) + index = click.prompt(text="Which behavior would you like to remove?\n" + behavior_listing + +"\n", + type=int) + new_behavior = world.behavior[:index] + world.behavior[index + 1:] + new_world = replace(world, behavior=new_behavior) + return new_world + +@register_behavior +def print_world(world: World): + """""" + + pprint(world) + return + +def main(world): + while True: + for behavior in world.behavior: + result = behavior(world) + if result is not None and isinstance(result, type(world)): + world = result + print_world(world) + if __name__ == "__main__": - pass \ No newline at end of file + world = World(state=0, behavior=[remove_behavior, add_one, add_behavior]) + main(world) From 5fdb462c4cb5f2ec8a984cb450e48181c9f4ab35 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 22 Jul 2024 12:50:52 -0400 Subject: [PATCH 04/25] reformatc --- src/social_norms_trees/chat.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/social_norms_trees/chat.py b/src/social_norms_trees/chat.py index 6f5fc199..dae3ca1c 100644 --- a/src/social_norms_trees/chat.py +++ b/src/social_norms_trees/chat.py @@ -75,13 +75,14 @@ def remove_behavior(world: World, index: Optional[int] = None): if index is None: behavior_listing = "\n".join([f"{i}: {get_behavior_message(b)}" for i, b in enumerate( world.behavior)]) - index = click.prompt(text="Which behavior would you like to remove?\n" + behavior_listing - +"\n", + text = "Which behavior would you like to remove?\n" + behavior_listing + "\n" + index = click.prompt(text=text, type=int) new_behavior = world.behavior[:index] + world.behavior[index + 1:] new_world = replace(world, behavior=new_behavior) return new_world + @register_behavior def print_world(world: World): """""" @@ -89,6 +90,7 @@ def print_world(world: World): pprint(world) return + def main(world): while True: for behavior in world.behavior: @@ -97,6 +99,7 @@ def main(world): world = result print_world(world) + if __name__ == "__main__": world = World(state=0, behavior=[remove_behavior, add_one, add_behavior]) main(world) From afa3e15b5765f9bfba5892185323b20a665a7338 Mon Sep 17 00:00:00 2001 From: John Gerrard Holland Date: Mon, 22 Jul 2024 12:51:27 -0400 Subject: [PATCH 05/25] always use black --- .idea/misc.xml | 6 +++++- src/social_norms_trees/chat.py | 38 ++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d2e56a2f..f8e8589b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,11 @@ -