Extended set operations: algebraic, functional, and utilities on top of Python set
Project description
betterset
Extended set operations: algebraic, functional, and utilities on top of Python's built-in set.
Features
- Operator overloads:
+union,*cartesian product,**n-fold cartesian,@relation composition - Algebraic utilities:
powerset,cartesian,complement,disjoint,partition,closure - Functional utilities:
map,filter,reduce, andflatten - Drop-in feel: Built directly on top of
set
Installation
pip install betterset
Or with uv:
uv add betterset
Quick Start
from betterset import BetterSet as S
A = S({1, 2, 3})
B = S({3, 4})
# Operators
assert (A + B) == {1, 2, 3, 4} # union via +
assert (A * {"x", "y"}) == {(1, "x"), (1, "y"), (2, "x"), (2, "y"), (3, "x"), (3, "y")}
assert (S({1, 2}) ** 2) == {(1, 1), (1, 2), (2, 1), (2, 2)}
assert (S({(1, 2), (2, 3)}) @ S({(2, 5), (3, 7)})) == {(1, 5), (2, 7)}
# Algebraic
ps = A.powerset() # set of frozensets
assert frozenset({1, 2}) in ps
assert A.disjoint({4, 5}) is True
assert A.complement({1, 2, 3, 4, 5}) == {4, 5}
parts = A.partition(2) # partitions into 2 non-empty blocks
# Functional
assert A.map(lambda x: x % 2) == {0, 1}
assert A.filter(lambda x: x >= 2) == {2, 3}
assert A.reduce(lambda acc, x: acc + x, 0) == 6
assert S.flatten([[1, 2], {2, 3}, (3, 4)]) == {1, 2, 3, 4}
# Cartesian as method
assert A.cartesian({"x"}) == {(1, "x"), (2, "x"), (3, "x")}
# Closure under an operation
def step(x: int):
return [x + 1] if x < 3 else []
assert S({1}).closure(step) == {1, 2, 3}
API Reference
Class: BetterSet
-
Operators
A + B→ unionA * B→ cartesian product of elements as tuplesA ** n→ n-fold cartesian product (A ** 0 == {()})A @ B→ relation composition whenAandBare sets of pairs- In-place variants:
+=,*=
-
Algebraic methods
powerset() -> BetterSet[FrozenSet[T]]cartesian(other: Iterable[U]) -> BetterSet[tuple[T, U]]disjoint(other: Iterable[T]) -> boolcomplement(universe: Iterable[T]) -> BetterSet[T]partition(k: int) -> BetterSet[tuple[tuple[T, ...], ...]]closure(op: Callable[[T], Iterable[T]]) -> BetterSet[T]
-
Functional methods
map(func: Callable[[T], U]) -> BetterSet[U]filter(predicate: Callable[[T], bool]) -> BetterSet[T]reduce(func: Callable[[U, T], U], initial: Optional[U] = None) -> U@classmethod flatten(iterable: Iterable[Union[Iterable[T], T, None]], *, skip_none: bool = True) -> BetterSet[T]
Notes on flatten:
- Strings/bytes/bytearray/memoryview are treated as atomic values.
- Other iterables are flattened (e.g., frozenset, numpy arrays).
- Mappings iterate keys by default; use
mapping.items()for key/value pairs. Nonevalues are skipped by default (skip_none=True).
Requirements
- Python 3.8+
Development
# Clone the repository
git clone https://github.com/izzet/betterset.git
cd betterset
# Install with uv (uses uv.lock for reproducible builds)
uv sync --group dev
# Set up pre-commit hooks (recommended)
uv run pre-commit install
# Run tests
uv run pytest
# Format code
uv run ruff format .
# Check linting
uv run ruff check .
# Build package
uv build
Note: This project uses uv.lock for reproducible dependency management. The lock file is committed to ensure all developers and CI/CD use identical dependency versions.
Pre-commit hooks: The project includes pre-commit hooks that automatically format code, check linting, and run tests before each commit to maintain code quality.
License
MIT License. See LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
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
File details
Details for the file betterset-0.2.0.tar.gz.
File metadata
- Download URL: betterset-0.2.0.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd5e3566ce8ef9e70e96cde6de69ae58f2a9f9436fc6c614a05c0407a4fb3142
|
|
| MD5 |
af4bbefc97d793a0dbefd0677adef13b
|
|
| BLAKE2b-256 |
1b7d393538b647d4a5af67106d7412d811959522f80e9cc329f04d31c0464fdc
|
Provenance
The following attestation bundles were made for betterset-0.2.0.tar.gz:
Publisher:
release.yaml on izzet/betterset
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
betterset-0.2.0.tar.gz -
Subject digest:
cd5e3566ce8ef9e70e96cde6de69ae58f2a9f9436fc6c614a05c0407a4fb3142 - Sigstore transparency entry: 869232443
- Sigstore integration time:
-
Permalink:
izzet/betterset@68ac8cdd8e48ea36dcb48af1cd2b5074475f611c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/izzet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@68ac8cdd8e48ea36dcb48af1cd2b5074475f611c -
Trigger Event:
release
-
Statement type:
File details
Details for the file betterset-0.2.0-py3-none-any.whl.
File metadata
- Download URL: betterset-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9395c8e0365e137d2e3365a176c66976d7a2160cd8ba2b3697e01dc66304fd74
|
|
| MD5 |
94e4c808206ff553ed0af940833338a9
|
|
| BLAKE2b-256 |
bcb0b5ee16c180b79d43f9fb7f356721b33d0ebb02aaa5d093281b63b4cbf769
|
Provenance
The following attestation bundles were made for betterset-0.2.0-py3-none-any.whl:
Publisher:
release.yaml on izzet/betterset
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
betterset-0.2.0-py3-none-any.whl -
Subject digest:
9395c8e0365e137d2e3365a176c66976d7a2160cd8ba2b3697e01dc66304fd74 - Sigstore transparency entry: 869232444
- Sigstore integration time:
-
Permalink:
izzet/betterset@68ac8cdd8e48ea36dcb48af1cd2b5074475f611c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/izzet
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@68ac8cdd8e48ea36dcb48af1cd2b5074475f611c -
Trigger Event:
release
-
Statement type: