MCPcopy
hub / github.com/msiemens/tinydb / _generate_test

Method _generate_test

tinydb/queries.py:207–241  ·  view source on GitHub ↗

Generate a query based on a test function that first resolves the query path. :param test: The test the query executes. :param hashval: The hash of the query. :return: A :class:`~tinydb.queries.QueryInstance` object

(
            self,
            test: Callable[[Any], bool],
            hashval: Tuple,
            allow_empty_path: bool = False
    )

Source from the content-addressed store, hash-verified

205 return self.__getattr__(item)
206
207 def _generate_test(
208 self,
209 test: Callable[[Any], bool],
210 hashval: Tuple,
211 allow_empty_path: bool = False
212 ) -> QueryInstance:
213 """
214 Generate a query based on a test function that first resolves the query
215 path.
216
217 :param test: The test the query executes.
218 :param hashval: The hash of the query.
219 :return: A :class:`~tinydb.queries.QueryInstance` object
220 """
221 if not self._path and not allow_empty_path:
222 raise ValueError('Query has no path')
223
224 def runner(value):
225 try:
226 # Resolve the path
227 for part in self._path:
228 if isinstance(part, str):
229 value = value[part]
230 else:
231 value = part(value)
232 except (KeyError, TypeError):
233 return False
234 else:
235 # Perform the specified test
236 return test(value)
237
238 return QueryInstance(
239 lambda value: runner(value),
240 (hashval if self.is_cacheable() else None)
241 )
242
243 def __eq__(self, rhs: Any):
244 """

Callers 15

__eq__Method · 0.95
__ne__Method · 0.95
__lt__Method · 0.95
__le__Method · 0.95
__gt__Method · 0.95
__ge__Method · 0.95
existsMethod · 0.95
matchesMethod · 0.95
searchMethod · 0.95
testMethod · 0.95
anyMethod · 0.95
allMethod · 0.95

Calls 2

QueryInstanceClass · 0.85
is_cacheableMethod · 0.80

Tested by 1

equal_doubleMethod · 0.64