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
)
| 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 | """ |