In this section, we will learn how to use validation and verification routines to help prevent syntax and semantic errors in our code. We are learning ...
So that we can ...
A program can be described as robust if it is difficult to cause it to crash by entering incorrect data. Robust programs rely on validation, or input sanitation, to make sure that the data entered is present, of the correct type, length or range. Validation ensures that data is sensible, even reasonable, but not accurate. Verification ensures that the data looks sensible by either asking for it twice or manually proofreading it. Where we learn about the difference between validation and verification Visit the BBC Education page for Data Validation and Verification. This is an GCSE ICT topic but it covers all that you need to know about the theory of these two essential pathways to robustness. Make sure that you run through the 'revision' lesson and try the quick test at the end. When you have done, you should try this ... Make some notes on validation and verification, specifically, how they are different. If you want to use some of the content from the BBC learning page, feel free but don't just copy and paste - you can use the screen clipping tool to cut down your work. INPUT SANITISATION TECHNIQUES HERE removing control characters, non-ascii characters use of the lstrip(), rstrip() and strip() methods messy = input('Give me a messy string : ') print('Wow! That is a messy string ...\n') messyencoded = bytes(messy,"utf-8").decode("unicode_escape") print(messyencoded) print('\nLet\'s clean this up!') print('Original String : "',messy,'"',sep='') print('Strip leading : "',str(bytes(messyencoded.lstrip(),"utf-8"))[2:-1],'"',sep='') print('Strip trailing : "',str(bytes(messyencoded.rstrip(),"utf-8"))[2:-1],'"',sep='') print('Strip both : "',str(bytes(messyencoded.strip(), "utf-8"))[2:-1],'"',sep='') print('Only ASCII : "',''.join([i for i in messyencoded if 32<=ord(i)<=127]),'"',sep='') print('Lower case : "',str(bytes(messyencoded, "utf-8"))[2:-1].lower(),'"',sep='') print('Only ASCII + strip : "',''.join([i for i in messyencoded if 32<=ord(i)<=127]).strip(),'"',sep='') print('Lower, ASCII + strip : "',''.join([i for i in messyencoded if 32<=ord(i)<=127]).strip().lower(),'"',sep='')
Again, there is no substitute for trying out some validation functions. Make sure that you engage with the following exercises. Ideally, these should be written as functions but, to keep things simple, I've given you them as simple script snippets. I have provided links to the function based versions in the extension activity. Just like most things to do with programming, the only way you learn about validation is to do some! The following tasks require you to code lots of simple scripts - there is a focus on testing in this exercise. You should confidently choose suitable testing values to make sure that the scripts work. Where we learn how to carry out a practical presence check in Python A presence check makes sure that you actually type something - that the input is actually 'present'. Enter the following in a new script. Save the script and run it.
value = input('Enter a value : ') if value == '': print('Validation error - please enter a value!') else: valid = TrueYou will be presented with a prompt. Test the script by pressing the ENTER key - you should be taken back to the prompt after a suitability derisive message. Try entering a value and pressing the ENTER key. Then ... >>> print(value) # comment at the top of the script and print it out for your folder. Write down, in your own words ...
Where we learn how to perform a practical type check using Python Type checks make sure that the data you enter is present and of the correct data type. There are two which are relatively easy to do using Python ...
ALPHA TYPE CHECK Enter the following in a new script. Save the script and run it. not valid: value = input('Enter a letter : ') if value == '': print('Validation error - please enter a value!') elif value.isalpha() == False: print('Validation error - please enter only LETTERS!') else: # Must be OK! valid = TrueAgain, you will be presented with a prompt. Try testing the script by typing in a number and pressing the ENTER key. You can carry on doing this forever, until you enter a letter. Try typing a letter (lower or uppercase) and press the ENTER key. Then ... >>> print(value)
DIGIT TYPE CHECK Enter the following in a new script. Save the script and run it. not valid: value = input('Enter an letter : ') if value == '': print('Validation error - please enter a value!') elif value.isdigit() == False: print('Validation error - please enter only NUMBERS!') else: # Must be OK! valid = TrueAgain, you will be presented with a prompt. Try testing the script by typing in a letter and pressing the ENTER key. You can carry on doing this forever, until you enter a number. Try typing a number and press the ENTER key. Then ... >>> print(value)
Where we learn how to perform a practical length check using Python A length check ensures that the input is the required number of characters long. There are three standard length checks that you can perform ...
MINIMUM LENGTH CHECK Type the following snippet in a new Python script. Save it, run it and test it.
while not valid: length = 5 value = input('Enter password (minimum {} characters) : '.format(length)) if value == '': print('Validation error - please enter something!') elif len(value) < length: print('Validation error - minimum characters {}. Try again.'.format(length)) else: valid = True
MAXIMUM LENGTH CHECK Type the following snippet in a new Python script. Save it, run it and test it.
while not valid: length = 5 value = input('Enter password (maximum {} characters) : '.format(length)) if value == '': print('Validation error - please enter something!') elif len(value) > length: print('Validation error - maximum characters {}. Try again.'.format(length)) else: valid = True
RANGE LENGTH CHECK Type the following snippet in a new Python script. Save it, run it and test it.
while not valid: minimum = 5 maximum = 10 if value == '': print('Validation error - please enter something!') elif (len(value) < minimum) or (len(value) > maximum): print('Validation error - expect {0}-{1} characters. Try again.'.format(minimum,maximum)) else: valid = True
Where we learn how to perform a practical range check using Python A range check is different to a length check because it checks the numerical range of a number is valid. This example allows you to specify a range but you could easily change this so that it only checked for a minimum or a maximum limit. More often than not, you need both - if you want a minimum only, set the maximum to 999 or something and if you want a maximum, set the minimum at 0 (or whatever, mate). Enter the following in a new script. Save the script, run it and test it.
while not valid: minimum = 3 maximum = 6 value = input('Enter a number ({0}-{1})'.format(minimum,maximum)) if value == '': print('Validation error - please enter something!') elif (float(value) < minimum) or (float(value) > maximum): print('Validation error - expected {0}-{1}. Try again.'.format(minimum,maximum)) else: valid = True
Where we learn how to perform a practical sequence check using Python Provide your script with a sequence of values and a sequence check can check that the value you entered is in the list or not. Again, this script also performs a presence check (which they all do). Enter the following in a new script. Save the script, run it and test it.
while not valid: sequence = ['1','2','3','4','5'] # Must be strings! value = input('Enter a valid value ({0}) : '.format(','.join(sequence))) if value == '': print('Validation error - please enter something!') elif value not in sequence: print('Validation error - choose from \'{0}\''.format(','.join(sequence))) else: valid = True
Where we learn how to match up a particular validation check to a particular scenario That's what you reckon! If your teacher has not provided you with a copy of Validation match up, download yourself a copy now, print it out (single sided!) and follow the instructions. You will be expected to code as many validation routines as you can because that's the only way you can demonstrate your understanding!
Where we learn how to program a practical exception handling routine using Python The following simple Python snippets are worth nothing outside a full program. Remember, these are techniques! Open up your favourite Python programming environment and enter the following simple script. You don't need to enter the comments. Save it, run it and test it thoroughly.
while not valid: try: number = int(input('Enter a number : ')) # This could cause an error ... valid = True # ... but it didn't! except: print('That\'s not a number - try again!') What happens when you try ... - a letter - a float - a boolean (True / False) - a word - a whole number Make sure you have printed out this script and described the results of the 5 tests you carried out. A (ridiculously easy) challenge Write a simple exception block which will accept a float. Hint : Use float() instead of int()
Yup! Your challenge, if you choose to accept it, is to create a validation routine to accept a valid date string in the form dd/mm/yy. Don't worry about leap years (oh, really!), but your validation routine must only allow valid months and only allow the correct number of days in each month. HINTS ...
If you give up, ask your teacher for a copy of their solution. In order to deepen your understanding of the way in which the script works, you should create a flowchart of it.
Task 4.1 Verification check Where we learn how to perform a practical verification check using Python If the data that is entered into a computer system matches the original source of the data then it is said to be verified and the techniques used to ensure this are called verification. Verification can be performed by proof reading / checking the input manually or using a procedure called double entry because you enter the same piece of data twice and the computer checks whether it is the same. This doesn't confirm that the data is actually correct, only that the likelihood is that you didn't make any errors when you typed it in and that it matches the original source of the data. Open up your favourite Python programming environment and type the following simple script. Save it, run it and test it thoroughly.
while not valid: value = input('Enter email address : ') if value == '': print('Please enter your email address - try again!') else: check = input('Enter your email address again : ') if value != check: print('Sorry - both values must be the same. Start again!') else: valid = TrueAgain, thoroughly test this code snippet using 4 tests. Try ... - Not entering any email address - Entering the first one and leaving the second one blank - Entering the first one and a slightly different second one - Entering them both exactly the same Make sure you have a copy of the script for your notes. Describe how it works and make sure you give details of the 4 tests you have performed to investigate its operation. Task 4.2 Email verification Where we how email verification works and practice drawing a flowchart. Often, when you sign up for a new web account, the website uses a technique called email verification to send you a email containing a unique, time-limited token in order to check that the email address exists and that you are happy to continue using it to sign up to the website ... ... and the email contains a link which you click which contains the unique token generated at the time you signed up. If, for some reason, you type in the wrong verification token (why would you?) or you don't verify your email within a certain time, the code expires and you have to ask for a new verification email to be sent. Firstly, copy up the algorithm into your notebooks. Next, create a flowchart on paper, using yEd Live or an alternative flow diagram editor (not Flowgorithm - this isn't really a program) which explains the method of email verification. My flowchart is based on the following algorithm ... 01 START 02 GET email address 03 WHILE email not verified 04 GENERATE unique token 05 SEND verification email 06 WAIT for user to click link 07 IF token is not valid 08 DISPLAY error message 09 ELSE IF token has expired 10 DISPLAY error message 11 ELSE 12 email verified 13 DISPLAY confirmation message 14 STOP HINT : Line 03 WHILE email not verified is a 'decision' symbol. The 'loop' simply leads back to line 04 .Your teacher will give you a copy of a worksheet which contains my solution. Complete the worksheet and then compare it to your solution. Does it look similar?
Click to load key word list to help you make your own flash cards
There is one more validation check that we've met before in the filehandling section - using the os library to check whether a file exists. Look back at the scripts for this section to refresh your memory!FAQ Q : What other things can you use for a type check? A : In Python, you can easily check for digits using variable.isdigit() , letters using variable.isalpha() , and both letters and numbers using variable.isalnum() . Checking for other variable types is a bit trickier ...Q : How do you set up a practical email validation routine for a website? A : This is a bit tricky - you would need to learn to write webpages with HTML and CSS, client side scripting with Javascript and serverside scripting with PHP. There are lots of tutorials on the web which could help you. Q : Are there other methods of verification? A : The two main methods are email verification and double entry which we've looked at. Q : Is this a popular topic on the exam? A : It may come up on the exam but it is more important for programming tasks. Q : Do you need to know regular expressions at GCSE level? A : No, not for the examination but it doesn't do any harm to learn about it! |