Composable specification pattern for Python
Project description
ZSpec
Composable specification pattern for Python 3.14+.
Installation
pip install zspec
Quick start
from dataclasses import dataclass
from zspec import Specification
@dataclass
class Product:
name: str
price: int
in_stock: bool
# Define specifications as simple subclasses
class InStock(Specification[Product]):
def is_satisfied_by(self, p: Product) -> bool:
return p.in_stock
class Affordable(Specification[Product]):
def __init__(self, max_price: int) -> None:
self.max_price = max_price
def is_satisfied_by(self, p: Product) -> bool:
return p.price <= self.max_price
# Compose with &, |, ~
in_stock = InStock()
reasonable = Affordable(max_price=1000)
eligible = in_stock & reasonable
product = Product(name="Laptop", price=800, in_stock=True)
assert eligible(product) # True -- callable directly
assert eligible.is_satisfied_by(product) # same thing
Features
- Composable --- combine specs with
&(and),|(or),~(not) - Type-safe --- generic
Specification[T]preserves the candidate type - Bulk combinators ---
Specification.all_of(...)andSpecification.any_of(...) - Zero dependencies --- standard library only
- Python 3.14+ --- leverages modern generics (
class Foo[T])
API overview
| Method | Description |
|---|---|
spec & other |
Both must be satisfied (AND) |
spec | other |
At least one must be satisfied (OR) |
~spec |
Negation (NOT) |
spec(candidate) |
Shorthand for is_satisfied_by |
Specification.all_of(specs) |
Reduce with AND, returns None for empty input |
Specification.any_of(specs) |
Reduce with OR, returns None for empty input |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zspec-0.1.0.tar.gz
(2.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
zspec-0.1.0-py3-none-any.whl
(3.1 kB
view details)
File details
Details for the file zspec-0.1.0.tar.gz.
File metadata
- Download URL: zspec-0.1.0.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61eec55e8a7aa6f4fd3dddc58dbcf0d4431595365e878835bbbaee18dbcd5d0d
|
|
| MD5 |
c355ce00d546f4c5e98225d5f2f893e4
|
|
| BLAKE2b-256 |
08323ace7c24f9cb3bb69999b9060ab9e78b33d2840c1336d6bb86c93a1e86ea
|
File details
Details for the file zspec-0.1.0-py3-none-any.whl.
File metadata
- Download URL: zspec-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfcdd3dad75fadb91724362f3fa0f34ac1009cbc34f9297d1e1237c79a7d99ec
|
|
| MD5 |
10ab6e611ade554e5de7d978463e97ee
|
|
| BLAKE2b-256 |
49883ccc305bdfc722644f7b1ef1b6c2fe8c089b962bd349775a0d0a900010de
|