Skip to main content

Attribute Enricher

Rule-based attribute enrichment for any sequence of items.

Overview

vcti-attribute-enricher adds attributes to items in a sequence by matching vcti-lookup rules. Each EnrichRule pairs a condition (when) with attributes to write (set); the enricher walks an iterable, evaluates each rule against each item, and stamps matching items with the rule's attributes.

The package is iterable-generic, not tree-specific: the items can be tree-node payloads, dicts, dataclasses, or any object with an attribute-bearing surface. Reading and writing are pluggable via getter / setter callables, with defaults for items that keep a merged attributes read view beside a mutable enricher_attributes dict.

The package is the write-side counterpart to vcti-lookup: where Lookup filters a sequence by rules, the enricher mutates the sequence by rules. A rule means the same thing in both.

Installation

pip install vcti-attribute-enricher

In requirements.txt

vcti-attribute-enricher>=3.0.0

In pyproject.toml dependencies

dependencies = [
    "vcti-attribute-enricher>=3.0.0",
]

Quick Start

from vcti.attribute_enricher import EnrichRule, apply_rules
from vcti.lookup import MISSING, Rule

items = [
    {"name": "stress.h5", "dtype": "float64"},
    {"name": "ids.h5", "dtype": "int64"},
    {"name": "config.json", "dtype": "object"},
]

result = apply_rules(
    items,
    rules=[
        EnrichRule(set={"loaded_at": "2026-06-06"}),                 # every item
        EnrichRule(set={"category": "mechanical"},
                   when=(Rule("name", "^=", "stress"),)),             # name starts with "stress"
        EnrichRule(set={"is_float": True},
                   when=(Rule("dtype", "^=", "float"),
                         Rule("name", "$=", ".h5"))),                 # AND across conditions
    ],
    # Return MISSING (not None) for an absent key, so the rule cleanly fails.
    getter=lambda item, key: item.get(key, MISSING),
    setter=lambda item, key, value: item.setdefault("tags", {}).__setitem__(key, value),
)

assert items[0]["tags"] == {
    "loaded_at": "2026-06-06", "category": "mechanical", "is_float": True,
}
assert result.per_rule_matches == (3, 1, 1)

apply_rules returns an EnrichResult of summary counts; the items are mutated in place:

print(result.items_visited, result.items_matched, result.writes_applied)
print(result.per_rule_matches)   # match count per rule, in order; 0 = dead rule

Getters must return vcti.lookup.MISSING for absent attributes, not None. None is a value like any other and is passed to the operator; MISSING makes the rule fail (or, for a presence test such as !exists, match).

Default getter and setter

With no getter or setter, the enricher reads item.attributes (any mapping, such as a ChainMap merging the item's own values with enrichment) and writes item.enricher_attributes. Items of that shape, tree-node payloads for example, need no callables at all:

from vcti.tree import descendants

apply_rules(
    descendants(tree, subtree_root, include_self=True),
    rules=[
        EnrichRule(set={"file_path": str(path)}),
        EnrichRule(set={"category": "mechanical"},
                   when=(Rule("name", "^=", "stress"),)),
    ],
)

How it works

  1. Prepare every condition once: each Rule builds its matcher (vcti.lookup.Rule.matcher), exactly as Lookup.filter does. A broken rule raises here, before any item is touched.
  2. Iterate the supplied items. For each item, evaluate every EnrichRule:
    • If when is empty, the rule matches.
    • Otherwise every Rule in when must match (AND), each testing the attribute value read through getter.
  3. On a match, write each key/value in set to the item via setter.

Layering — last write wins. Multiple EnrichRules that match the same item all apply, in the order given. Later rules overwrite earlier ones on collision, so "set a default for everything, then refine for specific subsets" needs no special support.


API

Symbol Description
EnrichRule(set, when=()) Frozen dataclass. set is a dict of attributes to write; when is a tuple[Rule, ...] combined with AND logic. Empty when matches every item. Not hashable (the set dict).
apply_rules(items, rules, *, getter=None, setter=None) Walk items, evaluate rules, stamp matches. Returns an EnrichResult. Defaults to vcti.lookup.attributes_getter and a setter that writes to item.enricher_attributes. Mutates in place (not thread-safe); rule errors are vcti.predicate.PredicateError subclasses with notes naming the rule, or the item and value (a value error is fail-fast, non-transactional).
EnrichResult(items_visited, items_matched, writes_applied, per_rule_matches) Frozen dataclass of summary counts returned by apply_rules. per_rule_matches is per-rule, in order (a 0 flags a dead rule).
Getter / Setter Type aliases for the (item, key) -> value and (item, key, value) -> None callables.

Values in set are written by reference — a mutable value shared across matched items is the same object on each. See docs/patterns.md for pitfalls.


Dependencies

  • vcti-lookup (>=5.1.1,<6) — Rule and its matcher, MISSING, the default attributes_getter
  • vcti-predicate (>=4.0.0,<5) — the PredicateError hierarchy rule errors belong to

Documentation

If you want to… Read
Get started using the package Quick Start above
Understand the design decisions and runtime semantics docs/design.md
See recipes for different item shapes, and the pitfalls docs/patterns.md
Navigate and understand the source docs/source-guide.md
Write a custom getter or setter docs/extending.md

Metadata

Release files for vcti-attribute-enricher 3.0.0

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-attribute-enricher 3.0.0
File Size Uploaded Provenance Actions
vcti_attribute_enricher-3.0.0.tar.gz 25.5 kB With provenance Details for vcti_attribute_enricher-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vcti-attribute-enricher 3.0.0
File Interpreter ABI Platform Actions
vcti_attribute_enricher-3.0.0-py3-none-any.whl Python 3 none any Details for vcti_attribute_enricher-3.0.0-py3-none-any.whl

Total release size: 36.3 kB

Release files / vcti_attribute_enricher-3.0.0.tar.gz

Download URL vcti_attribute_enricher-3.0.0.tar.gz
Size 25.5 kB
Tags Source
SHA-256 checksum
How to use checksums
26d5e8f59dba997fec504439c93709bd1cb61791fc718a829f9a208442bb3d6c
BLAKE2b-256 checksum
How to use checksums
ae8bb26f606ec9abd803c1c36559a6524a7f637898849b39067f9c1fa45d83c8
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_attribute_enricher-3.0.0-py3-none-any.whl

Download URL vcti_attribute_enricher-3.0.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea3dd8af86676a7f3732871dc1ce04a225a2a2b21aa73ed30b719d97478da1c7
BLAKE2b-256 checksum
How to use checksums
bf33d5f387d8630fe9148291e9034b7ee6a46f8eaaf3c8419ef2e5707c7bfc30
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-attribute-enricher 3.0.0

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_attribute_enricher-3.0.0.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.

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