How to use the tinydb.queries.QueryImpl function in tinydb

To help you get started, we’ve selected a few tinydb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github msiemens / tinydb / tinydb / queries.py View on Github external
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)
github msiemens / tinydb / tinydb / queries.py View on Github external
# (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)
github IDSIA / sacred / sacred / observers / tinydb_hashfs.py View on Github external
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 "
github msiemens / tinydb / tinydb / queries.py View on Github external
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])))
github msiemens / tinydb / tinydb / queries.py View on Github external
def __invert__(self) -> 'QueryImpl':
        return QueryImpl(lambda value: not self(value),
                         ('not', self.hashval))
github msiemens / tinydb / tinydb / queries.py View on Github external
def __eq__(self, other: object):
        if isinstance(other, QueryImpl):
            return self.hashval == other.hashval

        return False
github msiemens / tinydb / tinydb / queries.py View on Github external
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])))