Skip to main content

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

Release files for onecondition 1.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for onecondition 1.1.15
File Size Uploaded
onecondition-1.1.15.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for onecondition 1.1.15
File Interpreter ABI Platform
onecondition-1.1.15-py3-none-any.whl Python 3 none any Details

Total release size: 38.3 kB

Release files / onecondition-1.1.15.tar.gz

Download URL onecondition-1.1.15.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9fe7ca8e9eb1be399ea7c8820afb896bfd0ad9778aeb893bcf8a95428cd24f34
BLAKE2b-256 checksum
How to use checksums
062459aa3de5faebd925f539b476ed329bc638a51ccd65729a82318629eea41d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / onecondition-1.1.15-py3-none-any.whl

Download URL onecondition-1.1.15-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73f8cceba75587321129dba146e626d838d5de480c628cdb500e5ed63488f1ef
BLAKE2b-256 checksum
How to use checksums
1e5046ea48b8b76c2331100ed4b7f048b099581621741354e11aaf305cb1b3f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

1.1.15 This release

2 release files

1.1.11

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.15

2 release files

1.0.14

2 release files

1.0.13

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page