deephyper.core.analytics.Query#

class deephyper.core.analytics.Query[source]#

Bases: tinydb.queries.QueryInstance

TinyDB Queries.

Allows building 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)
  1. Classical usage:

>>> db.search(where('value') == True)

Note that where(...) is a shorthand for Query(...) allowing for a more fluent syntax.

Besides the methods documented here you can combine queries using the binary AND and OR operators:

>>> # Binary AND:
>>> db.search((where('field1').exists()) & (where('field2') == 5))
>>> # Binary OR:
>>> db.search((where('field1').exists()) | (where('field2') == 5))

Queries are executed by calling the resulting object. They expect to get the document to test as the first argument and return True or False depending on whether the documents match the query or not.

Methods

all

Check if a condition is met by all documents in a list, where a condition can also be a sequence (e.g.

any

Check if a condition is met by any document in a list, where a condition can also be a sequence (e.g.

exists

Test for a dict where a provided key exists.

fragment

is_cacheable

map

Add a function to the query path.

matches

Run a regex test against a dict value (whole string has to match).

noop

Always evaluate to True.

one_of

Check if the value is contained in a list or generator.

search

Run a regex test against a dict value (only substring string has to match).

test

Run a user-defined test function against a dict value.

__call__(value: Mapping)bool#

Evaluate the query to check if it matches a specified value.

Parameters

value – The value to check.

Returns

Whether the value matches this query.

all(cond: Union[tinydb.queries.QueryInstance, List[Any]])tinydb.queries.QueryInstance[source]#

Check if a condition is met by all documents in a list, where a condition can also be a sequence (e.g. list).

>>> Query().f1.all(Query().f2 == 1)

Matches:

{'f1': [{'f2': 1}, {'f2': 1}]}
>>> Query().f1.all([1, 2, 3])

Matches:

{'f1': [1, 2, 3, 4, 5]}
Parameters

cond – Either a query that all documents have to match or a list which has to be contained in the tested document.

any(cond: Union[tinydb.queries.QueryInstance, List[Any]])tinydb.queries.QueryInstance[source]#

Check if a condition is met by any document in a list, where a condition can also be a sequence (e.g. list).

>>> Query().f1.any(Query().f2 == 1)

Matches:

{'f1': [{'f2': 1}, {'f2': 0}]}
>>> Query().f1.any([1, 2, 3])

Matches:

{'f1': [1, 2]}
{'f1': [3, 4, 5]}
Parameters

cond – Either a query that at least one document has to match or a list of which at least one document has to be contained in the tested document.

exists()tinydb.queries.QueryInstance[source]#

Test for a dict where a provided key exists.

>>> Query().f1.exists()
map(fn: Callable[[Any], Any])tinydb.queries.Query[source]#

Add a function to the query path. Similar to __getattr__ but for arbitrary functions.

matches(regex: str, flags: int = 0)tinydb.queries.QueryInstance[source]#

Run a regex test against a dict value (whole string has to match).

>>> Query().f1.matches(r'^\w+$')
Parameters
  • regex – The regular expression to use for matching

  • flags – regex flags to pass to re.match

noop()tinydb.queries.QueryInstance[source]#

Always evaluate to True.

Useful for having a base value when composing queries dynamically.

one_of(items: List[Any])tinydb.queries.QueryInstance[source]#

Check if the value is contained in a list or generator.

>>> Query().f1.one_of(['value 1', 'value 2'])
Parameters

items – The list of items to check with

search(regex: str, flags: int = 0)tinydb.queries.QueryInstance[source]#

Run a regex test against a dict value (only substring string has to match).

>>> Query().f1.search(r'^\w+$')
Parameters
  • regex – The regular expression to use for matching

  • flags – regex flags to pass to re.match

test(func: Callable[[Mapping], bool], *args)tinydb.queries.QueryInstance[source]#

Run a user-defined test function against a dict value.

>>> def test_func(val):
...     return val == 42
...
>>> Query().f1.test(test_func)

Warning

The test fuction provided needs to be deterministic (returning the same value when provided with the same arguments), otherwise this may mess up the query cache that Table implements.

Parameters
  • func – The function to call, passing the dict as the first argument

  • args – Additional arguments to pass to the test function