Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lyse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def save_result_array(self, name, data, group=None,
if not group:
# Save dataset to results group by default
group = 'results/' + self.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.

Could you remove this unnecessary change so that it doesn't clutter up the diff?

elif not group in h5_file:
# Create the group if it doesn't exist
h5_file.create_group(group)
Expand Down Expand Up @@ -422,6 +423,11 @@ def __init__(self, h5_path, run_paths, no_write=False):
run_paths = run_paths['filepath']
self.h5_path = h5_path
self.no_write = no_write
try:
h5py.File(h5_path, 'r')

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 think that this may leave an open reference to the h5 file. It would be better to use a context manager like this:

with h5py.File(h5_path, 'r') as f:
    pass

Which ensures that the file is closed appropriately.

except:

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.

It is best to catch a specific exception class here. Could you catch the specific exception raised? (Presumably it is FileNotFoundError?)

h5py.File(h5_path,'w')

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.

Similar to previous comment, use of a context manager here would be preferable. Also, you might want to consider using the 'a' filemode (so that the file doesn't get truncated should it happen to be created by an external process in between the two h5py calls executing - which could conceivably be a significant time should there be contention on the zlock/h5lock lock).

I think it might also be appropriate to wrap the creation call in a check of the no_write argument and raise the FileNotFoundError should the file not exist and the no_write argument be true?


if not self.no_write:
self._create_group_if_not_exists(h5_path, '/', 'results')

Expand Down