473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Easy Validators


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')

Aug 20 '06 #1
0 1313

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML =...
1
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
0
by: Tom Pearson | last post by:
I create controls and validators dynamically dependent on data at runtime. I create the control then the relevant validator(s) for it assigning the Control.ID as the control to validate. These...
6
by: Mark | last post by:
We have Validators embedded in an asp table server control. The table server control is necessary and cannot be replaced. We want to apply CSS formatting to the validators, but the validators...
3
by: John Blair | last post by:
Hi, I have validators outside of a datagrid (for adding a new grid row) - however when i click "edit" column and then the "update" column of a grid row that has been edited - my other...
1
by: epigram | last post by:
I'm trying to use the ASP.NET validators to check some client-side business rules. I've got two ASP TextBox controls (call them tbxYear1 and tbxYear2) used to enter a range of years. I've got a...
1
by: Gabriel Lozano-Morán | last post by:
When using the tabstrip control combined with a multipage (several pageview) there is a problem when using validators. The problem is that validation also occurs on the validators that are not on...
2
by: Mike Surcouf | last post by:
Hi I have some regex validators on my page set to dynamic and like the way they appear after you tab out of a field and also when you try to postback the form. All OK so far When I register...
4
by: Madhur | last post by:
Hello All I am learning how to use ASP.NET Validators and would appreciate if someone could provide me with guidance. I have written very simple ASPX page below with a Dropdown list, a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.