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 contextlib support to the pyhf.schema API #1815

Closed
kratsg opened this issue Mar 22, 2022 · 1 comment · Fixed by #1818
Closed

Add contextlib support to the pyhf.schema API #1815

kratsg opened this issue Mar 22, 2022 · 1 comment · Fixed by #1818
Assignees
Labels
API Changes the public API feat/enhancement New feature or request

Comments

@kratsg
Copy link
Contributor

kratsg commented Mar 22, 2022

instead of having users overwriting the value by assignment, do you think there is value in offering a pyhf.utils.update_schema_path or something that performs this operation through the API?

Thanks @kratsg for taking up my suggestion!

I second @matthewfeickert's suggestion to change schemas via function call rather than assignment. It is much simpler to add functionality to a function if it ever becomes necessary, than to replace an entire existing module with a class instance with a property.

I'd even go so far to say that a context manager doubling as an update function would be ideal IMO:

# in pyhf.utils
_SCHEMAS = Path(...)
class use_schema_path: # snake_case to remind of function-like usage
    def  __init__(self, path):
        global _SCHEMAS
        self._old_schemas = _SCHEMAS
        _SCHEMAS = pathlib.Path(path)
    def __enter__(self):
        pass
    def __exit__(self, *args, **kwargs):
        global _SCHEMAS
        _SCHEMAS = self._old_schemas

which can still be called as a function (only executing __init__), so short scripts etc. are not forced to use with blocks.
But it can also be used like so:

def make_my_workspace(spec):
    with pyhf.utils.use_schema_path('/my/very/special/schemas'):
        return pyhf.Workspace(spec)

So as a user writing code on top of pyhf, I don't have to worry about resesetting the global variable, the CM does it for me, and there are fewer mistakes to make.

Originally posted by @lhenkelm in #1753 (comment)

@kratsg kratsg self-assigned this Mar 23, 2022
@kratsg kratsg added feat/enhancement New feature or request API Changes the public API labels Mar 23, 2022
@kratsg
Copy link
Contributor Author

kratsg commented Mar 23, 2022

This is on top of #1609 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes the public API feat/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant