Git Product home page Git Product logo

python-onecondition's Introduction

1️⃣ OneCondition

An ultra-lightweight package for validating single conditions.

Python Version PyPI Version

GitHub Build Codecov Coverage Codacy Badge

License PyPI Downloads

Why?

Often when writing Python code, you will need to validate one or more conditions about a value, and raise an error if those conditions aren't valid. The most simple example is as follows:

def inverse(value):
    # Validate that the input is a positive number
    if not isinstance(value, (int, float)):
        raise ValueError("Value must be either an int or a float")
    if not value > 0:
        raise ValueError("Value must be positive (non-zero)")
    
    return 1 / value

This works for very simple cases, but what if we wanted to format more information into the error messages? What if we want a custom error type to represent specifically a validation error? What if we need to do 20, or 200 validations, instead of 2? You can start to see how quickly your boilerplate for validation could clutter up your code.

onecondition aims to solve this issue with a simple design philosophy:

You should only have to write one line of code in order to validate one condition.

(And that line should be less than 100 characters long.)

Usage

The most common usage of onecondition is to validate that the parameters passed to a function are valid. The validate submodule provides a large number of functions that allow validating many aspects about a value.

import onecondition as oc
import onecondition.validate

def inverse(value):
    # Validate that the input is a positive number
    oc.validate.instance(value, (int, float))
    oc.validate.positive(value)
    
    return 1 / value

Now, if you run something like inverse(4), you will get the expected output of 0.25.

However, running inverse(0) would give the following error:

Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `0` must be positive (non-zero)

Similarly, running inverse("foobar") would result in the following:

Traceback (most recent call last):
    ...
onecondition.ValidationError: Value `'foobar'` must be an instance of (<class 'int'>, <class 'float'>), not a <class 'str'>

Isn't that much nicer than writing all that code yourself?

Full Documentation

onecondition on Read the Docs

python-onecondition's People

Contributors

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