Skip to main content

Lookup

Attribute-based item lookup and filtering for Python collections.

Overview

vcti-lookup provides Lookup, an attribute-based filtering engine for any Python sequence — dicts, dataclasses, Pydantic models, numpy structured-array rows or plain objects, read through pluggable getters. At Level 1 you filter the items directly with Rule conditions and get the matching items back. At Level 2 the items are an attribute set describing external data (file paths, an array, tree nodes) with id = index, and the matching IDs index back into that data. Lookup never wraps or copies what you pass it.

Level 1 — Filter directly          Level 2 — Map to external data

┌──────────────┐                   ┌───────────────┐
│ items        │                   │ your data     │
│ (id + attrs) │                   │ (list, array) │
└──────┬───────┘                   └───────▲───────┘
       │                                   │ indices
       ▼                                   │
┌──────────────┐  Rules    ┌──────────────┐  Rules
│    Lookup    │◄──────    │    Lookup    │◄──────
└──────┬───────┘           └──────┬───────┘
       │                          │
       ▼                          ▼
  matched items            matching_ids()

Installation

From PyPI

pip install vcti-lookup

In requirements.txt

vcti-lookup>=5.1.1

In pyproject.toml dependencies

dependencies = [
    "vcti-lookup>=5.1.1",
]

Quick Start

Level 1 — Filter items directly

from vcti.lookup import Lookup, Rule

items = [
    {"id": 1, "type": "pdf", "size": 1000},
    {"id": 2, "type": "txt", "size": 200},
    {"id": 3, "type": "pdf", "size": 1500},
]

lk = Lookup(items)

# Single rule
pdfs = lk.filter([Rule("type", "==", "pdf")])
# [{"id": 1, ...}, {"id": 3, ...}]

# Multiple rules (AND)
large_pdfs = lk.filter([Rule("type", "==", "pdf"), Rule("size", ">", 1200)])
# [{"id": 3, ...}]

# OR logic
result = lk.filter_any([Rule("type", "==", "pdf"), Rule("type", "==", "jpg")])

# Exclusion (complement of filter)
non_pdfs = lk.exclude([Rule("type", "==", "pdf")])

# Lookup by ID
item = lk.get(2)

# One of several values
lk.filter([Rule("type", "in", ["pdf", "jpg"])])

# Unary operators: omit the value
lk.filter([Rule("name", "is_empty")])

# None is an ordinary value to compare against
lk.filter([Rule("name", "==", None)])

A missing attribute fails every rule except the presence tests exists, !exists (alias is_none) and is_missing, which treat it like None: Rule("name", "!exists") matches items with no name as well as items whose name is None.

The rule's value decides how item values are read: Rule("size", ">", 500) is a numeric comparison whatever the items hold. The value is checked, prepared and copied when a query starts, so changing a list after building a rule affects the next query, not one already running.

A None value the rule does not apply to — Rule("size", ">", 100), Rule("name", "^=", "A") — does not match rather than raising, so one None cannot fail a whole filter. Where vcti-predicate accepts None it decides as usual: Rule("name", "!=", "A") matches None.

Every query checks its rules before reading any item, so a broken rule (an unknown operator, a misspelt modifier, a value given to a unary operator, a regular expression that does not compile, between bounds that cannot be ordered) raises a vcti.predicate.PredicateError subclass whatever the data holds, even on an empty Lookup. The error carries a note naming the rule, so a query with several rules shows which one is broken.

nan is refused unless the rule asks for IEEE rules with {"allow_nan": True}:

items = [{"id": 1, "v": 1.0}, {"id": 2, "v": float("nan")}]
lk = Lookup(items)
lk.filter([Rule("v", "!=", 1.0, {"allow_nan": True})])  # [{"id": 2, ...}]

Matching a single value

Rule.matcher() checks the rule once and returns a function that applies the same semantics to values you have already read, for code that reads attributes its own way; Rule.matches(value) does the same for one value. Pass MISSING for an absent attribute:

from vcti.lookup import MISSING, Rule

Rule("size", ">", 1000).matches(1500)   # True
Rule("name", "!=", "x").matches(MISSING)  # False: absent attributes fail
Rule("name", "!exists").matches(MISSING)  # True

large = Rule("size", ">", 1000).matcher()  # raises here if the rule is broken
[large(v) for v in (1500, 10, None)]       # [True, False, False]

Level 2 — Map attributes to external data

from vcti.lookup import Lookup, Rule

# Your data — any shape, Lookup doesn't touch it
data = ["report.pdf", "notes.txt", "analysis.pdf"]

# Build attribute set — id = index into your data
attrs = [
    {"id": 0, "type": "pdf", "size": 1000},
    {"id": 1, "type": "txt", "size": 200},
    {"id": 2, "type": "pdf", "size": 1500},
]

lk = Lookup(attrs)
indices = lk.matching_ids([Rule("type", "==", "pdf")])
# [0, 2]

# Map back to your data
results = [data[i] for i in indices]
# ["report.pdf", "analysis.pdf"]

Numpy structured arrays (direct, no copy)

import numpy as np
from vcti.lookup import Lookup, Rule
from vcti.lookup.getter import numpy_getter

dt = np.dtype([("id", "i4"), ("type", "U10"), ("value", "f8")])
arr = np.array([(0, "sensor", 3.14), (1, "actuator", 2.71)], dtype=dt)

# Pass the array directly — no conversion
lk = Lookup(arr, getter=numpy_getter, id_key="id")
indices = lk.matching_ids([Rule("type", "==", "sensor")])
# [0]

# Index back into the array
results = arr[indices]

Dataclasses

from dataclasses import dataclass

@dataclass
class File:
    id: int
    type: str
    size: int

files = [File(1, "pdf", 1000), File(2, "txt", 200), File(3, "pdf", 1500)]
lk = Lookup(files)
result = lk.filter([Rule("type", "==", "pdf")])
# [File(1, "pdf", 1000), File(3, "pdf", 1500)]

Modifiers

Modifiers are keyword arguments forwarded to vcti-predicate. They control comparison behaviour. Pass them as the fourth argument to Rule.

items = [
    {"id": 1, "name": "Report_Q1", "value": 3.14159, "log": "start\nerror: disk"},
    {"id": 2, "name": "report_Q2", "value": 2.71828, "log": "start\nok"},
]
lk = Lookup(items)

# Default: case-insensitive
lk.matching_ids([Rule("name", "^=", "report")])                            # [1, 2]

# Case-sensitive via modifier
lk.matching_ids([Rule("name", "^=", "Report", {"case_sensitive": True})])  # [1]

# Float tolerance: 3.14159 is within 0.01 of 3.14
lk.matching_ids([Rule("value", "==", 3.14, {"tolerance": 0.01})])          # [1]

# Regex with multiline: ^ matches at the start of each line
lk.matching_ids([Rule("log", "~=", r"^error:", {"multiline": True})])      # [1]

See patterns.md for more modifier examples.


Public API

Name Type Description
Lookup[T] Class Attribute-based filtering for any sequence
Rule Frozen dataclass Filter condition (attribute, operator, value, modifiers); the operator is a vcti-predicate op string or in / !in; is_unary when built without a value; matcher() checks it once and returns a value test; matches(value) tests one value
ValueGetter Type alias Callable[[Any, str], Any]
MISSING Sentinel Returned by getters when attribute is absent
auto_getter Function Handles dicts and objects transparently
dict_getter Function Extracts values from dicts
object_getter Function Extracts values via getattr
attributes_getter Function Extracts values from .attributes dict
numpy_getter Function Extracts fields from numpy structured-array rows

Lookup methods

Method Returns Description
filter(rules) list[T] Items matching ALL rules (AND)
filter_any(rules) list[T] Items matching ANY rule (OR)
exclude(rules) list[T] Items NOT matching all rules
first(rules) T | None First matching item (short-circuits)
first_any(rules) T | None First item matching any rule (short-circuits)
first_id(rules) Any | None ID of first matching item
matching_ids(rules) list[Any] IDs of all matching items
count(rules) int Number of matching items (no list allocation)
get(item_id) T | None Item by ID (O(1))
reindex() None Rebuild the ID index after the sequence changes
items Sequence[T] The underlying sequence (same reference)
lk[i] / lk[i:j] T / Sequence[T] Index or slice access
len(lk) / iter(lk) int / Iterator[T] Size and iteration over the items
item_id in lk bool Whether an item has that ID

Lookup(items, id_key=...) reads the ID through the getter; Lookup(items, id_attr=...) reads it with getattr. Pass at most one; with neither, the ID is auto-detected from .id or a "id" key. The ID index is built once: after adding, removing or replacing items, or changing an ID, call lk.reindex() before using get() or in.


Dependencies

  • vcti-predicate 4.x (4.0.0 or later, below 5) — condition evaluation engine
  • numpy — optional, for numpy_getter

Documentation

If you want to… Read
Get started using the package Quick Start above
Understand the architecture and design decisions docs/design.md
See practical, real-world usage docs/patterns.md
Navigate and understand the source docs/source-guide.md
Write a custom value getter docs/extending.md
See what changed between versions CHANGELOG.md

Metadata

Release files for vcti-lookup 5.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vcti-lookup 5.1.1
File Size Uploaded Provenance Actions
vcti_lookup-5.1.1.tar.gz 37.9 kB With provenance Details for vcti_lookup-5.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vcti-lookup 5.1.1
File Interpreter ABI Platform Actions
vcti_lookup-5.1.1-py3-none-any.whl Python 3 none any Details for vcti_lookup-5.1.1-py3-none-any.whl

Total release size: 53.9 kB

Release files / vcti_lookup-5.1.1.tar.gz

Download URL vcti_lookup-5.1.1.tar.gz
Size 37.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4aac3aa070268bc7bc943dd497e81680cbd830231fb0d51233cec60ddb934434
BLAKE2b-256 checksum
How to use checksums
d33ec77db0983abe21749479ce024895ae3a6edec04e0221ae175b12a9ec94e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / vcti_lookup-5.1.1-py3-none-any.whl

Download URL vcti_lookup-5.1.1-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d499941c98d41f92429790b69da2ef22641b59abf3f5b84484a535678848497a
BLAKE2b-256 checksum
How to use checksums
da1cfe67946b68529398c4c8096505a4dbe5894b00e4fa9011a171010e49db23
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for vcti-lookup 5.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vcti_lookup-5.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

5.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page