From 1c1154c3f99166d30d1629b092efd3b8d968c79c Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 17 Feb 2022 17:37:48 +0000 Subject: [PATCH 1/4] lisa.wlgen.rta: Fix with_props() examples FIX Fix examples: with_props(prop_X=...) should be with_props(X=...) --- lisa/wlgen/rta.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lisa/wlgen/rta.py b/lisa/wlgen/rta.py index 0d1dcb6cf..0dda68469 100644 --- a/lisa/wlgen/rta.py +++ b/lisa/wlgen/rta.py @@ -1606,8 +1606,8 @@ def leaf_precedence(val, **kwargs): """ Give precedence to the leaf values when combining with ``&``:: - phase = phase.with_props(prop_meta=({'hello': 'leaf'}) - phase = phase.with_props(prop_meta=leaf_precedence({'hello': 'root'}) + phase = phase.with_props(meta=({'hello': 'leaf'}) + phase = phase.with_props(meta=leaf_precedence({'hello': 'root'}) assert phase['meta'] == {'hello': 'leaf'} This allows setting a property with some kind of default value on a root @@ -1633,7 +1633,7 @@ def override(val, **kwargs): Override a property with the given value, rather than combining it with the property-specific ``&`` implementation:: - phase = phase.with_props(prop_cpus=override({1,2})) + phase = phase.with_props(cpus=override({1,2})) """ return _OverridingValue(val, **kwargs) @@ -1687,7 +1687,7 @@ def delete(): """ Remove the given property from the phase:: - phase = phase.with_props(prop_cpus=delete()) + phase = phase.with_props(cpus=delete()) """ return _DeletingValue() -- GitLab From 2296abe348f1173f917065910b27b3d07ad54e0f Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 17 Feb 2022 16:55:12 +0000 Subject: [PATCH 2/4] lisa.monad: Fix StateMonad docstring FIX Fix documentation on the return value of the function passed to StateMonad.__init__ . --- lisa/monad.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lisa/monad.py b/lisa/monad.py index 6e66ee11c..e1d7f9cec 100644 --- a/lisa/monad.py +++ b/lisa/monad.py @@ -66,8 +66,8 @@ class StateMonad(abc.ABC): """ The state monad. - :param f: Callable that takes the state as parameter and returns an - instance of the monad. + :param f: Callable that takes the state as parameter and returns a tuple + ``(value, new_state)``. :type f: collections.abc.Callable """ -- GitLab From 40531341510badbbd2f8a2d4edee024fa6ec755a Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 17 Feb 2022 17:07:07 +0000 Subject: [PATCH 3/4] lisa.monad: Avoid passing "name" to StateMonad subclasses' __init__ FIX StateMonad does not mandate __init__ to accept a "name" parameter, so avoid passing one to it. --- lisa/fuzz.py | 2 +- lisa/monad.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lisa/fuzz.py b/lisa/fuzz.py index 4f91b61d8..aebbb3fd1 100644 --- a/lisa/fuzz.py +++ b/lisa/fuzz.py @@ -153,7 +153,7 @@ class Gen(StateMonad, Loggable): self.logger.log(log_level, f'Drawn {val}{trials}from {self}{info}') return x - self.name = name + self.name = name or f.__qualname__ super().__init__(wrapper) class _STATE: diff --git a/lisa/monad.py b/lisa/monad.py index e1d7f9cec..07a4bc214 100644 --- a/lisa/monad.py +++ b/lisa/monad.py @@ -137,6 +137,7 @@ class StateMonad(abc.ABC): call = lambda: _f(*args, **kwargs) x = call() if inspect.iscoroutine(x): + @functools.wraps(f) def body(state): if inspect.getcoroutinestate(x) == inspect.CORO_CLOSED: _x = call() @@ -167,7 +168,7 @@ class StateMonad(abc.ABC): else: return (val, state) - val = cls.from_f(body, name=f.__qualname__) + val = cls.from_f(body) else: if isinstance(x, cls): val = x -- GitLab From 6fcf6b5d0077949a9d930393455dccd1c03a84cb Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Thu, 17 Feb 2022 17:26:56 +0000 Subject: [PATCH 4/4] lisa.monad: Add StateMonad.run() FEATURE Allow getting the return value and the state when running the monad using StateMonad.run() --- lisa/monad.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lisa/monad.py b/lisa/monad.py index 07a4bc214..22ef07e29 100644 --- a/lisa/monad.py +++ b/lisa/monad.py @@ -86,10 +86,16 @@ class StateMonad(abc.ABC): return (yield self) def __call__(self, *args, **kwargs): - state = self.make_state(*args, **kwargs) - x, _ = self._f(state) + x, _ = self.run(*args, **kwargs) return x + def run(self, *args, **kwargs): + """ + Run the state monad and return a tuple of ``(value, state)``. + """ + state = self.make_state(*args, **kwargs) + return self._f(state) + def __init_subclass__(cls, **kwargs): # The one inheriting directly from StateMonad is the base of the # hierarchy -- GitLab