<jupyter_start><jupyter_text>**Date, Regular Expressions, Validation**  **Date*** often need to manage date in programs* eg date of creation/update, loan/due date* common tasks   * generate current system date  * read date from keyboard / file  * date arithmetic  * write date to display / file<jupyter_code># datetime module has many useful functions to manipulate date and time, so use it.
import datetime
# current system date
current_date = datetime.date.today()
current_date
# current date is a datetime date object giving year, month and day eg datetime.date(2012, 2, 10)
type(current_date)
print(type(current_date.day))
current_date.month
current_date.year<jupyter_output><class 'int'><jupyter_text>**Formatting dates**<jupyter_code># return date as string
current_date.isoformat()

'2020-03-26' > '2020-04-01'
# conversion to different formats
print(current_date.strftime('%d-%m-%y'))
print(current_date.strftime('%d-%m-%Y'))
print(current_date.strftime('%d%m%Y'))
print(current_date.strftime('%Y%m%d'))
# days of week and month
print(current_date.strftime('%A %d %B %Y'))
print(current_date.strftime('%a %d %b %Y'))
# check for valid date
date = input("Enter date in DD-MM-YYYY: ")

try: # normal processing
  valid_date = datetime.datetime.strptime(date, "%d-%m-%Y") # p - parse
  print("date ok")
except ValueError: # if error happens
  print("Invalid date dude")<jupyter_output>Enter date in DD-MM-YYYY: 31-04-2020
Invalid date dude<jupyter_text>**Exercise**  Write a program to generate today's date and format the date in the following format:* DD-MM-YYYY* DDMMYY* YYMMDD*  day month year in full eg Saturday 12 March 2011*  day month year in short eg Sat 12 Mar 2011<jupyter_code><jupyter_output><empty_output><jupyter_text>**Validation**Workflow  * loop to get valid data* if user provides invalid data, prompt error message and allow re-entry* until user provides valid data  Example: Age validation  valid range for age 1 - 100 Pseudocode:  ``` set boolean valid_age to false loop until valid_age is true    get age    if empty input display empty input message and allow re-entry    if not a number display wrong data type and allow re-entry    if not within valid range display invalid range and allow re-entry    else set valid_age to true ```<jupyter_code># age validation
# set boolean valid_age to false
valid_age = False 
# loop until valid_age is true
while not valid_age: 
    # get age
    age = input("Enter age (1-100): ")
    # if empty input display empty input message and allow re-entry
    if len(age) == 0: # presence check
      print("Empty input. Try again.")
    # if not a number display wrong data type and allow re-entry
    elif not age.isdigit(): # data type check
      print("Age is a number. Try again.")
    # if not within valid range display invalid range and allow re-entry
    elif not (1 <= int(age) <= 100): # range check
      print("Are you human? Try again.")
    # else set valid_age to true 
    else:
      valid_age = True
print("Great job! You are", age)<jupyter_output>Enter age (1-100): 
Empty input. Try again.
Enter age (1-100): abc
Age is a number. Try again.
Enter age (1-100): 999
Are you human? Try again.
Enter age (1-100): 17
Great job! You are 17<jupyter_text>**Regular Expressions*** a powerful mechanism to check against patterns* used in data validation to avoid complicated (nested) if-else statements<jupyter_code># gender validation
import re

# get gender
gender = input("Enter gender: ")

# accept both upper and lowercase f and m
pattern = re.compile("[fFmM]")

if pattern.match(gender):
  print("Cool! You are not in between")
else:
  print("Oh. LGBT ftw.")
# NRIC validation
import re

# get nric
nric = input("Enter NRIC: ")

# ^ - starts with, {n} - exactly n times, $ - ends with 
pattern = re.compile("^[sStTfFgG][0-9]{7}[a-zA-Z]$")

if pattern.match(nric):
  print("Welcome to Singapore!")
else:
  print("Thank you very much.")<jupyter_output>Enter NRIC: A1234567H
Thank you very much.<jupyter_text>**NRIC validation (cont'd)**The check digit of a Singapore-based NRIC is actually derived from modulo arithmeticWrite a program to validate a user-input NRIC. Your program should contain* a function to return the check alphabet of a NRIC* a boolean function to determine if a user-input NRIC is validReference:http://coding-and-crypto.tripod.com/01NRIC.htm<jupyter_code><jupyter_output><empty_output><jupyter_text>**Modulo Arithmetic Validation**  Modulo arithmetic is used in many real-life situations:* Singapore NRIC* Singapore car plate registration number* book ISBN* credit card number* etc.You will be encountering more modulo arithmetic exercises (hint). **Exercise**  Write a program to get a student's nric, name, class, gender, date of birth, weight (in kg) and height (in metres). Your program should perform the necessary validation. Save your program to a file STUDENTS.DAT in append mode using the following structure:``````* class is in the form YC eg 20Y5C23* name is at most 30 characters* date of birth is in DD-MM-YYYY format sample record:```S1234567ALim Ah Seng ... M20Y5C35 701.73```<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1025491340727768, 'fraction_numerical': 0.02237789453201012, 'mean_word_length': 4.6670341786108045, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '472758', 'license': 'CC0-1.0', 'n_tokens_mistral': 1676, 'n_tokens_neox': 1540, 'n_words': 627, 'path': 'computing_sh/Python09_Date,_Regular_Expressions,_Validation.ipynb', 'repo_name': 'nadiaalee/cp4all', 'size': '5139'}