Coder Social home page Coder Social logo

vsevolod-fedorov / pyvalid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from eterey/pyvalid

0.0 2.0 0.0 115 KB

:white_check_mark: Python values validator

Home Page: http://uzumaxy.github.io/pyvalid/

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

pyvalid's Introduction

pyvalid

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.

pyvalid's People

Contributors

eterey avatar vsevolod-fedorov avatar

Watchers

James Cloos avatar  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.