Build a Partial Index Mock in Python for Database Filtering
Simulate a partial database index by filtering keys with a predicate, then return a limited mock lookup dictionary.
Python code
20 linesdata = [
"alpha", "beta", "gamma", "delta", "epsilon",
"zeta", "eta", "theta", "iota", "kappa"
]
filtered_keys = [item for item in data if len(item) >= 5]
def mock_partial_index(keys, filter_func, limit=3):
result = {}
for key in keys:
if not filter_func(key):
continue
result[key] = f"mock://{key}"
if len(result) >= limit:
break
return result
if __name__ == "__main__":
index = mock_partial_index(filtered_keys, lambda k: k.startswith(("a", "b", "d", "e")))
print(index)
Output
{'alpha': 'mock://alpha', 'beta': 'mock://beta', 'delta': 'mock://delta'}
How it works
This function mimics a partial index by applying a filter_func to each key before adding it to the result. It limits entries to limit (default 3) to simulate practical index size constraints. The lambda checks if keys start with common prefixes, and only filtered keys are stored. The mock URL format gives a placeholder for actual indexed lookups.
Common mistakes
- Forgetting that the limit applies after filtering, not before
- Assuming the input order is alphabetical when it's the original list order
- Using `filter()` built-in which returns an iterator, not a dict
- Not handling empty filtered results gracefully
Variations
- Use `itertools.islice` to lazy-limit the filtered sequence
- Return a `dict.fromkeys` with the filter applied for one-liner mock
Real-world use cases
- Simulating a partial index in a test suite before implementing the real database query
- Prototyping index-aware query behavior in a mock service for unit tests
- Comparing filtered vs full index lookup performance in a profiling script
Sponsored
More from Database scaling & optimization
- Approximate Count with HyperLogLog in Python medium
- B-Tree Insert and In-Order Traversal in Python hard
- Broadcast a Small Reference Table in Python easy
- Build a Full Text Search Index in Python medium
- Composite index leftmost prefix in Python medium
- Consistent Hashing with Virtual Buckets in Python medium
Keep learning
Related tutorials and quizzes for this topic.