datatree icon indicating copy to clipboard operation
datatree copied to clipboard

implement open_datatree context manager

Open malmans2 opened this issue 2 years ago • 4 comments

  • [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.

malmans2 avatar Jun 17 '22 13:06 malmans2

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)

TomNicholas avatar Jun 17 '22 15:06 TomNicholas

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!

malmans2 avatar Jun 17 '22 15:06 malmans2

@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?

malmans2 avatar Jun 20 '22 14:06 malmans2

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?

TomNicholas avatar Jun 20 '22 17:06 TomNicholas