The Pmw megawidgets toolkit for tkinter implements a mechanism for data validation. For example the background color of an entry field turns pink if you enter bad input. Although this module is old, it could be a good starting point for experimenting with data validation. You can create your own validators, and you can also browse the pmw source code to understand how it's implemented, which could give you ideas to develop your own system.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
the poorly coded "if x != 1 and x != 2 and x != 3"
Why yes, that is poorly coded. Good thing this is Python!
>>> x != 1 and x != 2 and x != 3
True
>>> x not in [1,2,3]
True
Data validation is extremely important for code that is distributable.
If you're writing scripts for you and only you that will never be used by anybody else than you can skip this; however when you have other users you should consider that they did not sit with you while you designed your program. They might completely miss the concept of what you're trying to achieve. If they provide improper input it is highly likely that your program will crash.
There's also going to be malicious users. These 1337 h4x0rs could intentionally try to either crash the system distributing your code or piggy back through your code into the guts of your system, causing all kinds of havoc.
Just my $0.02
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
When validating, you want to include, not exclude. So you want a list of data that is acceptable, and the input has to be in the list. That way, when another unit or type of data is introduced it is automatically an error. If your program excludes, then the new unit would be automatically included, since you did not explicitly exclude it, which is not a good idea.
Does anyone have a good synonym for exclude or include. This post could certainly use one.
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714