-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 close() method to DataTree and use it to clean-up open files in tests #9651
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -582,7 +582,7 @@ def _to_dataset_view(self, rebuild_dims: bool, inherit: bool) -> DatasetView: | |
attrs=self._attrs, | ||
indexes=dict(self._indexes if inherit else self._node_indexes), | ||
encoding=self._encoding, | ||
close=None, | ||
close=self._close, | ||
) | ||
|
||
@property | ||
|
@@ -796,6 +796,20 @@ def _repr_html_(self): | |
return f"<pre>{escape(repr(self))}</pre>" | ||
return datatree_repr_html(self) | ||
|
||
def __enter__(self) -> Self: | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback) -> None: | ||
self.close() | ||
|
||
def close(self): | ||
for node in self.subtree: | ||
node.dataset.close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to add a test to verify that calling close() repeatedly does not raise an error. (Dataset.close is not idempotent, because it replaces _close with None, but the dataset objects on which I'm calling it here are not persistent. Probably I should also make DatasetView.close raise an error.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def set_close(self, closers: Mapping[str, Callable[[], None] | None], /) -> None: | ||
for path, close in closers.items(): | ||
self[path]._close = close | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another option would be to only set the closer on the root node, similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean you can presumably pull out the dataset from that node and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The datasets created by DataTree.dataset are ephemeral, so that wouldn't work. (I think I should probably change this to only act at the local node level) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm modified |
||
|
||
def _replace_node( | ||
self: DataTree, | ||
data: Dataset | Default = _default, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done