wraps-core


Namewraps-core JSON
Version 0.3.0 PyPI version JSON
download
home_pageNone
SummaryCore functionality of wraps.
upload_time2024-08-10 18:52:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024-present, Nikita Tikhonov (nekitdev) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords core either option python result
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `wraps-core`

[![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]

> *Core functionality of wraps.*

## Installing

**Python 3.8 or above is required.**

### `pip`

Installing the library with `pip` is quite simple:

```console
$ pip install wraps-core
```

Alternatively, the library can be installed from the source:

```console
$ pip install git+https://github.com/nekitdev/wraps-core.git
```

Or via cloning the repository:

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

### `uv`

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

```console
$ uv add wraps-core
```

## Examples

### Option

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

Here is an example of `divide` function returning `Option[float]` instead of raising an error when
the denominator is zero:

```python
# option.py

from wraps.core import NULL, Option, Some


def divide(numerator: float, denominator: float) -> Option[float]:
    return Some(numerator / denominator) if denominator else NULL
```

There are two ways to process the resulting option: either via pattern matching or predicates.

```python
# option_matching.py

from wraps.core import Null, Some

from option import divide

DIVISION_BY_ZERO = "division by zero"

match divide(1.0, 2.0):
    case Some(value):
        print(value)

    case Null():
        print(DIVISION_BY_ZERO)
```

```python
# option_predicates.py

from option import divide

DIVISION_BY_ZERO = "division by zero"

option = divide(1.0, 2.0)

if option.is_some():
    # here we know that the `option` is `Some[float]`, so it is safe to unwrap it
    print(option.unwrap())

else:
    # and here we know that the `option` is `Null`
    print(DIVISION_BY_ZERO)
```

Note that both examples are fully type safe.

### Result

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

Below is the enhanced `divide` function from above, now using `Result[float, DivideError]`
instead of `Option[float]`.

```python
# result.py

from enum import Enum

from wraps.core 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)
```

Using new `divide` is as simple as the old one:

```python
# result_matching.py

from wraps.core import Error, Ok

from result import divide

match divide(1.0, 2.0):
    case Ok(value):
        print(value)

    case Error(error):
        print(error.value)  # we use `value` here to get error details
```

```python
# result_predicates.py

from result import divide

result = divide(1.0, 2.0)

if result.is_ok():
    # here we know the `result` is `Ok[float]`, so we can unwrap it safely
    print(result.unwrap())

else:
    # and here the `result` is `Error[DivideError]`, so we can unwrap the error safely
    print(result.unwrap_error().value)
```

### Decorators

### Early Return

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

Here is an example using [`wrap_option_on`][wraps.core.option.wrap_option_on] to catch errors:

```python
from wraps.core 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 divide_string(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-core` [here][Security].

## Contributing

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

## License

`wraps-core` 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-core/actions

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

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

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

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

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

[wraps-decorators]: https://github.com/nekitdev/wraps-decorators

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

[wraps.core.option.wrap_option_on]: https://nekitdev.github.io/wraps-core/reference/option#wraps.core.option.wrap_option_on

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

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "wraps-core",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "core, either, option, python, result",
    "author": null,
    "author_email": "nekitdev <nekit@nekit.dev>",
    "download_url": "https://files.pythonhosted.org/packages/9c/60/51baa4cde755a67d0805d4ec92615e5d016c3a6a37073c0ce17f6a1b599b/wraps_core-0.3.0.tar.gz",
    "platform": null,
    "description": "# `wraps-core`\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> *Core functionality of wraps.*\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-core\n```\n\nAlternatively, the library can be installed from the source:\n\n```console\n$ pip install git+https://github.com/nekitdev/wraps-core.git\n```\n\nOr via cloning the repository:\n\n```console\n$ git clone https://github.com/nekitdev/wraps-core.git\n$ cd wraps-core\n$ pip install .\n```\n\n### `uv`\n\nYou can add `wraps-core` as a dependency with the following command:\n\n```console\n$ uv add wraps-core\n```\n\n## Examples\n\n### Option\n\n[`Option[T]`][wraps.core.option.Option] type represents an optional value: every option is either\n[`Some[T]`][wraps.core.option.Some] and contains a value, or [`Null`][wraps.core.option.Null],\nand does not.\n\nHere is an example of `divide` function returning `Option[float]` instead of raising an error when\nthe denominator is zero:\n\n```python\n# option.py\n\nfrom wraps.core import NULL, Option, Some\n\n\ndef divide(numerator: float, denominator: float) -> Option[float]:\n    return Some(numerator / denominator) if denominator else NULL\n```\n\nThere are two ways to process the resulting option: either via pattern matching or predicates.\n\n```python\n# option_matching.py\n\nfrom wraps.core import Null, Some\n\nfrom option import divide\n\nDIVISION_BY_ZERO = \"division by zero\"\n\nmatch divide(1.0, 2.0):\n    case Some(value):\n        print(value)\n\n    case Null():\n        print(DIVISION_BY_ZERO)\n```\n\n```python\n# option_predicates.py\n\nfrom option import divide\n\nDIVISION_BY_ZERO = \"division by zero\"\n\noption = divide(1.0, 2.0)\n\nif option.is_some():\n    # here we know that the `option` is `Some[float]`, so it is safe to unwrap it\n    print(option.unwrap())\n\nelse:\n    # and here we know that the `option` is `Null`\n    print(DIVISION_BY_ZERO)\n```\n\nNote that both examples are fully type safe.\n\n### Result\n\n[`Result[T, E]`][wraps.core.result.Result] is the type used for returning and propagating errors.\nIt has two variants, [`Ok[T]`][wraps.core.result.Ok], representing success and containing a value,\nand [`Error[E]`][wraps.core.result.Error], representing error and containing an error value.\n\nBelow is the enhanced `divide` function from above, now using `Result[float, DivideError]`\ninstead of `Option[float]`.\n\n```python\n# result.py\n\nfrom enum import Enum\n\nfrom wraps.core 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\nUsing new `divide` is as simple as the old one:\n\n```python\n# result_matching.py\n\nfrom wraps.core import Error, Ok\n\nfrom result import divide\n\nmatch divide(1.0, 2.0):\n    case Ok(value):\n        print(value)\n\n    case Error(error):\n        print(error.value)  # we use `value` here to get error details\n```\n\n```python\n# result_predicates.py\n\nfrom result import divide\n\nresult = divide(1.0, 2.0)\n\nif result.is_ok():\n    # here we know the `result` is `Ok[float]`, so we can unwrap it safely\n    print(result.unwrap())\n\nelse:\n    # and here the `result` is `Error[DivideError]`, so we can unwrap the error safely\n    print(result.unwrap_error().value)\n```\n\n### Decorators\n\n### Early Return\n\nEarly return functionality (like the *question mark* (`?`) operator in Rust) is implemented via\n`early` methods (for both [`Option[T]`][wraps.core.option.Option]\nand [`Result[T, E]`][wraps.core.result.Result] types) combined with the\n[`@early_option`][wraps.core.early.decorators.early_option] and\n[`@early_result`][wraps.core.early.decorators.early_result] decorators respectively.\n\nHere is an example using [`wrap_option_on`][wraps.core.option.wrap_option_on] to catch errors:\n\n```python\nfrom wraps.core 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 divide_string(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-core` [here][Security].\n\n## Contributing\n\nIf you are interested in contributing to `wraps-core`, 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-core` 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-core/actions\n\n[Changelog]: https://github.com/nekitdev/wraps-core/blob/main/CHANGELOG.md\n[Code of Conduct]: https://github.com/nekitdev/wraps-core/blob/main/CODE_OF_CONDUCT.md\n[Contributing Guide]: https://github.com/nekitdev/wraps-core/blob/main/CONTRIBUTING.md\n[Security]: https://github.com/nekitdev/wraps-core/blob/main/SECURITY.md\n\n[License]: https://github.com/nekitdev/wraps-core/blob/main/LICENSE\n\n[Package]: https://pypi.org/project/wraps-core\n[Coverage]: https://codecov.io/gh/nekitdev/wraps-core\n[Documentation]: https://nekitdev.github.io/wraps-core\n\n[Discord Badge]: https://img.shields.io/discord/728012506899021874\n[License Badge]: https://img.shields.io/pypi/l/wraps-core\n[Version Badge]: https://img.shields.io/pypi/v/wraps-core\n[Downloads Badge]: https://img.shields.io/pypi/dm/wraps-core\n\n[Documentation Badge]: https://github.com/nekitdev/wraps-core/workflows/docs/badge.svg\n[Check Badge]: https://github.com/nekitdev/wraps-core/workflows/check/badge.svg\n[Test Badge]: https://github.com/nekitdev/wraps-core/workflows/test/badge.svg\n[Coverage Badge]: https://codecov.io/gh/nekitdev/wraps-core/branch/main/graph/badge.svg\n\n[wraps-decorators]: https://github.com/nekitdev/wraps-decorators\n\n[wraps.core.option.Option]: https://nekitdev.github.io/wraps-core/reference/option#wraps.core.option.Option\n[wraps.core.option.Some]: https://nekitdev.github.io/wraps-core/reference/option#wraps.core.option.Some\n[wraps.core.option.Null]: https://nekitdev.github.io/wraps-core/reference/option#wraps.core.option.Null\n\n[wraps.core.option.wrap_option_on]: https://nekitdev.github.io/wraps-core/reference/option#wraps.core.option.wrap_option_on\n\n[wraps.core.result.Result]: https://nekitdev.github.io/wraps-core/reference/result#wraps.core.result.Result\n[wraps.core.result.Ok]: https://nekitdev.github.io/wraps-core/reference/result#wraps.core.result.Ok\n[wraps.core.result.Error]: https://nekitdev.github.io/wraps-core/reference/result#wraps.core.result.Error\n\n[wraps.core.early.decorators.early_option]: https://nekitdev.github.io/wraps-core/reference/early/decorators#wraps.core.early.decorators.early_option\n[wraps.core.early.decorators.early_result]: https://nekitdev.github.io/wraps-core/reference/early/decorators#wraps.core.early.decorators.early_result\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024-present, Nikita Tikhonov (nekitdev)  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Core functionality of wraps.",
    "version": "0.3.0",
    "project_urls": {
        "Chat": "https://nekit.dev/chat",
        "Documentation": "https://nekitdev.github.io/wraps-core",
        "Funding": "https://nekit.dev/funding",
        "Homepage": "https://github.com/nekitdev/wraps-core",
        "Issues": "https://github.com/nekitdev/wraps-core/issues",
        "Repository": "https://github.com/nekitdev/wraps-core"
    },
    "split_keywords": [
        "core",
        " either",
        " option",
        " python",
        " result"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cf6d36ab06c59005bcac532efce29ea07a53efc9613083201203d1bbf402e3ad",
                "md5": "024ff3701d3efd5c732899bb6b70f15c",
                "sha256": "24368989c7317b3887ab143eb5d68f4ce23eb9a3dcd718065c7fad132a0e0999"
            },
            "downloads": -1,
            "filename": "wraps_core-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "024ff3701d3efd5c732899bb6b70f15c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 27519,
            "upload_time": "2024-08-10T18:52:42",
            "upload_time_iso_8601": "2024-08-10T18:52:42.001987Z",
            "url": "https://files.pythonhosted.org/packages/cf/6d/36ab06c59005bcac532efce29ea07a53efc9613083201203d1bbf402e3ad/wraps_core-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9c6051baa4cde755a67d0805d4ec92615e5d016c3a6a37073c0ce17f6a1b599b",
                "md5": "b20537ff3469bdffa3f689f19c956d0c",
                "sha256": "e37c35f5382b978ddfe92b5ecb19d20b940392ffa237263acda07f03aecb9cca"
            },
            "downloads": -1,
            "filename": "wraps_core-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "b20537ff3469bdffa3f689f19c956d0c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 32597,
            "upload_time": "2024-08-10T18:52:43",
            "upload_time_iso_8601": "2024-08-10T18:52:43.613426Z",
            "url": "https://files.pythonhosted.org/packages/9c/60/51baa4cde755a67d0805d4ec92615e5d016c3a6a37073c0ce17f6a1b599b/wraps_core-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-10 18:52:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nekitdev",
    "github_project": "wraps-core",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "wraps-core"
}
        
Elapsed time: 0.49956s