From 7776fbbbe2cf3796cb73d6a3c90255884941edd7 Mon Sep 17 00:00:00 2001 From: Matthias Probst Date: Tue, 7 Nov 2023 08:17:11 +0100 Subject: [PATCH] module query funcs can guess the filename --- CHANGELOG.md | 20 ++++++++++++++++---- h5rdmtoolbox/__init__.py | 22 +++++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1552ddc..13997d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,26 +2,38 @@ Log of changes in the versions +## v0.12.2 + +- bugfix requirements +- add `$exist` `find()`-methods inside of HDF files +- bugfix 0D time data as dimension +- module query functions (`find`, ...) can guess the filename from objects that have with `hasattr(hdf_filename, 'hdf_filename')` + ## v0.12.1 + - bugfix in zenodo search (did Zenodo change their API?) ## v0.12.0 + - 0D data is written to MongoDB -- new utils like computing filesize +- new utils like computing filesize - update to new zenodo_search package due to change in backend at Zenodo.org - `find`, `find_one` and `distinct` can be called on HDF files - small bugfixes ## v0.11.1 + - bugfix standard attribute validation - bugfix in `EngMeta.ipynb` ## v0.11.0 + - working with time data is now possible: - - time data can be created using the high-level method `create_time_dataset` - - slicing the above "time datasets" will return xarray data - - See `docs/wrapper/Misc.ipynb` + - time data can be created using the high-level method `create_time_dataset` + - slicing the above "time datasets" will return xarray data + - See `docs/wrapper/Misc.ipynb` - fixed issue with user-defined & nested standard attributes ## v0.10.0 + - Initial version, published on `pypi` \ No newline at end of file diff --git a/h5rdmtoolbox/__init__.py b/h5rdmtoolbox/__init__.py index 40bb45df..dbf3b092 100644 --- a/h5rdmtoolbox/__init__.py +++ b/h5rdmtoolbox/__init__.py @@ -1,12 +1,11 @@ """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 @@ -68,16 +67,33 @@ def __new__(cls, path, rec=False, **kwargs): return database.File(path, **kwargs) +def guess_filename(func): + """wrapper to guess the filename from the first argument""" + + def wrapper(hdf_filename, *args, **kwargs): + if not isinstance(hdf_filename, (str, pathlib.Path)): + if hasattr(hdf_filename, 'hdf_filename'): + hdf_filename = hdf_filename.hdf_filename + else: + raise TypeError(f'Expected str or pathlib.Path, got {type(hdf_filename)}') + return func(hdf_filename, *args, **kwargs) + + return wrapper + + +@guess_filename def find(hdf_filename, *args, **kwargs): """Opens file with `FileDB` and calls `find()` on the root group""" return FileDB(hdf_filename).find(*args, **kwargs) +@guess_filename def find_one(hdf_filename, *args, **kwargs): """Opens file with `FileDB` and calls `find_one()` on the root group""" return FileDB(hdf_filename).find_one(*args, **kwargs) +@guess_filename def distinct(hdf_filename, key, objfilter=None): """Opens file and calls `distinct()` on the root group""" with File(hdf_filename) as h5: