Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally using wakepy modes #372

Open
fohrloop opened this issue Jun 29, 2024 · 0 comments
Open

Conditionally using wakepy modes #372

fohrloop opened this issue Jun 29, 2024 · 0 comments
Labels
Type: Feature New feature or request

Comments

@fohrloop
Copy link
Owner

fohrloop commented Jun 29, 2024

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"?
  • m.activation_result: An ActivationResult instance which gives more detailed information about the activation process. -- needs thinking

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

  • Design how to conditionally use wakepy modes
  • Implement the solution

Comments / suggestions welcome!

@fohrloop fohrloop added Type: Documentation Improvements or additions to documentation good first issue Good for newcomers labels Jun 29, 2024
@fohrloop fohrloop changed the title Docs: conditionally using wakepy modes Conditionally using wakepy modes Jun 29, 2024
@fohrloop fohrloop removed Type: Documentation Improvements or additions to documentation good first issue Good for newcomers labels Jun 29, 2024
@fohrloop fohrloop added the Type: Feature New feature or request label Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant