-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
Context manager for SPICE kernels #454
Comments
hey @alfonsoSR thanks for writing in. I think in the past I have been asked about adding context managers, specifically #413, that attempted this with larger scope but the author appears to have lost interest in completing that contribution. There may have been too much scope creep in that PR, so maybe you can take that PR and pare it down to the above example? note the concept of kernel management in the contribution guidelines is intended to avoid adding tools that download kernels for users, so this would be in scope |
@AndrewAnnex I didn't notice there already was an issue regarding this topic. Now that I've read it, I have a couple of questions:
Just want to know your opinion before starting to work on it. |
Hey @alfonsoSR, sorry I have been very busy and hadn't had time to think about this, here are my responses:
Otherwise, I have a few suggestions/questions:
Number 3 above gives me a lot of pause on this idea due to the possible complexity involved, but provided enough unit tests demonstrating the functionality I could be convinced... maybe... |
Hi @AndrewAnnex, it's been a while since you answered me, sorry for the delay. I've also been a bit busy this last month. Using your suggestions as a reference, I have created a pull request with a working version of the context manager. Let me develop a bit on what I have done: Context manager should be able to accept variable number of strings/paths.I decided to use a list of strings as input for the context manager. The members of this list may be either paths to individual kernels, paths to meta-kernel files, or a combination of both. Additionaly, the list can be directly passed as an argument for Using contextlib.AbstractContextManager as base classThough I initially intended to implement the context manager as a class with three methods: Following the suggestion in the accepted answer, I decided to rewrite the context manager using the Using unload instead of kclearThough I initially thought that using kernel_list = ["kernel_A", "kernel_A", "kernel_B"]
furnsh(kernel_list)
ktotal("all") = 3
# Using unload
unload(kernel_list)
ktotal("all") = 1
kdata = ["kernel_A"]
# Using kclear
kclear()
ktotal = 0 The reason for this unexpected behaviour might have something to do with the following excerpts of documentation:
If Make the context manager side-effect freeAccording to what I have read, the issue with mixing kernels that where loaded before the with statement (global kernels) and those that where loaded by the context manager (local kernels), can be solved using the solution you suggested. I used The only issue with clearing, and then restoring the kernel database is mentioned in the following excerpt of the kernel required reading:
I cannot think about a reasonable way to prevent this issue, but loading and unloading kernels in a regular way is likely to be more convenient than using a context manager for this specific case. Explicitly mentioning this behaviour in the documentation is what I would do. Changing directory inside the context managerThis problem is also mentioned in the kernel required reading:
|
hey @alfonsoSR thanks for the detailed response, I've left some comments on the PR that need to be addressed but I'll address some of your response here first. Agree about worrying about changing directories to be out of scope, users should ensure they pass in absolute paths to kernels in general. Secondly after looking into it a bit more I agree now with using kclear, although we could detect the type of kernel file, text kernels are so commonly used calling unload on them would end up having the same effect as kclear. These caveats should be explained in a new docs file and in the docstrings for these functions (see other context manager PR for example...) to make it very clear to users what things to keep in mind. |
Hi @AndrewAnnex , thanks for the feedback! I agree with the need to create documentation for the context manager. In fact, if we want people to know that the context manager exists, it would be great to update current function usage examples to include it. I am also open to work on that, but it is better to do things one at a time. I don't see any comments in the pull request (leaving the codecov report aside). Have you already left them, or do you intend to do it? There's no rush, I am just asking :). |
@alfonsoSR sorry forgot to submit the review, mostly formatting but a few things to adjust also |
@alfonsoSR thanks for the contribution! I've merged your pr in #458, closing this issue. |
Note
I've read the contributing guidelines and am aware that Kernels' management interfaces are out of your scope. That being said, this could be implemented in less than a minute, and it has improved my personal experience using this library.
Is your feature request related to a problem? Please describe.
I've been working with spicepy for the last year, and one of the only things that annoys me about it is having to use a
try-except
ortry-finally
scheme in order to assure that kernels are properly closed, even if an exception is raised. Let me show a litte example:Describe the solution you'd like
I'd like to be able to use a context manager to automatically load and unload kernels.
Describe alternatives you've considered
I've been working with a custom, and pretty simple, context manager that looks like this:
What are your thoughts on this? I'd love to help to implement it if you were open to it.
The text was updated successfully, but these errors were encountered: