[Tutor] data validation logic

Rance Hall ranceh at gmail.com
Sun Apr 17 01:49:18 CEST 2011


Hey gang:

I need some help trying to pythonize (sp?, if this is even a word?) an idea.

I'd like to define a datavalidation function that returns true if data
is valid, and false if it isn't.


Here is the clincher.  the logic of the data validator needs to be
able to handle different types of data testing on request.


So here is my idea:

Define a function that can accept multiple inputs.  One being the data
element to validate, and the additional arguments, which can be null
would define the validation needing to be done.

For example, if a match value, or set of match values is defined,
check the data element to ensure it matches one of the match values.

I'd also like to be able to feed this data validator a list of names
of tests to check (numeric, alpha-numeric, maxlength=10, etc.) each of
these names would be the names of the various sub tests the validator
can perform

eventually I'll probably convert this to a class, but I really don't
have a knack for OOP yet. (I'm still learning that.)

Anyway so here is the problem I see with this idea and can't quite
figure out how to handle.

I need a way to store the pass fail values of each of the individual
tests, and then return a pass if the data passes ALL tests no matter
how many individual tests might be executed.

In my mind an associative array is the best way to track this.  It can
be dynamically created with each call of the function and the array
elements can be named for each test ran.

The problem is the final test of pass/fail based on the pass/fail
results of individual tests.

It might be as simple as looping through the array elements and if any
value is false break out of the loop and return false.  else if all
are true return true.

Would someone be kind enough to help me work out the finer details of
such an approach.

Perhaps lists might be better for this sort of thing?, perhaps I have
screwed up my logic somewhere?

Thanks for any hints you can provide.  PS this is on python 3.x.

Rance


More information about the Tutor mailing list