Skip to content

Allow passing a h5py file when opening a file - #238

Merged
jl-wynen merged 5 commits into
mainfrom
allow-passing-h5py-file
Sep 11, 2024
Merged

jl-wynen merged 5 commits into
mainfrom
allow-passing-h5py-file

Conversation

@jl-wynen

@jl-wynen jl-wynen commented Sep 9, 2024

Copy link
Copy Markdown
Member

Fixes #236

@nvaytet nvaytet self-assigned this Sep 10, 2024
Comment thread src/scippnexus/file.py Outdated
Parameters
----------
name:
Path, Bytes object, or h5py.Group.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave this line out if it's already specified in the type hints? Would avoid it accidentally becoming out of date if we change something in the future...

Comment thread src/scippnexus/file.py
class File(AbstractContextManager, Group):
def __init__(
self,
name: str | os.PathLike[str] | io.BytesIO | h5py.Group,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file is open, is it a h5py.Group or a h5py.File?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h5py.File. But that is a subclass of h5py.group. So this annotation is enough and also supports passing in a subgroup.

Comment thread tests/file_test.py


@pytest.mark.parametrize('path_type', [str, Path])
def test_load_entry_from_filename(tmp_path, path_type):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmp_path: I hate pytest and all its hidden conventions/pre-defined objects. It took me a while to figure out where tmp_path was defined... :-(

Comment thread tests/file_test.py Outdated
Comment thread tests/file_test.py


def test_load_entry_from_h5py_group_toor(tmp_path):
with h5.File('test.nxs', 'w', driver='core', backing_store=False) as h5_file:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't quite figure out if this is creating a file on disk or not. From the h5py docs for backing_store, it says "If False, any changes are discarded when the file is closed."
Does this mean that if the file did not exist, and you did not make changes to it, then no file will be created, or will an empty file be created?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know. This is used everywhere else in the tests. I just copied it from there.

Comment thread tests/file_test.py Outdated
@jl-wynen
jl-wynen merged commit b335c82 into main Sep 11, 2024
@jl-wynen
jl-wynen deleted the allow-passing-h5py-file branch September 11, 2024 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support passing an already open h5py file

2 participants