def-result


Namedef-result JSON
Version 1.0.1 PyPI version JSON
download
home_pagehttps://github.com/Payadel/def_result
SummaryA functional error handling library for working with the results of a function to improve managing errors.
upload_time2023-04-03 15:19:03
maintainer
docs_urlNone
authorPayadel
requires_python>=3.10,<4.0
licenseGPLV3
keywords functional programming error handling def function
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">
  <h1>def_result</h1>
  <br />

## Project Discontinuation Announcement

`def_result` will not be developed anymore.

Please use **[on_rails](https://github.com/Payadel/on_rails/)** library.

The **[on_rails](https://github.com/Payadel/on_rails/)** project is better and more complete than this project. In
addition to the features of this project, it has tools for railway oriented programming (ROP).

Thank you
  <br />
  <br />

  <a href="#getting-started"><strong>Getting Started ยป</strong></a>
  <br />
  <br />
  <a href="https://github.com/Payadel/def_result/issues/new?assignees=&labels=scope-bug&template=BUG_REPORT.md&title=bug%3A+">Report a Bug</a>
  ยท
  <a href="https://github.com/Payadel/def_result/issues/new?assignees=&labels=scope-enhancement&template=FEATURE_REQUEST.md&title=feat%3A+">Request a Feature</a>
  .
  <a href="https://github.com/Payadel/def_result/issues/new?assignees=&labels=help-wanted&template=SUPPORT_QUESTION.md&title=support%3A+">Ask a Question</a>
</div>

<div align="center">
<br />

[![code with love by Payadel](https://img.shields.io/badge/%3C%2F%3E%20with%20%E2%99%A5%20by-Payadel-ff1414.svg?style=flat-square)](https://github.com/Payadel)

[![Build Status](https://img.shields.io/github/actions/workflow/status/Payadel/def_result/build.yaml?branch=dev)](https://github.com/Payadel/def_result/actions/workflows/build.yaml?query=branch%3Adev)
[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](coverage.md)
[![PyPI](https://img.shields.io/pypi/v/def_result.svg)](https://pypi.org/project/def_result/)

![GitHub](https://img.shields.io/github/license/Payadel/def_result)
[![Pull Requests welcome](https://img.shields.io/badge/PRs-welcome-ff69b4.svg?style=flat-square)](https://github.com/Payadel/def_result/issues?q=is%3Aissue+is%3Aopen)

</div>
<details>
<summary>Table of Contents</summary>

- [About](#about)
    - [Purpose](#Purpose)
    - [Motivation](#Motivation)
    - [What problems are solved?](#what-problems-are-solved)
- [Getting Started](#getting-started)
- [Usage](#usage)
    - [Documentation](#documentation)
- [CHANGELOG](#changelog)
- [Features](#features)
- [Roadmap](#roadmap)
- [Support](#support)
- [FAQ](#faq)
- [Project assistance](#project-assistance)
- [Contributing](#contributing)
- [Authors & contributors](#authors--contributors)
- [Security](#security)
- [License](#license)

</details>

## About

`def_result` is a library for **python**.

It is a library for functional error handling to improve **managing errors**.

It stores result of functions and provides a simple way to handle and manipulate the return values of functions, with
built-in support for error handling and more.

### Purpose

In functional programming, it is not always appropriate to use traditional `try-except` blocks because they can lead to
code that is difficult to read, understand, and maintain.

The purpose of Functional Error Handling Libraries is to provide developers with a set of abstractions and tools for
managing errors in a functional way.

`def_result` is a functional error handling library. The goal of this library is to make error handling m**ore explicit,
composable, and testable**. By using this library, developers can write code that is **more robust, maintainable, and
expressive**.

### Motivation

The motivation behind this library is the desire to write code that is **more reliable, easier to understand, and less
prone to errors**. In many cases, functional programming languages provide built-in abstractions for handling errors.
However, for languages that do not have built-in support for functional error handling, libraries like this can provide
a useful alternative.

### What problems are solved?

This library can solve several problems, including:

- Ensuring that error handling is consistent across an application.
- Making it easier to test error handling code.
- Making error handling code more readable and maintainable.
- Encouraging developers to handle errors in a more functional way, which can lead to more reliable and robust code.

Developers can spend less time debugging and more time writing code that adds value to their organization. Additionally,
by using functional programming concepts, developers can write code that is easier to reason about and understand, which
can lead to faster development cycles and better quality code.

## Getting Started

Use `pip` to install package:

`pip install def_result`

## Usage

### Sample 1: use decorator for pure functions

In this example, `def_result` is used as a decorator to wrap the `divide_numbers` function.

```python
from def_result import def_result


@def_result
def divide_numbers(a: int, b: int):
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b


result = divide_numbers(10, 0)

if result.success:
    print(f"Operation was successful: {result.value}")
else:
    print(f"Operation failed: {str(result.detail)}")
```

### Sample 2: use `Result` in function

```python
from def_result import Result
from def_result.ResultDetails.Errors import ValidationError


def divide_numbers(a: int, b: int):
    if b == 0:
        return Result.fail(ValidationError(message="Cannot divide by zero"))
    return Result.ok(a / b)


result = divide_numbers(10, 0)

if result.success:
    print(f"Operation was successful: {result.value}")
else:
    print(f"Operation failed:")
    print(str(result.detail))

```

## CHANGELOG

Please see the [CHANGELOG](https://github.com/Payadel/def_result/blob/main/CHANGELOG.md) file.

## Features

- **Easy to use:** `def_result` is designed to be simple and easy to use, with a minimal API and clear documentation.
- **Compatibility with existing code:** `def_result` can be easily added to existing codes without the need for major
  refactoring. You can use decorator for wrap old functions or write new functions without worrying about
  incompatibilities.
- **Save any details you like:** Thanks to
  the [ResultDetail](https://github.com/Payadel/def_result/blob/main/def_result/ResultDetail.py) class, you can store
  various information about the output of the function. Also, by inheriting from this class, you can write new and
  customized classes for your project.
- **Special details for errors:** With
  the [ErrorDetail](https://github.com/Payadel/def_result/blob/main/def_result/ResultDetails/ErrorDetail.py) class, you
  can store specific details about errors. For example, this class supports **stack trace** in a built-in way.
- **Support for common details by default:**
  In [this link](https://github.com/Payadel/def_result/tree/main/def_result/ResultDetails), you can see the different
  types of details that are supported.

## Roadmap

See the [open issues](https://github.com/Payadel/def_result/issues) for a list of proposed features (and known issues).

- [Top Feature Requests](https://github.com/Payadel/def_result/issues?q=label%3Ascope-enhancement+is%3Aopen+sort%3Areactions-%2B1-desc) (
  Add your votes using the ๐Ÿ‘ reaction)
- [Top Bugs](https://github.com/Payadel/def_result/issues?q=is%3Aissue+is%3Aopen+label%3Ascope-bug+sort%3Areactions-%2B1-desc) (
  Add your votes using the ๐Ÿ‘ reaction)
- [Newest Bugs](https://github.com/Payadel/def_result/issues?q=is%3Aopen+is%3Aissue+label%3Ascope-bug)

## Support

Reach out to the maintainers at one of the following places:

- [GitHub issues](https://github.com/Payadel/def_result/issues/new?assignees=&labels=help-wanted&template=SUPPORT_QUESTION.md&title=support%3A+)

## FAQ

#### Do I need rewrite all the functions in a new way?

**not necessarily.** You can add this library and write new functions without changing the previous codes.

Also for old functions, you can use **decorator**. By using decorator, The output of the function is converted
to `Result` format. This way, your code is wrap in a `try-except` block to handle all exceptions.

#### How to manage all function exceptions?

By using decorator, your code is wrap in a `try-except` block and the final output is converted to Result. In this way,
all exceptions are handled.

## Project assistance

**First of, thank you.**

If you want to say thank you or/and support active development of `def_result`:

- Add a [GitHub Star](https://github.com/Payadel/def_result) to the project.
- Write interesting articles about the project on [Dev.to](https://dev.to/), [Medium](https://medium.com/) or your
  personal blog.

Together, we can make `def_result` **better**!

## Contributing

First off, thanks for taking the time to contribute! Contributions are what make the free/open-source community such an
amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are **greatly
appreciated**.

Please read [our contribution guidelines](https://github.com/Payadel/def_result/blob/main/docs/CONTRIBUTING.md), and
thank you for being involved!

Please do not forget that this project uses [conventional commits](https://www.conventionalcommits.org), so please
follow the specification in your commit messages. You can see valid types
from [this file](https://github.com/Payadel/def_result/blob/main/.configs/commitlint.config.js).

## Authors & contributors

The original setup of this repository is by [Payadel](https://github.com/Payadel).

For a full list of all authors and contributors,
see [the contributors page](https://github.com/Payadel/def_result/contributors).

## Security

`def_result` follows good practices of security, but 100% security cannot be assured. `def_result` is provided **"as
is"** without any **warranty**.

_For more information and to report security issues, please refer to
our [security documentation](https://github.com/Payadel/def_result/blob/main/docs/SECURITY.md)._

## License

This project is licensed under the **GPLv3**.

See [LICENSE](https://github.com/Payadel/def_result/blob/main/LICENSE) for more information.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Payadel/def_result",
    "name": "def-result",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "functional programming,error handling,def,function",
    "author": "Payadel",
    "author_email": "payadelteam@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e9/79/b9e0a2c362b679cc4b63c90acca12ced5b310dc3431681f1850862c2dc8f/def_result-1.0.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n  <h1>def_result</h1>\n  <br />\n\n## Project Discontinuation Announcement\n\n`def_result` will not be developed anymore.\n\nPlease use **[on_rails](https://github.com/Payadel/on_rails/)** library.\n\nThe **[on_rails](https://github.com/Payadel/on_rails/)** project is better and more complete than this project. In\naddition to the features of this project, it has tools for railway oriented programming (ROP).\n\nThank you\n  <br />\n  <br />\n\n  <a href=\"#getting-started\"><strong>Getting Started \u00bb</strong></a>\n  <br />\n  <br />\n  <a href=\"https://github.com/Payadel/def_result/issues/new?assignees=&labels=scope-bug&template=BUG_REPORT.md&title=bug%3A+\">Report a Bug</a>\n  \u00b7\n  <a href=\"https://github.com/Payadel/def_result/issues/new?assignees=&labels=scope-enhancement&template=FEATURE_REQUEST.md&title=feat%3A+\">Request a Feature</a>\n  .\n  <a href=\"https://github.com/Payadel/def_result/issues/new?assignees=&labels=help-wanted&template=SUPPORT_QUESTION.md&title=support%3A+\">Ask a Question</a>\n</div>\n\n<div align=\"center\">\n<br />\n\n[![code with love by Payadel](https://img.shields.io/badge/%3C%2F%3E%20with%20%E2%99%A5%20by-Payadel-ff1414.svg?style=flat-square)](https://github.com/Payadel)\n\n[![Build Status](https://img.shields.io/github/actions/workflow/status/Payadel/def_result/build.yaml?branch=dev)](https://github.com/Payadel/def_result/actions/workflows/build.yaml?query=branch%3Adev)\n[![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](coverage.md)\n[![PyPI](https://img.shields.io/pypi/v/def_result.svg)](https://pypi.org/project/def_result/)\n\n![GitHub](https://img.shields.io/github/license/Payadel/def_result)\n[![Pull Requests welcome](https://img.shields.io/badge/PRs-welcome-ff69b4.svg?style=flat-square)](https://github.com/Payadel/def_result/issues?q=is%3Aissue+is%3Aopen)\n\n</div>\n<details>\n<summary>Table of Contents</summary>\n\n- [About](#about)\n    - [Purpose](#Purpose)\n    - [Motivation](#Motivation)\n    - [What problems are solved?](#what-problems-are-solved)\n- [Getting Started](#getting-started)\n- [Usage](#usage)\n    - [Documentation](#documentation)\n- [CHANGELOG](#changelog)\n- [Features](#features)\n- [Roadmap](#roadmap)\n- [Support](#support)\n- [FAQ](#faq)\n- [Project assistance](#project-assistance)\n- [Contributing](#contributing)\n- [Authors & contributors](#authors--contributors)\n- [Security](#security)\n- [License](#license)\n\n</details>\n\n## About\n\n`def_result` is a library for **python**.\n\nIt is a library for functional error handling to improve **managing errors**.\n\nIt stores result of functions and provides a simple way to handle and manipulate the return values of functions, with\nbuilt-in support for error handling and more.\n\n### Purpose\n\nIn functional programming, it is not always appropriate to use traditional `try-except` blocks because they can lead to\ncode that is difficult to read, understand, and maintain.\n\nThe purpose of Functional Error Handling Libraries is to provide developers with a set of abstractions and tools for\nmanaging errors in a functional way.\n\n`def_result` is a functional error handling library. The goal of this library is to make error handling m**ore explicit,\ncomposable, and testable**. By using this library, developers can write code that is **more robust, maintainable, and\nexpressive**.\n\n### Motivation\n\nThe motivation behind this library is the desire to write code that is **more reliable, easier to understand, and less\nprone to errors**. In many cases, functional programming languages provide built-in abstractions for handling errors.\nHowever, for languages that do not have built-in support for functional error handling, libraries like this can provide\na useful alternative.\n\n### What problems are solved?\n\nThis library can solve several problems, including:\n\n- Ensuring that error handling is consistent across an application.\n- Making it easier to test error handling code.\n- Making error handling code more readable and maintainable.\n- Encouraging developers to handle errors in a more functional way, which can lead to more reliable and robust code.\n\nDevelopers can spend less time debugging and more time writing code that adds value to their organization. Additionally,\nby using functional programming concepts, developers can write code that is easier to reason about and understand, which\ncan lead to faster development cycles and better quality code.\n\n## Getting Started\n\nUse `pip` to install package:\n\n`pip install def_result`\n\n## Usage\n\n### Sample 1: use decorator for pure functions\n\nIn this example, `def_result` is used as a decorator to wrap the `divide_numbers` function.\n\n```python\nfrom def_result import def_result\n\n\n@def_result\ndef divide_numbers(a: int, b: int):\n    if b == 0:\n        raise ValueError(\"Cannot divide by zero\")\n    return a / b\n\n\nresult = divide_numbers(10, 0)\n\nif result.success:\n    print(f\"Operation was successful: {result.value}\")\nelse:\n    print(f\"Operation failed: {str(result.detail)}\")\n```\n\n### Sample 2: use `Result` in function\n\n```python\nfrom def_result import Result\nfrom def_result.ResultDetails.Errors import ValidationError\n\n\ndef divide_numbers(a: int, b: int):\n    if b == 0:\n        return Result.fail(ValidationError(message=\"Cannot divide by zero\"))\n    return Result.ok(a / b)\n\n\nresult = divide_numbers(10, 0)\n\nif result.success:\n    print(f\"Operation was successful: {result.value}\")\nelse:\n    print(f\"Operation failed:\")\n    print(str(result.detail))\n\n```\n\n## CHANGELOG\n\nPlease see the [CHANGELOG](https://github.com/Payadel/def_result/blob/main/CHANGELOG.md) file.\n\n## Features\n\n- **Easy to use:** `def_result` is designed to be simple and easy to use, with a minimal API and clear documentation.\n- **Compatibility with existing code:** `def_result` can be easily added to existing codes without the need for major\n  refactoring. You can use decorator for wrap old functions or write new functions without worrying about\n  incompatibilities.\n- **Save any details you like:** Thanks to\n  the [ResultDetail](https://github.com/Payadel/def_result/blob/main/def_result/ResultDetail.py) class, you can store\n  various information about the output of the function. Also, by inheriting from this class, you can write new and\n  customized classes for your project.\n- **Special details for errors:** With\n  the [ErrorDetail](https://github.com/Payadel/def_result/blob/main/def_result/ResultDetails/ErrorDetail.py) class, you\n  can store specific details about errors. For example, this class supports **stack trace** in a built-in way.\n- **Support for common details by default:**\n  In [this link](https://github.com/Payadel/def_result/tree/main/def_result/ResultDetails), you can see the different\n  types of details that are supported.\n\n## Roadmap\n\nSee the [open issues](https://github.com/Payadel/def_result/issues) for a list of proposed features (and known issues).\n\n- [Top Feature Requests](https://github.com/Payadel/def_result/issues?q=label%3Ascope-enhancement+is%3Aopen+sort%3Areactions-%2B1-desc) (\n  Add your votes using the \ud83d\udc4d reaction)\n- [Top Bugs](https://github.com/Payadel/def_result/issues?q=is%3Aissue+is%3Aopen+label%3Ascope-bug+sort%3Areactions-%2B1-desc) (\n  Add your votes using the \ud83d\udc4d reaction)\n- [Newest Bugs](https://github.com/Payadel/def_result/issues?q=is%3Aopen+is%3Aissue+label%3Ascope-bug)\n\n## Support\n\nReach out to the maintainers at one of the following places:\n\n- [GitHub issues](https://github.com/Payadel/def_result/issues/new?assignees=&labels=help-wanted&template=SUPPORT_QUESTION.md&title=support%3A+)\n\n## FAQ\n\n#### Do I need rewrite all the functions in a new way?\n\n**not necessarily.** You can add this library and write new functions without changing the previous codes.\n\nAlso for old functions, you can use **decorator**. By using decorator, The output of the function is converted\nto `Result` format. This way, your code is wrap in a `try-except` block to handle all exceptions.\n\n#### How to manage all function exceptions?\n\nBy using decorator, your code is wrap in a `try-except` block and the final output is converted to Result. In this way,\nall exceptions are handled.\n\n## Project assistance\n\n**First of, thank you.**\n\nIf you want to say thank you or/and support active development of `def_result`:\n\n- Add a [GitHub Star](https://github.com/Payadel/def_result) to the project.\n- Write interesting articles about the project on [Dev.to](https://dev.to/), [Medium](https://medium.com/) or your\n  personal blog.\n\nTogether, we can make `def_result` **better**!\n\n## Contributing\n\nFirst off, thanks for taking the time to contribute! Contributions are what make the free/open-source community such an\namazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are **greatly\nappreciated**.\n\nPlease read [our contribution guidelines](https://github.com/Payadel/def_result/blob/main/docs/CONTRIBUTING.md), and\nthank you for being involved!\n\nPlease do not forget that this project uses [conventional commits](https://www.conventionalcommits.org), so please\nfollow the specification in your commit messages. You can see valid types\nfrom [this file](https://github.com/Payadel/def_result/blob/main/.configs/commitlint.config.js).\n\n## Authors & contributors\n\nThe original setup of this repository is by [Payadel](https://github.com/Payadel).\n\nFor a full list of all authors and contributors,\nsee [the contributors page](https://github.com/Payadel/def_result/contributors).\n\n## Security\n\n`def_result` follows good practices of security, but 100% security cannot be assured. `def_result` is provided **\"as\nis\"** without any **warranty**.\n\n_For more information and to report security issues, please refer to\nour [security documentation](https://github.com/Payadel/def_result/blob/main/docs/SECURITY.md)._\n\n## License\n\nThis project is licensed under the **GPLv3**.\n\nSee [LICENSE](https://github.com/Payadel/def_result/blob/main/LICENSE) for more information.\n",
    "bugtrack_url": null,
    "license": "GPLV3",
    "summary": "A functional error handling library for working with the results of a function to improve managing errors.",
    "version": "1.0.1",
    "split_keywords": [
        "functional programming",
        "error handling",
        "def",
        "function"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3403d27894f1a148e7ebe9007ef5e380c9e291824a1b2f67b28abd0420a3c1ff",
                "md5": "61af46d388e6985df166919ca611be82",
                "sha256": "0801ef16a68f3b926c315f702ef0a0f1d363581c8cbcc99cf579a5ae1f331389"
            },
            "downloads": -1,
            "filename": "def_result-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "61af46d388e6985df166919ca611be82",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 28041,
            "upload_time": "2023-04-03T15:19:02",
            "upload_time_iso_8601": "2023-04-03T15:19:02.384145Z",
            "url": "https://files.pythonhosted.org/packages/34/03/d27894f1a148e7ebe9007ef5e380c9e291824a1b2f67b28abd0420a3c1ff/def_result-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e979b9e0a2c362b679cc4b63c90acca12ced5b310dc3431681f1850862c2dc8f",
                "md5": "e834c6f3843689a1ca5ef51f0de07323",
                "sha256": "78a3c1319de9e35f2ed3999481d508c1b005de2a6c757f026b55788513b94ff0"
            },
            "downloads": -1,
            "filename": "def_result-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e834c6f3843689a1ca5ef51f0de07323",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 23853,
            "upload_time": "2023-04-03T15:19:03",
            "upload_time_iso_8601": "2023-04-03T15:19:03.670536Z",
            "url": "https://files.pythonhosted.org/packages/e9/79/b9e0a2c362b679cc4b63c90acca12ced5b310dc3431681f1850862c2dc8f/def_result-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-03 15:19:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Payadel",
    "github_project": "def_result",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "def-result"
}
        
Elapsed time: 0.05079s