Giter Club home page Giter Club logo

nah's Introduction

nah PyPI version

Build Status

A tiny collection of simple error handling decorators.

Usage

Ignoring exception

import nah

@nah.meh
def check_age(age):
    if age < 18:
        raise UnderAgeException

check_age(17)  # returns None

Returning a value

import nah

@nah.gimme(0)
def divide_by(n):
    return 100 / n

divide_by(0) # returns 0 instead of raising ZeroDivisionError

Retry

@nah.again(3)
def connect_db(config):
    return DbConnection.connect(config)

Alternative function

def alternative_func(a, b):
    return safe_divide(a, b)

@nah.maybe(alternative_func)
def my_func(a, b):
    return a / b

Other options

@nah.meh(when=ValueError) # filter ignored exception
@nah.meh(when=(ValueError, KeyError))
@nah.gimme(when=ValueError)
@nah.gimme(when=(ValueError, KeyError))
@nah.again(3, when=ValueError)
@nah.again(3, when=(ValueError, KeyError))
@nah.again(3, backoff=2.0) # back-off for 2 seconds
@nah.maybe(alternative_fn) # delegate the call to function `alternative_fn`
@nah.maybe(alternative_fn, when=ValueError)
@nah.maybe(alternative_fn, when=(ValueError, KeyError))
def my_func():
    ...

Copyright

nah is an open source project by me. See the license file for more information.

nah's People

Contributors

ocxyz avatar

Watchers

 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.