It's probably quite usual use case to do something like:
if some_flag:
with keep.running():
do_something()
else:
do_something()
I thought first that there could be some conditional flag added to the modes, like
with keep.running(only_if=some_flag):
do_something()
but that raises questions like: What does "success" mean here is the flag is set and the mode is not entered?
It would be probably enough to document a recipe for conditionally using a wakepy mode using the solution from the standard library: nullcontext -- which conveniently supports Python 3.7+ (all versions which wakepy supports). The recipe is:
mode = keep.running() if some_flag else contextlib.nullcontext()
with mode:
do_something()
EDIT: The nullcontext is not a 100% drop-in replacement as you cannot do, for example:
mode = keep.running() if some_flag else contextlib.nullcontext()
with mode as m:
print('Used method:', m.used_method)
do_something()
because the with statement target (m) does will be a Mode instance when using keep.running() and None by default when using the nullcontext. As the nullcontext gives any input argument as the with statement target, there it could be:
import contextlib
from wakepy import keep, DummyMode
mode = keep.running() if some_flag else contextlib.nullcontext(DummyMode())
with mode:
do_something()
This already needs two imports and requires that the user knows about the contextlib.nullcontext. It would be probably more user-friendly to provide the dummy / no-op mode from wakepy, so one could write:
from wakepy import keep, DummyMode
mode = keep.running() if some_flag else DummyMode()
with mode:
do_something()
Questions to be answered:
1. What is the nicest name for this?
DummyMode -> usage: DummyMode()
dummy_mode -> usage: dummy_mode() (function)
dummy_mode -> usage: dummy_mode (instance of Mode. Does nothing. Reusable)
dummy or dummy() or dummymode or dummymode()
noop: usage: noop() or noop
noop_mode / noop_mode()
others?
2. What attributes/attribute values the DummyMode should have?
From the docs, the Modes have the following attributes (among others):
m.active: True, if entering mode was successful. --> Should be True?
m.active_method: The name of the active method. Will be None after mode is deactivated. -- should be same as in used_method
m.used_method: The name of the used method. Will not be reset to None after deactivation. -- should be just "Dummy" or "No-op"?
It's probably quite usual use case to do something like:
I thought first that there could be some conditional flag added to the modes, like
but that raises questions like: What does "success" mean here is the flag is set and the mode is not entered?
It would be probably enough to document a recipe for conditionally using a wakepy mode using the solution from the standard library: nullcontext -- which conveniently supports Python 3.7+ (all versions which wakepy supports). The recipe is:
EDIT: The nullcontext is not a 100% drop-in replacement as you cannot do, for example:
because the with statement target (
m
) does will be a Mode instance when usingkeep.running()
and None by default when using thenullcontext
. As thenullcontext
gives any input argument as the with statement target, there it could be:This already needs two imports and requires that the user knows about the
contextlib.nullcontext
. It would be probably more user-friendly to provide the dummy / no-op mode from wakepy, so one could write:Questions to be answered:
1. What is the nicest name for this?
DummyMode
-> usage:DummyMode()
dummy_mode
-> usage:dummy_mode()
(function)dummy_mode
-> usage:dummy_mode
(instance of Mode. Does nothing. Reusable)dummy
ordummy()
ordummymode
ordummymode()
noop
: usage:noop()
ornoop
noop_mode
/noop_mode()
2. What attributes/attribute values the DummyMode should have?
From the docs, the Modes have the following attributes (among others):
3. Is this any better than using
keep.running(only_if=some_flag)
?This is starting to become more complicated. Would the initial idea be simper from the user's perspective?
Task
Comments / suggestions welcome!