Skip to main content

The module, which allows easily validate function's input/output values.

Project description

https://img.shields.io/codecov/c/github/uzumaxy/pyvalid.svg?style=plastic https://img.shields.io/github/workflow/status/uzumaxy/pyvalid/Python%20package?style=plastic

Python validation tool for checking function’s input parameters and return values. This module can be very helpful on the develop stage of the project, when control for accepted and returned function values is a one of most important things.

Module consists of two decorators: accepts and returns.

accepts(*accepted_arg_values, **accepted_kwargs_values)

A decorator for validating types and values of input parameters of a given function. You can pass the set of accepted types and values or validation function as decorator’s input parameters. Validation process can raise the following exceptions:

  • pyvalid.InvalidArgumentNumberError — when the number or position of arguments supplied to a function is incorrect.

  • pyvalid.ArgumentValidationError — when the type of an argument to a function is not what it should be.

returns(*accepted_returns_values)

A decorator for validating the return value of a given function. You can pass the set of accepted types and values or validation function as a decorator’s input parameters. Validation process can raise pyvalid.InvalidReturnType when the return value is not in the collection of supported values and types.

How to install

  • With PyPI: pip install -U pyvalid

  • Manually: python setup.py install

Example of usage

Function calc in example below has next limitations:

  • Can return only int or float value;

  • First parameter must be only of type str;

  • Second parameter must be of type int or equals to 2.0;

  • Third parameter must be of type int or float.

from pyvalid import accepts, returns


@returns(int, float)
@accepts(str, (int, 2.0), (int, float))
def calc(operator, val1, val2, val3):
    expression = '{v1} {op} {v2} {op} {v3}'.format(
        op=operator,
        v1=val1, v2=val2, v3=val3
    )
    return eval(expression)


# Output: 24.
print(calc('*', 2, 3, 4))

# Output: 24.0.
print(calc(operator='*', val1=2, val2=3.0, val3=4))

# Output: 24.0.
print(calc('*', 2.0, 3, 4))

# Raise pyvalid.ArgumentValidationError exception,
# because second argument has unsupported value.
print(calc('*', 3.14, 3, 4))


# Raise pyvalid.InvalidReturnType exception,
# because returns value is of type str.
print(calc('*', 2, 3, '"4"'))

Here is an example of usage pyvalid module in context of classes. Pay attention to the method connect of the class SqlDriver. This method is a good demonstration of usage accepts decorator for functions with keyword arguments.

from pyvalid import accepts, returns
from collections import Iterable


class SqlDriver(object):

    @returns(bool)
    @accepts(object, host=str, port=int, usr=str, pwd=str, db=[str, None])
    def connect(self, **kwargs):
        connection_string = \
            'tsql -S {host} -p {port} -U {usr} -P {pwd} -D {db}'.format(**kwargs)
        try:
            print('Establishing connection: "{}"'.format(connection_string))
            # Create connection..
            success = True
        except:
            success = False
        return success

    @returns(bool)
    def close(self):
        try:
            print('Closing connection')
            # Close connection..
            success = True
        except:
            success = False
        return success

    @returns(None, dict)
    @accepts(object, str, Iterable)
    def query(self, sql, params=None):
        try:
            query_info = 'Processing request "{}"'.format(sql)
            if params is not None:
                query_info += ' with following params: ' + ', '.join(params)
            print(query_info)
            # Process request..
            data = dict()
        except:
            data = None
        return data


sql_driver = SqlDriver()

conn_params = {
    'host': '8.8.8.8',
    'port': 1433,
    'usr': 'admin',
    'pwd': 'Super_Mega_Strong_Password_2000',
    'db': 'info_tech'
}
sql_driver.connect(**conn_params)

sql = r'SELECT * FROM ProgrammingLang'
pl = sql_driver.query(sql)

sql = r'SELECT * FROM ProgrammingLang WHERE name=?'
python_pl = sql_driver.query(sql, ('Python',))

sql_driver.close()

Following example with class User will show you how to use pyvalid module to validate some value with using validation function.

from pyvalid import accepts
from pyvalid.validators import is_validator


class User(object):

    class Validator(object):

        unsafe_passwords = [
            '111111', '000000', '123123',
            '123456', '12345678', '1234567890',
            'qwerty', 'sunshine', 'password',
        ]

        @classmethod
        @is_validator
        def login_checker(cls, login):
            is_valid = isinstance(login, str) and 1 <= len(login) <= 16
            if is_valid:
                for reg_user in User.registered:
                    if login == reg_user.login:
                        is_valid = False
                        break
            return is_valid

        @classmethod
        @is_validator
        def password_checker(cls, password):
            is_valid = isinstance(password, str) and \
                (6 <= len(password) <= 32) and \
                (password not in cls.unsafe_passwords)
            return is_valid

    registered = list()

    def __init__(self, login, password):
        self.__login = None
        self.login = login
        self.__password = None
        self.password = password
        User.registered.append(self)

    @property
    def login(self):
        return self.__login

    @login.setter
    @accepts(object, Validator.login_checker)
    def login(self, value):
        self.__login = value

    @property
    def password(self):
        return self.__password

    @password.setter
    @accepts(object, Validator.password_checker)
    def password(self, value):
        self.__password = value


user = User('admin', 'Super_Mega_Strong_Password_2000')

# Output: admin Super_Mega_Strong_Password_2000
print(user.login, user.password)

# Raise pyvalid.ArgumentValidationError exception,
# because User.Validator.password_checker method
# returns False value.
user.password = 'qwerty'

# Raise pyvalid.ArgumentValidationError exception,
# because User.Validator.login_checker method
# returns False value.
user = User('admin', 'Super_Mega_Strong_Password_2001')

License

Note that this project is distributed under the MIT License.

Download files

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

Source Distribution

pyvalid-0.9.5.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyvalid-0.9.5-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file pyvalid-0.9.5.tar.gz.

File metadata

  • Download URL: pyvalid-0.9.5.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for pyvalid-0.9.5.tar.gz
Algorithm Hash digest
SHA256 df5656a90e899926925e8e1a753788c58f7e61c99688b1484562996cfaae6601
MD5 14ffd3983c675efe4cd7ff3bff46ef43
BLAKE2b-256 d429e812d55ebfae55bb17c4500187c7962a84a4f3f4627597f81ef8aa61d36a

See more details on using hashes here.

File details

Details for the file pyvalid-0.9.5-py3-none-any.whl.

File metadata

  • Download URL: pyvalid-0.9.5-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.5

File hashes

Hashes for pyvalid-0.9.5-py3-none-any.whl
Algorithm Hash digest
SHA256 16bb9907e083c28f86ee975198b29964b2c4642707dc978104c3a2ceace887c8
MD5 83a71596d296797124c5ee68590dc7b6
BLAKE2b-256 dfc29e8edfb360758ef87a1cfbab8913314a4fd2f2d7fdb869a98540aa2b65b4

See more details on using hashes here.

Supported by

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