Skip to main content

Guard clause library for Python projects, to validate arguments on every python function/method.

Project description

CheckArg

Guard clause library for Python projects, to validate arguments on every python function/method.

Installation

You can install the latest version of this software from the Python package index (PyPI) as follows:

pip install --upgrade checkarg

Usage

Using CheckArg to validate numbers

In the following example we want to guarantee the first argument is a negative number and the second argument has a positive value or zero:

import checkarg.number as Number

def doSomethingValid(negative_number: int, positive_number: int):
    Number.is_lower(negative_number, 0)
    Number.is_greater_or_equals(positive_number, 0)

    return negative_number, positive_number

Using CheckArg to validate text

The following example requires the string of the first argument has some content, if it is None or empty or whith whitespaces, it will rise an exception. The second argument only requires to not be None or an empty message:

import checkarg.text as Text

def doSomethingValid(title: str, body: str):
    Text.is_not_whitespace(title)
    Text.is_not_empty(body)

    return title, body

Controlling the flow with the exceptions

Whenever the CheckArg detects something wrong it will raise different exceptions by the context. This is an example controling the flow execution:

import checkarg.none_type as NoneType
import checkarg.number as Number
import checkarg.text as Text

from checkarg.exceptions import ArgumentNoneException, ArgumentException, ArgumentOutOfRangeException


def lookup_name(mapping, key: str, default: int):
    try:
        Number.is_greater(default, 0)
    except ArgumentOutOfRangeException:
        return None

    try:
        NoneType.is_not_none(mapping)
    except ArgumentNoneException:
        return default

    try:
        Text.is_not_empty(key)
    except (ArgumentException, ArgumentNoneException) as e:
        return default

    return mapping[key]

Contributing

Read the Contributing guidelines

Disclaimer

By sending us your contributions, you are agreeing that your contribution is made subject to the terms of our Contributor Ownership Statement

Maintainers

List of Maintainers

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

checkarg-0.0.2.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

checkarg-0.0.2-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page