betterset


Namebetterset JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryExtended set operations: algebraic, functional, and utilities on top of Python set
upload_time2025-08-31 03:20:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords algebra cartesian functional powerset python set
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # betterset

[![PyPI version](https://badge.fury.io/py/betterset.svg)](https://badge.fury.io/py/betterset)
[![GitHub release](https://img.shields.io/github/v/release/izzet/betterset)](https://github.com/izzet/betterset/releases)
[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)

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

```bash
pip install betterset
```

Or with uv:

```bash
uv add betterset
```

## Quick Start

```python
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[Iterable[T]]) -> BetterSet[T]`

## Requirements

- Python 3.8+

## Development

```bash
# 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](LICENSE) file for details.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "betterset",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Izzet Yildirim <izzetcyildirim@gmail.com>",
    "keywords": "algebra, cartesian, functional, powerset, python, set",
    "author": null,
    "author_email": "Izzet Yildirim <izzetcyildirim@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/5d/2b/9071beb3bbc209349fc8cb8cb2d51325d198b2b8d036b78022c79da30d1d/betterset-0.1.0.tar.gz",
    "platform": null,
    "description": "# betterset\n\n[![PyPI version](https://badge.fury.io/py/betterset.svg)](https://badge.fury.io/py/betterset)\n[![GitHub release](https://img.shields.io/github/v/release/izzet/betterset)](https://github.com/izzet/betterset/releases)\n[![Python 3.8+](https://img.shields.io/badge/python-3.8+-blue.svg)](https://www.python.org/downloads/)\n\nExtended set operations: algebraic, functional, and utilities on top of Python's built-in `set`.\n\n## Features\n\n- **Operator overloads**: `+` union, `*` cartesian product, `**` n-fold cartesian, `@` relation composition\n- **Algebraic utilities**: `powerset`, `cartesian`, `complement`, `disjoint`, `partition`, `closure`\n- **Functional utilities**: `map`, `filter`, `reduce`, and `flatten`\n- **Drop-in feel**: Built directly on top of `set`\n\n## Installation\n\n```bash\npip install betterset\n```\n\nOr with uv:\n\n```bash\nuv add betterset\n```\n\n## Quick Start\n\n```python\nfrom betterset import BetterSet as S\n\nA = S({1, 2, 3})\nB = S({3, 4})\n\n# Operators\nassert (A + B) == {1, 2, 3, 4}          # union via +\nassert (A * {\"x\", \"y\"}) == {(1, \"x\"), (1, \"y\"), (2, \"x\"), (2, \"y\"), (3, \"x\"), (3, \"y\")}\nassert (S({1, 2}) ** 2) == {(1, 1), (1, 2), (2, 1), (2, 2)}\nassert (S({(1, 2), (2, 3)}) @ S({(2, 5), (3, 7)})) == {(1, 5), (2, 7)}\n\n# Algebraic\nps = A.powerset()                        # set of frozensets\nassert frozenset({1, 2}) in ps\nassert A.disjoint({4, 5}) is True\nassert A.complement({1, 2, 3, 4, 5}) == {4, 5}\nparts = A.partition(2)                   # partitions into 2 non-empty blocks\n\n# Functional\nassert A.map(lambda x: x % 2) == {0, 1}\nassert A.filter(lambda x: x >= 2) == {2, 3}\nassert A.reduce(lambda acc, x: acc + x, 0) == 6\nassert S.flatten([[1, 2], {2, 3}, (3, 4)]) == {1, 2, 3, 4}\n\n# Cartesian as method\nassert A.cartesian({\"x\"}) == {(1, \"x\"), (2, \"x\"), (3, \"x\")}\n\n# Closure under an operation\ndef step(x: int):\n    return [x + 1] if x < 3 else []\n\nassert S({1}).closure(step) == {1, 2, 3}\n```\n\n## API Reference\n\n### Class: `BetterSet`\n\n- Operators\n\n  - `A + B` \u2192 union\n  - `A * B` \u2192 cartesian product of elements as tuples\n  - `A ** n` \u2192 n-fold cartesian product (`A ** 0 == {()}`)\n  - `A @ B` \u2192 relation composition when `A` and `B` are sets of pairs\n  - In-place variants: `+=`, `*=`\n\n- Algebraic methods\n\n  - `powerset() -> BetterSet[FrozenSet[T]]`\n  - `cartesian(other: Iterable[U]) -> BetterSet[tuple[T, U]]`\n  - `disjoint(other: Iterable[T]) -> bool`\n  - `complement(universe: Iterable[T]) -> BetterSet[T]`\n  - `partition(k: int) -> BetterSet[tuple[tuple[T, ...], ...]]`\n  - `closure(op: Callable[[T], Iterable[T]]) -> BetterSet[T]`\n\n- Functional methods\n  - `map(func: Callable[[T], U]) -> BetterSet[U]`\n  - `filter(predicate: Callable[[T], bool]) -> BetterSet[T]`\n  - `reduce(func: Callable[[U, T], U], initial: Optional[U] = None) -> U`\n  - `@classmethod flatten(iterable: Iterable[Iterable[T]]) -> BetterSet[T]`\n\n## Requirements\n\n- Python 3.8+\n\n## Development\n\n```bash\n# Clone the repository\ngit clone https://github.com/izzet/betterset.git\ncd betterset\n\n# Install with uv (uses uv.lock for reproducible builds)\nuv sync --group dev\n\n# Set up pre-commit hooks (recommended)\nuv run pre-commit install\n\n# Run tests\nuv run pytest\n\n# Format code\nuv run ruff format .\n\n# Check linting\nuv run ruff check .\n\n# Build package\nuv build\n```\n\n**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.\n\n**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.\n\n## License\n\nMIT License. See [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Extended set operations: algebraic, functional, and utilities on top of Python set",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/izzet/betterset/issues",
        "Documentation": "https://github.com/izzet/betterset#readme",
        "Homepage": "https://github.com/izzet/betterset",
        "Repository": "https://github.com/izzet/betterset"
    },
    "split_keywords": [
        "algebra",
        " cartesian",
        " functional",
        " powerset",
        " python",
        " set"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "423fd808a7f1325e55ca40eabc6992359e0e3e41b528fdfb5376b735f1d1c33b",
                "md5": "13334fd49cef44e881b0ffe855a7ee54",
                "sha256": "dda344f4ed71aa02bca8d2af919e1f91dd6f2ee2f04d886e2f17c0689ea2b3c5"
            },
            "downloads": -1,
            "filename": "betterset-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "13334fd49cef44e881b0ffe855a7ee54",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6198,
            "upload_time": "2025-08-31T03:20:49",
            "upload_time_iso_8601": "2025-08-31T03:20:49.178411Z",
            "url": "https://files.pythonhosted.org/packages/42/3f/d808a7f1325e55ca40eabc6992359e0e3e41b528fdfb5376b735f1d1c33b/betterset-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5d2b9071beb3bbc209349fc8cb8cb2d51325d198b2b8d036b78022c79da30d1d",
                "md5": "5a595d85d145462bf62402b770e12ab9",
                "sha256": "791d355a991c7a2b6248039622afb2d29aae082a72e9eb900897ca4f108fc87e"
            },
            "downloads": -1,
            "filename": "betterset-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5a595d85d145462bf62402b770e12ab9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 23823,
            "upload_time": "2025-08-31T03:20:50",
            "upload_time_iso_8601": "2025-08-31T03:20:50.155475Z",
            "url": "https://files.pythonhosted.org/packages/5d/2b/9071beb3bbc209349fc8cb8cb2d51325d198b2b8d036b78022c79da30d1d/betterset-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-31 03:20:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "izzet",
    "github_project": "betterset",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "betterset"
}
        
Elapsed time: 0.95065s