Skip to main content

betterset

PyPI version GitHub release Python 3.8+

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, and flatten
  • 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 → union
    • A * B → cartesian product of elements as tuples
    • A ** n → n-fold cartesian product (A ** 0 == {()})
    • A @ B → relation composition when A and B are sets of pairs
    • In-place variants: +=, *=
  • Algebraic methods

    • powerset() -> BetterSet[FrozenSet[T]]
    • cartesian(other: Iterable[U]) -> BetterSet[tuple[T, U]]
    • disjoint(other: Iterable[T]) -> bool
    • complement(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.
  • None values 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.

Metadata

Release files for betterset 0.2.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 betterset 0.2.0
File Size Uploaded Provenance Actions
betterset-0.2.0.tar.gz 24.4 kB With provenance Details for betterset-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for betterset 0.2.0
File Interpreter ABI Platform Actions
betterset-0.2.0-py3-none-any.whl Python 3 none any Details for betterset-0.2.0-py3-none-any.whl

Total release size: 31.1 kB

Release files / betterset-0.2.0.tar.gz

Download URL betterset-0.2.0.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
cd5e3566ce8ef9e70e96cde6de69ae58f2a9f9436fc6c614a05c0407a4fb3142
BLAKE2b-256 checksum
How to use checksums
1b7d393538b647d4a5af67106d7412d811959522f80e9cc329f04d31c0464fdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Jan 29, 2026.

Transparency log

Release files / betterset-0.2.0-py3-none-any.whl

Download URL betterset-0.2.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9395c8e0365e137d2e3365a176c66976d7a2160cd8ba2b3697e01dc66304fd74
BLAKE2b-256 checksum
How to use checksums
bcb0b5ee16c180b79d43f9fb7f356721b33d0ebb02aaa5d093281b63b4cbf769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Jan 29, 2026.

Transparency log

Security for betterset 0.2.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 betterset-0.2.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 Jan 29, 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

0.2.0 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