datatree
datatree copied to clipboard
implement open_datatree context manager
- [x] Closes #93
- [ ] Tests added
- [x] Passes
pre-commit run --all-files
- [ ] New functions/methods are listed in
api.rst
- [ ] Changes are summarized in
docs/source/whats-new.rst
I haven't added any test, but the io tests now use the context manager. Not sure if there's a better way to test this functionality.
do not look at parent
Did you get a RecursionError
when you tried to close the parent too? I've done that before :sweat_smile:
This seems good though. For testing xarray must have some tests for this type of opening that we could copy.
I'm trying to think if there are any other gotchas with tree structures... .close()
closing that node and all children seems fine, though I would like to be clear about what happens if you refer to a closed child via its parent:
subtree = open_datatree(file)
dt = DataTree(children={"folder1": subtree})
dt["folder1"].close()
print(dt)
Did you get a RecursionError when you tried to close the parent too? I've done that before 😅
Yes, which made me realise that I don't think closing parents is the right thing to do. I.e., to close everything users must close from root.
This seems good though. For testing xarray must have some tests for this type of opening that we could copy.
That's what I thought too, but I couldn't find anything. I'll take a better look next week, but let me know if you know where I can find those tests.
I'm trying to think if there are any other gotchas with tree structures... .close() closing that node and all children seems fine, though I would like to be clear about what happens if you refer to a closed child via its parent:
Good point - I'll think about this as well!
@TomNicholas I've added open=True/False
to the repr. Not sure if this is the best way to go though. What do you think?
I've added open=True/False to the repr. Not sure if this is the best way to go though. What do you think?
Hmmm... that seems a bit janky to me, especially as Dataset
doesn't do that.
I remember talking about this problem briefly with @jhamman - perhaps he has some suggestions as to how to handle multiple file handles?