Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _generate_test(
self,
test: Callable[[Any], bool],
hashval: Tuple,
) -> QueryImpl:
"""
Generate a query based on a test function.
:param test: The test the query executes.
:param hashval: The hash of the query.
:return: A :class:`~tinydb.queries.QueryImpl` object
"""
if not self._path:
raise ValueError('Query has no path')
return QueryImpl(self._prepare_test(test), hashval)
# (a & b == b & a)
return QueryImpl(lambda value: self(value) and other(value),
('and', frozenset([self.hashval, other.hashval])))
def __or__(self, other: 'QueryImpl') -> 'QueryImpl':
# We use a frozenset for the hash as the OR operation is commutative
# (a | b == b | a)
return QueryImpl(lambda value: self(value) or other(value),
('or', frozenset([self.hashval, other.hashval])))
def __invert__(self) -> 'QueryImpl':
return QueryImpl(lambda value: not self(value),
('not', self.hashval))
class Query(QueryImpl):
"""
TinyDB Queries.
Allows to build queries for TinyDB databases. There are two main ways of
using queries:
1) ORM-like usage:
>>> User = Query()
>>> db.search(User.name == 'John Doe')
>>> db.search(User['logged-in'] == True)
2) Classical usage:
>>> db.search(where('value') == True)
def fetch_metadata(self, exp_name=None, query=None, indices=None):
"""Return all metadata for matching experiment name, index or query."""
from tinydb import Query
from tinydb.queries import QueryImpl
if exp_name or query:
if query:
assert type(query), QueryImpl
q = query
elif exp_name:
q = Query().experiment.name.search(exp_name)
entries = self.runs.search(q)
elif indices or indices == 0:
if not isinstance(indices, (tuple, list)):
indices = [indices]
num_recs = len(self.runs)
for idx in indices:
if idx >= num_recs:
raise ValueError(
"Index value ({}) must be less than "
def __or__(self, other: 'QueryImpl') -> 'QueryImpl':
# We use a frozenset for the hash as the OR operation is commutative
# (a | b == b | a)
return QueryImpl(lambda value: self(value) or other(value),
('or', frozenset([self.hashval, other.hashval])))
def __invert__(self) -> 'QueryImpl':
return QueryImpl(lambda value: not self(value),
('not', self.hashval))
def __eq__(self, other: object):
if isinstance(other, QueryImpl):
return self.hashval == other.hashval
return False
def __and__(self, other: 'QueryImpl') -> 'QueryImpl':
# We use a frozenset for the hash as the AND operation is commutative
# (a & b == b & a)
return QueryImpl(lambda value: self(value) and other(value),
('and', frozenset([self.hashval, other.hashval])))