From 616a36fb40a5b4e153f5d5e433a4149d30126551 Mon Sep 17 00:00:00 2001 From: Matthias Probst Date: Tue, 24 Oct 2023 09:01:31 +0200 Subject: [PATCH] find and find_one available at h5tbx.__init__ --- h5rdmtoolbox/__init__.py | 13 +++++++++++-- h5rdmtoolbox/conventions/_my_valiator.py | 24 ------------------------ tests/database/test_filequery.py | 15 ++++++++++++++- 3 files changed, 25 insertions(+), 27 deletions(-) delete mode 100644 h5rdmtoolbox/conventions/_my_valiator.py diff --git a/h5rdmtoolbox/__init__.py b/h5rdmtoolbox/__init__.py index 889d0642..667fe69d 100644 --- a/h5rdmtoolbox/__init__.py +++ b/h5rdmtoolbox/__init__.py @@ -1,11 +1,12 @@ """h5rdtoolbox repository""" import atexit import pathlib +import shutil +from typing import Union, Callable + # noinspection PyUnresolvedReferences import pint_xarray -import shutil import xarray as xr -from typing import Union, Callable from h5rdmtoolbox._cfg import set_config, get_config, get_ureg @@ -67,6 +68,14 @@ def __new__(cls, path, rec=False, **kwargs): return database.File(path, **kwargs) +def find(hdf_filename, *args, **kwargs): + return FileDB(hdf_filename).find(*args, **kwargs) + + +def find_one(hdf_filename, *args, **kwargs): + return FileDB(hdf_filename).find_one(*args, **kwargs) + + def dump(src: Union[str, File, pathlib.Path]) -> None: """Call h5.dump() on the provided HDF5 file diff --git a/h5rdmtoolbox/conventions/_my_valiator.py b/h5rdmtoolbox/conventions/_my_valiator.py deleted file mode 100644 index 78500cb0..00000000 --- a/h5rdmtoolbox/conventions/_my_valiator.py +++ /dev/null @@ -1,24 +0,0 @@ -# import re -# from pydantic import ( -# BaseModel, -# ) -# from pydantic.functional_validators import WrapValidator -# from typing_extensions import Annotated -# -# -# def _regex_validator(value, parent=None, attrs=None): -# pattern = re.compile(r"^\d{4}-\d{4}-\d{4}-\d{3}(\d|X)$") -# if not pattern.match(value): -# raise ValueError('Invalid format for pattern') -# return value -# -# -# regex = Annotated[int, WrapValidator(_regex_validator)] -# -# -# class ContactValidator(BaseModel): -# name: str -# orcid: regex -# -# -# ContactValidator(name='awd', orcid='0000-0002-1825-0097') diff --git a/tests/database/test_filequery.py b/tests/database/test_filequery.py index 18816615..2ad4ec15 100644 --- a/tests/database/test_filequery.py +++ b/tests/database/test_filequery.py @@ -1,6 +1,7 @@ -import numpy as np import unittest +import numpy as np + import h5rdmtoolbox as h5tbx from h5rdmtoolbox.wrapper.core import File @@ -260,6 +261,18 @@ def test_compare_to_dataset_values_2(self): res = h5.find({'$eq': [1.2, 3.4, 4.0]}, rec=False) self.assertEqual(0, len(res)) + def test_find_init_function(self): + with h5tbx.File() as h5: + h5.create_dataset('u', data=[1.2, 3.4, 4.5], attrs=dict(units='m/s', standard_name='x_velocity')) + h5.create_dataset('v', data=[4.0, 13.5, -3.4], attrs=dict(units='m/s', standard_name='y_velocity')) + res = h5tbx.find_one(h5.hdf_filename, {'$eq': [1.2, 3.4, 4.5]}, rec=False) + self.assertEqual(res.basename, 'u') + res = sorted(h5tbx.find(h5.hdf_filename, {})) + self.assertEqual(len(res), 3) + self.assertEqual(res[0].basename, '') + self.assertEqual(res[1].basename, 'u') + self.assertEqual(res[2].basename, 'v') + def test_compare_to_dataset_values_mean(self): with h5tbx.use('h5tbx'): with h5tbx.File() as h5: