Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds syntactic sugar for inverting conditions

elijahbenizzy59888f10da4e
diff --git a/burr/core/action.py b/burr/core/action.py
index a7e330fa..c896cf3e 100644
--- a/burr/core/action.py
+++ b/burr/core/action.py
@@ -174,6 +174,8 @@ class Condition(Function):
def __init__(self, keys: List[str], resolver: Callable[[State], bool], name: str = None):
"""Base condition class. Chooses keys to read from the state and a resolver function.
+ Note that the ``~`` operator allows you to automatically invert the condition.
+
:param keys: Keys to read from the state
:param resolver: Function to resolve the condition to True or False
:param name: Name of the condition
@@ -266,6 +268,9 @@ class Condition(Function):
def name(self) -> str:
return self._name
+ def __invert__(self):
+ return Condition(self._keys, lambda state: not self._resolver(state), name=f"~{self._name}")
+
# Annotated inline as linters don't understand classmethods + properties
# These, however, are allowed up until python 3.13
diff --git a/docs/concepts/transitions.rst b/docs/concepts/transitions.rst
index 5ab9d336..efd19175 100644
--- a/docs/concepts/transitions.rst
+++ b/docs/concepts/transitions.rst
@@ -29,7 +29,7 @@ Conditions have a few APIs, but the most common are the three convenience functi
from burr.core import when, expr, default
with_transitions(
("from", "to", when(foo="bar"), # will evaluate when the state has the variable "foo" set to the value "bar"
- ("from", "to", expr('epochs>100')) # will evaluate to True when the state has the variable "foo" set to the value "bar"
+ ("from", "to", expr('epochs>100')) # will evaluate to True when epochs is greater than 100
("from", "to", default) # will always evaluate to True
("from", "to") # leaving out a third conditions we allow defaults
)
@@ -38,6 +38,16 @@ Conditions have a few APIs, but the most common are the three convenience functi
Conditions are evaluated in the order they are specified, and the first one that evaluates to True will be the transition that is selected
when determining which action to run next. If no condition evaluates to ``True``, the application execution will stop early.
+The ``~`` operator will invert a condition. For instance:
+
+.. code-block:: python
+
+ from burr.core import when, expr
+ with_transitions(
+ ("from", "to", ~when(foo="baz"), # will evaluate to True when foo != baz
+ ("from", "to", ~expr('epochs<=100')) # will evaluate to True when epochs hits 101
+ )
+
All keys present in the condition (E.G. ``foo`` and ``epochs`` above) must be present in the state for the condition to work. It will error otherwise.
.. note::
diff --git a/tests/core/test_action.py b/tests/core/test_action.py
index da890afc..01035f03 100644
--- a/tests/core/test_action.py
+++ b/tests/core/test_action.py
@@ -138,6 +138,19 @@ def test_condition__validate_failure():
cond._validate(State({"baz": "baz"}))
+def test_condition_invert():
+ cond = Condition(
+ ["foo"],
+ lambda state: state["foo"] == "bar",
+ name="foo == 'bar'",
+ )
+ cond_inverted = ~cond
+ assert cond_inverted.name == "~foo == 'bar'"
+ assert cond_inverted.reads == ["foo"]
+ assert cond_inverted.run(State({"foo": "bar"})) == {Condition.KEY: False}
+ assert cond_inverted.run(State({"foo": "baz"})) == {Condition.KEY: True}
+
+
def test_result():
result = Result("foo", "bar")
assert result.run(State({"foo": "baz", "bar": "qux", "baz": "quux"})) == {