[Tutor] data validation logic

Dave Angel davea at ieee.org
Sun Apr 17 12:46:11 CEST 2011


On 01/-10/-28163 02:59 PM, Rance Hall wrote:
> 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.
>
> <snip>
>
> 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.
>

Alan gave a suggestion for the first part of your approach. 
Essentially, generate a function object for each test you want to perform.

For the "return a pass if the data passes ALL..." part, you can use the 
all() builtin function.

You mention having an "associative array" (known in Python as a dict) 
containing the Trues and Falses for the corresponding tests.  To 
determine if all items in the dict are true, use

all(mydict.itervalues())

But, if you don't need to keep track of which test failed, why not just 
store the results in a list?  Then use
all(mylist)

That will return True if all the items in the list are true, and False 
if any of them is false.

DaveA


More information about the Tutor mailing list