Easy Validators

R

Ron Adam

Sometimes it's good to check things while working on them. This seems
like a good way to do that.

You could probably put these in a module and just import them.

from validators import *


I'm interested if anyone can think of ways to improve this further.


Each validator consists of an assertion with an error message if the
assertion fails.

The validator arguments can include an extra argument to check the
return value. Less than the number of arguments is ok. Any unspecified
arguments are not checked. Use Any to not check previous arguments in a
list. (see the examples at the end)

Cheers,
Ron



# ------------- Some Simple Validators.

def Any(arg): pass

def IsNumber(arg):
assert type(arg) in (int, long, float), \
"%r is not a number" % arg

def IsInt(arg):
assert type(arg) in (int, long), \
"%r is not an Int" % arg

def IsFloat(arg):
assert isinstance(arg, float), \
"%r is not a flaot" % arg

def IsLong(arg):
assert isinstance(arg, long), \
"%r is not a long integer" % arg

def IsString(arg):
assert type(arg) in (str, unicode), \
"%r is not a string type" % arg

def InRange(start, stop):
def inrange(arg):
assert start <= arg <= stop, \
"%r is not in range %r through %r" % (arg, start, stop)
return inrange

def InSet(list_):
s = set(list_)
def inset(arg):
assert arg in s, \
"%r is not in %r" % (arg, s)
return inset

def LessThan(value):
def lessthan(arg):
assert arg < value, \
"%r is not less than %r." % (arg, value)
return lessthan

def MoreThan(value):
def morethan(arg):
assert arg > value, \
"%r is not more than %r." % (arg, value)
return morethan

def IsEven(arg):
assert arg % 2 == 0, \
"%r is not even" % arg

def IsOdd(arg):
assert arg % 2 == 1, \
"%r is not odd" % arg

def IsPositive(arg):
assert arg >= 0, \
"%r is not positive" % arg

def IsNegative(arg):
assert arg < 0, \
"%r is not negative" % arg

def IsTrue(arg):
assert arg is True, \
"%r is not True" % arg

def IsFalse(arg):
assert arg is False, \
"%r is not False" % arg


# ----------------- The validator decorator.

def validate(*types):
""" check arguments + return value against types given.
"""
def check_accepts(f):
def new_f(*args, **kwds):
assert len(types) <= len(args)+1, \
"Validators exceed arg count + return value."
for (a, t) in zip(args, types):
t(a)
result = f(*args, **kwds)
if len(types)>len(args):
types[-1](result)
return result
new_f.func_name = f.func_name
return new_f
return check_accepts



# -------------- Examples to play around with.

@validate(Any, IsInt, IsEven)
def add(a, b):
return a + b

@validate(InRange(1,6), InRange(1,6), LessThan(19))
def mul(a, b):
return a * b

# they stack too
@validate(IsInt, IsInt)
@validate(MoreThan(10),LessThan(10))
@validate(Any, IsPositive)
def sub(a, b):
return a - b

@validate(IsString, IsString)
def addstr(a, b):
return a + b

print add(3.0, 5)
print mul(3, 6)
print sub(11, 9)
print addstr('Hello ', 'World')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top