API Reference
filter

def filter(filter: Callable[[Any], bool], query: QueryObject) -> QueryObject

Apply fine-grained, custom filtering logic to a query.

Useful in situations where complex filtering conditions cannot be expressed with tags and dbzero.find(). Works similarly to Python's built-in filter(), but seamlessly integrates into the dbzero query pipeline. Because dbzero.filter() returns a query object, it can be efficiently used in other functions which take QueryObject as an argument.

Parameters

  • filter Callable[[Any], bool]
    A function or lambda that takes a single object as argument. Must return True to include the object, False to exclude it.

  • query QueryObject
    A query to filter. This is typically the result of another dbzero operation, such as dbzero.find(), index.select(), or index.sort().

Returns

A query that only yields items for which filter function returned True.


Examples

Basic filtering

Let's say you've found all objects tagged with "tag1" but you only want the ones where the value attribute is exactly 1.

# Find all objects with "tag1" and then filter them
query = db0.filter(lambda x: x.value == 1, db0.find("tag1"))
 
# The query will only yield objects matching the lambda condition
results = list(query)
assert len(results) == 1
assert results[0].value == 1

Chain with index sorting

dbzero.filter() can be combined with other dbzero operations. Here, we find a set of objects, filter them with a custom condition, and then sort the result using an index.

# Assume ix_value is a db0.index() populated with objects
 
# 1. Find all objects with the "tag1"
initial_results = db0.find(MemoTestClass, "tag1")
 
# 2. Filter out objects where the value is divisible by 3
filtered_results = db0.filter(lambda x: x.value % 3 != 0, initial_results)
 
# 3. Sort the filtered results efficiently using the index
query = ix_value.sort(filtered_results)
 
# The final result is a sorted list of filtered values
sorted_values = [x.value for x in query]
# e.g., returns [1, 2, 4, 5, 7, 8]

Advanced text matching

In a real-world scenario, you can use dbzero.find() to get a broad set of candidates and then use dbzero.filter() for a more precise, order-dependent match that dbzero.find() can't handle alone.

def find_cities(search_phrase: str):
  # Get a broad list of cities using tags from the search phrase
  search_tags = list(taggify(search_phrase))
  results = cities_index.sort(db0.find(search_tags))
 
  # Use filter for a more precise, in-order token matching on the city name
  return db0.filter(
    lambda city: match_tokens_in_order(city.name, search_phrase), results
  )