wraps


Namewraps JSON
Version 0.12.0 PyPI version JSON
download
home_pagehttps://github.com/nekitdev/wraps
SummaryMeaningful and safe wrapping types.
upload_time2024-04-30 16:51:12
maintainerNone
docs_urlNone
authornekitdev
requires_python>=3.8
licenseMIT
keywords python future either option result
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `wraps`

[![License][License Badge]][License]
[![Version][Version Badge]][Package]
[![Downloads][Downloads Badge]][Package]
[![Discord][Discord Badge]][Discord]

[![Documentation][Documentation Badge]][Documentation]
[![Check][Check Badge]][Actions]
[![Test][Test Badge]][Actions]
[![Coverage][Coverage Badge]][Coverage]

> *Meaningful and safe wrapping types.*

## Installing

**Python 3.8 or above is required.**

### pip

Installing the library with `pip` is quite simple:

```console
$ pip install wraps
```

Alternatively, the library can be installed from the source:

```console
$ git clone https://github.com/nekitdev/wraps.git
$ cd wraps
$ python -m pip install .
```

### poetry

You can add `wraps` as a dependency with the following command:

```console
$ poetry add wraps
```

Or by directly specifying it in the configuration like so:

```toml
[tool.poetry.dependencies]
wraps = "^0.12.0"
```

Alternatively, you can add it directly from the source:

```toml
[tool.poetry.dependencies.wraps]
git = "https://github.com/nekitdev/wraps.git"
```

## Examples

### Option

[`Option[T]`][wraps.primitives.option.Option] type represents an optional value: every option is either
[`Some[T]`][wraps.primitives.option.Some] and contains a value, or [`Null`][wraps.primitives.option.Null], and does not.

Here is an example of using [`wrap_option`][wraps.wraps.option.wrap_option] to catch any errors:

```python
from typing import List, TypeVar
from wraps import wrap_option

T = TypeVar("T", covariant=True)


class Array(List[T]):
    @wrap_option
    def get(self, index: int) -> T:
        return self[index]


array = Array([1, 2, 3])

print(array.get(0).unwrap())  # 1
print(array.get(5).unwrap_or(0))  # 0
```

### Result

[`Result[T, E]`][wraps.primitives.result.Result] is the type used for returning and propagating errors.
It has two variants, [`Ok[T]`][wraps.primitives.result.Ok], representing success and containing a value,
and [`Error[E]`][wraps.primitives.result.Error], representing error and containing an error value.

```python
from enum import Enum

from wraps import Error, Ok, Result


class DivideError(Enum):
    DIVISION_BY_ZERO = "division by zero"


def divide(numerator: float, denominator: float) -> Result[float, DivideError]:
    return Ok(numerator / denominator) if denominator else Error(DivideError.DIVISION_BY_ZERO)
```

### Early Return

Early return functionality (like the *question-mark* (`?`) operator in Rust) is implemented via `early` methods
(for both [`Option[T]`][wraps.primitives.option.Option] and [`Result[T, E]`][wraps.primitives.result.Result] types)
combined with the [`@early_option`][wraps.early.decorators.early_option] and
[`@early_result`][wraps.early.decorators.early_result] decorators respectively.

```python
from wraps import Option, early_option, wrap_option_on


@wrap_option_on(ValueError)
def parse(string: str) -> float:
    return float(string)


@wrap_option_on(ZeroDivisionError)
def divide(numerator: float, denominator: float) -> float:
    return numerator / denominator


@early_option
def function(x: str, y: str) -> Option[float]:
    return divide(parse(x).early(), parse(y).early())
```

## Documentation

You can find the documentation [here][Documentation].

## Support

If you need support with the library, you can send us an [email][Email]
or refer to the official [Discord server][Discord].

## Changelog

You can find the changelog [here][Changelog].

## Security Policy

You can find the Security Policy of `wraps` [here][Security].

## Contributing

If you are interested in contributing to `wraps`, make sure to take a look at the
[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].

## License

`wraps` is licensed under the MIT License terms. See [License][License] for details.

[Email]: mailto:support@nekit.dev

[Discord]: https://nekit.dev/chat

[Actions]: https://github.com/nekitdev/wraps/actions

[Changelog]: https://github.com/nekitdev/wraps/blob/main/CHANGELOG.md
[Code of Conduct]: https://github.com/nekitdev/wraps/blob/main/CODE_OF_CONDUCT.md
[Contributing Guide]: https://github.com/nekitdev/wraps/blob/main/CONTRIBUTING.md
[Security]: https://github.com/nekitdev/wraps/blob/main/SECURITY.md

[License]: https://github.com/nekitdev/wraps/blob/main/LICENSE

[Package]: https://pypi.org/project/wraps
[Coverage]: https://codecov.io/gh/nekitdev/wraps
[Documentation]: https://nekitdev.github.io/wraps

[Discord Badge]: https://img.shields.io/discord/728012506899021874
[License Badge]: https://img.shields.io/pypi/l/wraps
[Version Badge]: https://img.shields.io/pypi/v/wraps
[Downloads Badge]: https://img.shields.io/pypi/dm/wraps

[Documentation Badge]: https://github.com/nekitdev/wraps/workflows/docs/badge.svg
[Check Badge]: https://github.com/nekitdev/wraps/workflows/check/badge.svg
[Test Badge]: https://github.com/nekitdev/wraps/workflows/test/badge.svg
[Coverage Badge]: https://codecov.io/gh/nekitdev/wraps/branch/main/graph/badge.svg

[wraps.primitives.option.Option]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Option
[wraps.primitives.option.Some]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Some
[wraps.primitives.option.Null]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Null

[wraps.primitives.result.Result]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Result
[wraps.primitives.result.Ok]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Ok
[wraps.primitives.result.Error]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Error

[wraps.wraps.option.wrap_option]: https://nekitdev.github.io/wraps/reference/wraps/option#wraps.wraps.option.wrap_option

[wraps.early.decorators.early_option]: https://nekitdev.github.io/wraps/reference/early/decorators#wraps.early.decorators.early_option
[wraps.early.decorators.early_result]: https://nekitdev.github.io/wraps/reference/early/decorators#wraps.early.decorators.early_result

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/nekitdev/wraps",
    "name": "wraps",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "python, future, either, option, result",
    "author": "nekitdev",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/8b/9a/daf15d46c96d53c0419aa947dc1828a39dae6918ff452669339114ade511/wraps-0.12.0.tar.gz",
    "platform": null,
    "description": "# `wraps`\n\n[![License][License Badge]][License]\n[![Version][Version Badge]][Package]\n[![Downloads][Downloads Badge]][Package]\n[![Discord][Discord Badge]][Discord]\n\n[![Documentation][Documentation Badge]][Documentation]\n[![Check][Check Badge]][Actions]\n[![Test][Test Badge]][Actions]\n[![Coverage][Coverage Badge]][Coverage]\n\n> *Meaningful and safe wrapping types.*\n\n## Installing\n\n**Python 3.8 or above is required.**\n\n### pip\n\nInstalling the library with `pip` is quite simple:\n\n```console\n$ pip install wraps\n```\n\nAlternatively, the library can be installed from the source:\n\n```console\n$ git clone https://github.com/nekitdev/wraps.git\n$ cd wraps\n$ python -m pip install .\n```\n\n### poetry\n\nYou can add `wraps` as a dependency with the following command:\n\n```console\n$ poetry add wraps\n```\n\nOr by directly specifying it in the configuration like so:\n\n```toml\n[tool.poetry.dependencies]\nwraps = \"^0.12.0\"\n```\n\nAlternatively, you can add it directly from the source:\n\n```toml\n[tool.poetry.dependencies.wraps]\ngit = \"https://github.com/nekitdev/wraps.git\"\n```\n\n## Examples\n\n### Option\n\n[`Option[T]`][wraps.primitives.option.Option] type represents an optional value: every option is either\n[`Some[T]`][wraps.primitives.option.Some] and contains a value, or [`Null`][wraps.primitives.option.Null], and does not.\n\nHere is an example of using [`wrap_option`][wraps.wraps.option.wrap_option] to catch any errors:\n\n```python\nfrom typing import List, TypeVar\nfrom wraps import wrap_option\n\nT = TypeVar(\"T\", covariant=True)\n\n\nclass Array(List[T]):\n    @wrap_option\n    def get(self, index: int) -> T:\n        return self[index]\n\n\narray = Array([1, 2, 3])\n\nprint(array.get(0).unwrap())  # 1\nprint(array.get(5).unwrap_or(0))  # 0\n```\n\n### Result\n\n[`Result[T, E]`][wraps.primitives.result.Result] is the type used for returning and propagating errors.\nIt has two variants, [`Ok[T]`][wraps.primitives.result.Ok], representing success and containing a value,\nand [`Error[E]`][wraps.primitives.result.Error], representing error and containing an error value.\n\n```python\nfrom enum import Enum\n\nfrom wraps import Error, Ok, Result\n\n\nclass DivideError(Enum):\n    DIVISION_BY_ZERO = \"division by zero\"\n\n\ndef divide(numerator: float, denominator: float) -> Result[float, DivideError]:\n    return Ok(numerator / denominator) if denominator else Error(DivideError.DIVISION_BY_ZERO)\n```\n\n### Early Return\n\nEarly return functionality (like the *question-mark* (`?`) operator in Rust) is implemented via `early` methods\n(for both [`Option[T]`][wraps.primitives.option.Option] and [`Result[T, E]`][wraps.primitives.result.Result] types)\ncombined with the [`@early_option`][wraps.early.decorators.early_option] and\n[`@early_result`][wraps.early.decorators.early_result] decorators respectively.\n\n```python\nfrom wraps import Option, early_option, wrap_option_on\n\n\n@wrap_option_on(ValueError)\ndef parse(string: str) -> float:\n    return float(string)\n\n\n@wrap_option_on(ZeroDivisionError)\ndef divide(numerator: float, denominator: float) -> float:\n    return numerator / denominator\n\n\n@early_option\ndef function(x: str, y: str) -> Option[float]:\n    return divide(parse(x).early(), parse(y).early())\n```\n\n## Documentation\n\nYou can find the documentation [here][Documentation].\n\n## Support\n\nIf you need support with the library, you can send us an [email][Email]\nor refer to the official [Discord server][Discord].\n\n## Changelog\n\nYou can find the changelog [here][Changelog].\n\n## Security Policy\n\nYou can find the Security Policy of `wraps` [here][Security].\n\n## Contributing\n\nIf you are interested in contributing to `wraps`, make sure to take a look at the\n[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].\n\n## License\n\n`wraps` is licensed under the MIT License terms. See [License][License] for details.\n\n[Email]: mailto:support@nekit.dev\n\n[Discord]: https://nekit.dev/chat\n\n[Actions]: https://github.com/nekitdev/wraps/actions\n\n[Changelog]: https://github.com/nekitdev/wraps/blob/main/CHANGELOG.md\n[Code of Conduct]: https://github.com/nekitdev/wraps/blob/main/CODE_OF_CONDUCT.md\n[Contributing Guide]: https://github.com/nekitdev/wraps/blob/main/CONTRIBUTING.md\n[Security]: https://github.com/nekitdev/wraps/blob/main/SECURITY.md\n\n[License]: https://github.com/nekitdev/wraps/blob/main/LICENSE\n\n[Package]: https://pypi.org/project/wraps\n[Coverage]: https://codecov.io/gh/nekitdev/wraps\n[Documentation]: https://nekitdev.github.io/wraps\n\n[Discord Badge]: https://img.shields.io/discord/728012506899021874\n[License Badge]: https://img.shields.io/pypi/l/wraps\n[Version Badge]: https://img.shields.io/pypi/v/wraps\n[Downloads Badge]: https://img.shields.io/pypi/dm/wraps\n\n[Documentation Badge]: https://github.com/nekitdev/wraps/workflows/docs/badge.svg\n[Check Badge]: https://github.com/nekitdev/wraps/workflows/check/badge.svg\n[Test Badge]: https://github.com/nekitdev/wraps/workflows/test/badge.svg\n[Coverage Badge]: https://codecov.io/gh/nekitdev/wraps/branch/main/graph/badge.svg\n\n[wraps.primitives.option.Option]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Option\n[wraps.primitives.option.Some]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Some\n[wraps.primitives.option.Null]: https://nekitdev.github.io/wraps/reference/primitives/option#wraps.primitives.option.Null\n\n[wraps.primitives.result.Result]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Result\n[wraps.primitives.result.Ok]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Ok\n[wraps.primitives.result.Error]: https://nekitdev.github.io/wraps/reference/primitives/result#wraps.primitives.result.Error\n\n[wraps.wraps.option.wrap_option]: https://nekitdev.github.io/wraps/reference/wraps/option#wraps.wraps.option.wrap_option\n\n[wraps.early.decorators.early_option]: https://nekitdev.github.io/wraps/reference/early/decorators#wraps.early.decorators.early_option\n[wraps.early.decorators.early_result]: https://nekitdev.github.io/wraps/reference/early/decorators#wraps.early.decorators.early_result\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Meaningful and safe wrapping types.",
    "version": "0.12.0",
    "project_urls": {
        "Chat": "https://nekit.dev/chat",
        "Documentation": "https://nekitdev.github.io/wraps",
        "Funding": "https://nekit.dev/funding",
        "Homepage": "https://github.com/nekitdev/wraps",
        "Issues": "https://github.com/nekitdev/wraps/issues",
        "Repository": "https://github.com/nekitdev/wraps"
    },
    "split_keywords": [
        "python",
        " future",
        " either",
        " option",
        " result"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8a0298e504ae742eb53868505f786e2be78b611a830ae466e0a7e81aea7c7558",
                "md5": "56778fd517d15ba9974261b1d970ba36",
                "sha256": "0c10928dffefbd935cb3fd4acf9ac34a9ee2ed91f90dc8c7754039a5ed81b92c"
            },
            "downloads": -1,
            "filename": "wraps-0.12.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "56778fd517d15ba9974261b1d970ba36",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 39252,
            "upload_time": "2024-04-30T16:51:10",
            "upload_time_iso_8601": "2024-04-30T16:51:10.212903Z",
            "url": "https://files.pythonhosted.org/packages/8a/02/98e504ae742eb53868505f786e2be78b611a830ae466e0a7e81aea7c7558/wraps-0.12.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8b9adaf15d46c96d53c0419aa947dc1828a39dae6918ff452669339114ade511",
                "md5": "dd158ce023cd3ea82586e51b844642ba",
                "sha256": "354d84d04c9cc91842092ccee6584febe5f37af2cc9eccb6ab2446778826dd74"
            },
            "downloads": -1,
            "filename": "wraps-0.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "dd158ce023cd3ea82586e51b844642ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 31217,
            "upload_time": "2024-04-30T16:51:12",
            "upload_time_iso_8601": "2024-04-30T16:51:12.159496Z",
            "url": "https://files.pythonhosted.org/packages/8b/9a/daf15d46c96d53c0419aa947dc1828a39dae6918ff452669339114ade511/wraps-0.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-30 16:51:12",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nekitdev",
    "github_project": "wraps",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "wraps"
}
        
Elapsed time: 0.25789s