Saturday, April 14, 2018

Data Verification

Now that we have uploaded our file, we need to verify that the data we have is valid. To check that the information for each claim is complete, we verify the following:
  • That f_name, l_name, and email are not null
  • That claim_num is a 5 digit number
  • That phone_num is in the format ###-###-####
For each claim that contains errors, we will print out an error indicating what information is missing or invalid. If the data is all valid for a claim, we will state that the data is valid.

Here is the data we are reading from:



Here's the code:


And here is the output:


Notes on the code:
Before going on to read the rest of this post, I encourage you to read through the code provided above. Once you have done that, here are some observations:

  • Notice that this code is written using methods. We have a main() method where the program starts, and one additional method called check_phone_validity(). I wrote this method, because the checking of a valid phone number requires more code that a simple conditional statement. I wrote all the other checks in the main() method because they were simple and short enough to handle there. I could have written methods for each check case, but it was not necessary and is really a matter of preference. Note that this code could also have been written without using any methods at all. This is a great example of Python's flexibility and ease of use!
  • Since f_name, l_name, and email are strings, to check if they are null, we compare them to "". If we were working with integers, we would compare them to None. This is a distinction I came across and learned about as I was writing the code.
  • We use the boolean variable 'clean' to mark if the row of data being checked has any deficiencies, and if it does, clean is set to false, otherwise, the claim is reported to have valid data. In my next blog post, I will add a line at the very end of the method, under the last conditional, so that if clean is True, the program calls a method to send a formatted email to the claimant with the information for that claim. 
  • The check_phone_validity() method first checks that the input has 12 characters. Then it goes on to make sure that in positions 3 and 7 in the string, there must be a dash, in keeping with the format requirements. We return whether an invalid phone number affects the validity of the data.
As always, feel free to comment or ask questions in the comments section below!

Sources:

2 comments:

  1. Are you able to check for a null String by using 'is null' to compare instead of the double quotes? Also, would it be beneficial to add a method for checking that the email address is in the proper name@domain.com format?

    ReplyDelete
    Replies
    1. To check for a 'null' string, I do want to use the double quotes. There is an equivalent to 'null' in Python, and it is the word 'None.' This is something I ran into when I was coding. The familiar word 'null' is surprisingly not part of the Python vocabulary. But 'None' specifically refers to objects, not strings. Consider the following code:

      name = 'Sally'
      name = ""
      if name is None:
      print('name is null')

      When this code is run, nothing prints! That is because "" and None are not the same.

      As for your second question, checking email validity is an excellent suggestion, but I did the phone number check instead to show the same concept. Checking for a correct email format would be a little bit more complicated, and I don't plan on getting into that. If it came to the point where there was an invalid email, the program, as it is written in my next post, would not be able to send the email, and would error out in that phase, so we would know that something is wrong.

      Delete