Skip to content

Commit

Permalink
don't use monkey patch actually
Browse files Browse the repository at this point in the history
  • Loading branch information
magland committed Apr 25, 2024
1 parent f914ced commit 7db0a40
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
24 changes: 6 additions & 18 deletions lindi/h5py_patch/__init__.py → lindi/File/File.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
from typing import Literal
import os
import h5py
from h5py import File as OriginalH5pyFile
from ..LindiH5pyFile.LindiH5pyFile import LindiH5pyFile
from ..LindiStagingStore.StagingArea import StagingArea
from ..LocalCache.LocalCache import LocalCache


# We need to use this metaclass so that isinstance(f, h5py.File) works when f is
# a LindiH5pyFile or OriginalH5pyFile
class InstanceCheckMeta(type):
def __instancecheck__(cls, instance):
return isinstance(instance, OriginalH5pyFile) or isinstance(instance, LindiH5pyFile)


class File(metaclass=InstanceCheckMeta):
class File(h5py.File):
"""
A drop-in replacement for h5py.File that is either a lindi.LindiH5pyFile or
h5py.File depending on whether the file name ends with .lindi.json or not.
"""
def __new__(cls, name, mode: Literal['r', 'r+', 'w', 'w-', 'x', 'a'] = 'r', **kwds):
if isinstance(name, str) and name.endswith('.lindi.json'):
# should we raise exceptions on select unsupported kwds? or just go with the flow?
Expand All @@ -35,12 +31,4 @@ def __new__(cls, name, mode: Literal['r', 'r+', 'w', 'w-', 'x', 'a'] = 'r', **kw
local_cache=local_cache
)
else:
return OriginalH5pyFile(name, mode=mode, **kwds)


def apply_h5py_patch():
h5py.File = File


# by virtue of importing we apply the patch
apply_h5py_patch()
return h5py.File(name, mode=mode, **kwds)
Empty file added lindi/File/__init__.py
Empty file.
1 change: 1 addition & 0 deletions lindi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .LindiH5pyFile import LindiH5pyFile, LindiH5pyGroup, LindiH5pyDataset, LindiH5pyHardLink, LindiH5pySoftLink # noqa: F401
from .LindiStagingStore import LindiStagingStore, StagingArea # noqa: F401
from .LocalCache.LocalCache import LocalCache # noqa: F401
from .File.File import File # noqa: F401
12 changes: 5 additions & 7 deletions tests/test_patch.py → tests/test_lindi_file.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import lindi.h5py_patch # noqa: F401
import tempfile
import numpy as np
import h5py
from h5py import File
import lindi


def test_patch():
def test_lindi_file():
with tempfile.TemporaryDirectory() as tmpdir:
fname = f'{tmpdir}/test.lindi.json'
with File(fname, 'w') as f:
assert isinstance(f, File)
with lindi.File(fname, 'w') as f:
f.create_dataset('data', data=np.arange(500000, dtype=np.uint32), chunks=(100000,))

with File(fname, 'r') as f:
with lindi.File(fname, 'r') as f:
ds = f['data']
assert isinstance(ds, h5py.Dataset)
assert ds.shape == (500000,)
Expand All @@ -22,4 +20,4 @@ def test_patch():


if __name__ == '__main__':
test_patch()
test_lindi_file()

0 comments on commit 7db0a40

Please sign in to comment.