Skip to content

Commit

Permalink
add query opt $exists
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasprobst committed Oct 30, 2023
1 parent 750d7af commit 4e90db8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 8 additions & 1 deletion h5rdmtoolbox/database/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ def _regex(value, pattern) -> bool:
return False
return True

def _exists(value, tf: bool) -> bool:
if tf:
return value is not None
else:
return value is None


operator = {'$regex': _regex, '$eq': _eq, '$gt': _gt, '$gte': _gte, '$lt': _lt, '$lte': _lte}
operator = {'$regex': _regex, '$eq': _eq, '$gt': _gt, '$gte': _gte, '$lt': _lt, '$lte': _lte,
'$exists': _exists}
value_operator = {'$eq': _arreq, '$gt': _gt, '$gte': _gte, '$lt': _lt, '$lte': _lte}


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = h5rdmtoolbox
version = 0.12.1
version = 0.12.2
author = Matthias Probst
author_email = matthias.probst@kit.edu
description = Supporting a FAIR Research Data lifecycle using Python and HDF5.
Expand Down
14 changes: 12 additions & 2 deletions tests/database/test_filequery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import unittest

import numpy as np
import unittest

import h5rdmtoolbox as h5tbx
from h5rdmtoolbox.wrapper.core import File
Expand Down Expand Up @@ -109,6 +108,17 @@ def test_Folder(self):
res = fd.find_one_per_file({'$basename': {'$regex': 'ds[0-9]'}})
self.assertEqual(2, len(res))

def test_exists(self):
with h5tbx.File() as h5:
h5.create_dataset('ds1', shape=(1, 2, 3), attrs=dict(units='', long_name='long name 1'))
h5.create_dataset('ds2', shape=(1, 2, 3), attrs=dict(units='', long_name='long name 1'))

self.assertTrue('long_name' in h5.find_one({'long_name': {'$exists': True}}).attrs)
self.assertIsInstance(h5.find_one({'long_name': {'$exists': True}}), h5tbx.wrapper.core.Dataset)

mres = h5.find({'long_name': {'$exists': True}})
self.assertEqual(2, len(mres))

def test_chained_find(self):
with h5tbx.File() as h5:
g = h5.create_group('grp1')
Expand Down

0 comments on commit 4e90db8

Please sign in to comment.