Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

def-result

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

def-result

A functional error handling library for working with the results of a function to improve managing errors.

  • 1.0.1
  • PyPI
  • Socket score

Maintainers
1

def_result


Project Discontinuation Announcement

def_result will not be developed anymore.

Please use on_rails library.

The 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

Getting Started »

Report a Bug · Request a Feature . Ask a Question

Table of Contents

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 more 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.

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

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 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 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 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, you can see the different types of details that are supported.

Roadmap

See the open issues for a list of proposed features (and known issues).

Support

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

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 to the project.
  • Write interesting articles about the project on Dev.to, Medium 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, and thank you for being involved!

Please do not forget that this project uses conventional commits, so please follow the specification in your commit messages. You can see valid types from this file.

Authors & contributors

The original setup of this repository is by Payadel.

For a full list of all authors and contributors, see the contributors page.

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.

License

This project is licensed under the GPLv3.

See LICENSE for more information.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc