Metadata-Version: 2.4
Name: vcti-predicate
Version: 3.0.0
Summary: VCollab Predicate - condition evaluation engine with extensible operators and modifiers
Author: Visual Collaboration Technologies Inc.
License-Expression: LicenseRef-Proprietary
Project-URL: Repository, https://github.com/vcollab/vcti-python-predicate
Project-URL: Changelog, https://github.com/vcollab/vcti-python-predicate/blob/main/CHANGELOG.md
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Python: <3.15,>=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: numpy; extra == "test"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: typecheck
Requires-Dist: mypy; extra == "typecheck"
Dynamic: license-file

# vcti-predicate

VCollab Predicate - condition evaluation engine with extensible operators and modifiers

## Overview

Filtering, validation, rule engines and routing logic all need to answer
one question: does this value satisfy this condition? `vcti-predicate`
answers it with two entry points, `check()` and `evaluate()`. A condition
is an *op string* (`"=="`, `"^="`, `"between"`, `"is_file"`) that names a
test, applied to one or more arguments, with *modifiers* such as
`case_sensitive` or `tolerance`. The type of the first argument selects the
implementation, so one rule works across strings, numbers, paths and
`None`. Every op string can be negated with a leading `!`, and the built-in
symbols can be extended by building your own registry.

---

## Installation

```bash
pip install vcti-predicate
```

### In `requirements.txt`

```
vcti-predicate>=3.0.0
```

### In `pyproject.toml` dependencies

```toml
dependencies = [
    "vcti-predicate>=3.0.0",
]
```

---

## Quick Start

```python
from vcti.predicate import check, evaluate

# ── Binary: check(lhs, op_string, rhs) ──

check("Hello", "==", "hello")                       # True: case-insensitive by default
check("Hello", "==", "hello", case_sensitive=True)  # False
check(1.0, "==", 1.001, tolerance=0.01)             # True
check(5, "between", (1, 10))                        # True: bounds in either order

check("filename.pdf", "$=", ".PDF")                 # True: ends with
check("Hello", "^=cs", "he")                        # False: the cs variant is case-sensitive
check("test123", "~=", r"\d+$")                     # True: regex search
check(["Wing", "Tail"], "?=", "wing")               # True: contains

# ── Negation: "!" before any op string ──

check("hello", "!^=", "xyz")                        # True
check(0, "!between", (1, 10))                       # True

# ── Unary: check(value, op_string) ──

check("", "is_empty")                               # True
check(None, "exists")                               # False
check("12", "is_numeric", numeric_strings=True)     # True
check(".", "is_dir")                                # True

# ── Prefix form, for rules held as data ──

rule = ("^=", "wing")
evaluate(rule[0], "Wing_01", rule[1])               # True
```

Strings and numbers are not mixed unless asked: `check(5, "==", "5")` is
`False`, and `check(5, "==", "5", numeric_strings=True)` is `True`.

---

## Entry Points

| Function | Argument order | Use case |
|---|---|---|
| `check(value, op_string, /, **modifiers)` | value first | A unary test, written as code reads |
| `check(lhs, op_string, rhs, /, **modifiers)` | value first | A binary test |
| `evaluate(op_string, /, *args, **modifiers)` | op string first | Rules held as data, any number of arguments |

Both run the same pipeline: parse the op string, select the operator by the
type of the first argument, prepare the arguments and modifiers, and run
the test. `builtin_registry` holds the built-in symbols, and `check` and
`evaluate` are its methods. A custom `Registry` has the same methods.

---

## Symbol Summary

| Family | Op strings | Modifiers |
|---|---|---|
| Comparison | `==` `eq`, `<` `lt`, `<=` `le`, `>` `gt`, `>=` `ge`, `between`; `!=` and `ne` negate `==` | `case_sensitive`, `tolerance`, `inclusive`, `allow_nan`, `numeric_strings` |
| String matching | `^=` `startswith`, `$=` `endswith`, `~=` `matches`, each with `cs` and `ic` variants (`^=cs`) | `case_sensitive`, `multiline`, `dotall` |
| Collection items | `?=` `contains` `has`, `?=cs`, `?=ic` | `case_sensitive`, `tolerance`, `allow_nan`, `numeric_strings` |
| Value checks | `is_empty`, `exists`, `is_numeric` | `numeric_strings` |
| Path checks | `is_file`, `is_dir`, `path_exists` | none |

Every op string has a negated form with a leading `!`: `!^=cs`, `!?=`,
`!is_file`. The enum `Symbol` names each symbol (`Symbol.STARTS_WITH`) for
code that should not type strings. The full reference, with what each
symbol does for each type, is [docs/symbols.md](docs/symbols.md).

---

## Modifiers

| Modifier | Default | Effect |
|---|---|---|
| `case_sensitive` | `False` | Compare strings without casefolding |
| `tolerance` | `0` | Numbers within this absolute difference are equal, also for ordering |
| `inclusive` | `True` | `between` includes its bounds |
| `allow_nan` | `False` | Accept `nan` and apply IEEE rules; by default a `nan` raises |
| `numeric_strings` | `False` | Let a string stand in for a number, and a number for a string |
| `multiline`, `dotall` | `False` | `re.MULTILINE` / `re.DOTALL` for `~=` |

A modifier outside the symbol's family raises `ModifierError`, which
catches typos such as `tolernce`. A modifier of the family that the
selected operator does not use is ignored, so one rule works on values of
different types: `tolerance` matters for numbers and not for strings.
These modifiers are the fields of `BuiltinModifiers`, the built-in
registry's modifiers class; a custom registry can subclass it to add its
own (see the [extension guide](docs/extending.md)).

---

## Errors

Every error derives from `PredicateError` and reports the local failure,
with the op string as written (`error.op_string`):

| Error | Raised when |
|---|---|
| `UnknownSymbolError` | The op string is not known |
| `NoOperatorError` | The symbol has no operator for the value's type (`None` with `"^="`) |
| `ArityError` | The number of arguments is wrong |
| `ModifierError` | A modifier is unknown, invalid, or disagrees with the op string |
| `OperandError` | An argument is not accepted (`5 < "6"`, a `nan`, bounds that are not a pair) |
| `NormalizationError` | An accepted argument cannot be prepared (an invalid regex) |
| `PreparationError` | The base of the three above, and an unexpected failure while preparing |
| `EvaluationError` | The test itself failed (permission denied on `is_file`) |

What an error means is for the caller to decide: a filter can treat
`NoOperatorError` as "no match" and report every other error as a broken
rule.

---

## Dependencies

None. `vcti-predicate` uses only the Python standard library.

---

## Documentation

| If you want to… | Read |
|---|---|
| Get started using the package | Quick Start above |
| Look up every symbol and what it does for each type | [docs/symbols.md](docs/symbols.md) |
| See practical, real-world usage | [docs/patterns.md](docs/patterns.md) |
| Understand the architecture and design decisions | [docs/design.md](docs/design.md) |
| Move code from 2.x to 3.0 | [docs/migration-3.0.md](docs/migration-3.0.md) |
| Navigate and understand the source | [docs/source-guide.md](docs/source-guide.md) |
| Add symbols, groups and operators | [docs/extending.md](docs/extending.md) |
