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

Add a convenience layer for creating option namespaces #12

Open
austinwarner-8451 opened this issue Aug 12, 2024 · 2 comments
Open

Add a convenience layer for creating option namespaces #12

austinwarner-8451 opened this issue Aug 12, 2024 · 2 comments
Assignees

Comments

@austinwarner-8451
Copy link
Collaborator

As application code becomes more complex, it can be useful to organize all of the possible options into a single source of truth, rather than declaring them all in-line. I am imagining something like this could be useful.

################################################################################
## pkg/options.py
################################################################################
import labrea


@labrea.options.namespace
class PKG:
    class MODULE_1:
        A: str
        B: int = 3

    class MODULE_2:
        C: list[str]
        D: bool


################################################################################
## pkg/module_1.py
################################################################################
from labrea import dataset
from ..options import PKG


@dataset
def a_repeated_b_times(
    a: str = PKG.MODULE_1.A
    b: int = PKG.MODULE_1.B
) -> list[str]:
    return [a] * b


a_repeated_b_times({'PKG': {'MODULE_1': {'A': 'foo'}}})  # ['foo', 'foo', 'foo']

Ideally there would also be a way to add a docstring to each option as well, which could potentially allow for the full option set of an application/library to be automatically documented with sphinx (perhaps requiring a plugin)

@richardhathaway-8451
Copy link

richardhathaway-8451 commented Jan 24, 2025

Something to consider when designing the namespace:

Currently, with a structure of classes to store Options, we can do something like this:

class PKG:
    class MODULE_1:
        A = Option('PKG.MODULE_1.A')
        B = Option('PKG.MODULE_1.B')
    class MODULE_2:
        A = Option('PKG.MODULE_2.A')
        B = Option('PKG.MODULE_2.B') >> func

where we can apply a basic cleaning/ingestion function on the Option definition itself rather than applying it every place where PKG.MODULE_2.B is called. Not sure if preserving this functionality would be possible in a namespace?

@austinwarner-8451 austinwarner-8451 self-assigned this Jan 27, 2025
@austinwarner-8451
Copy link
Collaborator Author

@richardhathaway-8451 I just pushed to feature/namespace. This is what I have so far

from labrea import Option

@Option.namespace
class PKG:
    class MOD1:
        A: str
        B: int = 3
    class MOD2:
        A = Option.auto() >> str
  • PKG is a namespace
  • PKG.MOD1 and PKG.MOD2 are also (sub)namespaces
  • PKG.MOD1.A is equivalent to Option("PKG.MOD1.A")
  • PKG.MOD1.B is equivalent to Option("PKG.MOD1.B", 3)
  • PKG.MOD2.A is equivalent to Option("PKG.MOD2.A") >> str

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants