Git Product home page Git Product logo

gofuncyourself's Introduction

gofuncyourself

Golang has this... interesting approach to exception handling where functions return a tuple of the actual result and a error (that may be nil).

This is a reimagining of the same concept applied to Python. It comes in form of a decorator:

from gofuncyourself import gofunc

@gofunc
def div(x, y):
    return x / y

If you now call div(16, 2) it will return (8, None), where None references the fact that no error has occurred.

If you instead do something like res, err = div(7, 0) then res will be None, and err will not; instead it will be a special error object that contains the exception that was caught in err.exception.

Functions decorated with gofunc are to be called like this:

res, err = div(x, y)
if err:
    ...  # handle exception

But wait, there's more!


What if you forget to check for an error? gofuncyourself protects you from that, by raising the exception one second later, if you didn't do if err:. The delay can be adjusted by passing a delay keyword argument to the decorator. For example, if you want to make sure the error is handled swiftly:

@gofunc(delay=0.1)  # only wait for 100ms before raising
def div(x, y):
    return x / y

Installation

pip install gofuncyourself

Acknowledgements

gofuncyourself's People

Contributors

l3viathan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.