"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

October 25, 2019

Day #289 - Example code for Class Creation, Data Persistence, Email and Phone number Validation

Example code for Class Creation, Data Persistence, Email and Phone number validators

#pip install phonenumbers
#pip install validate_email
import phonenumbers
from phonenumbers import carrier
from phonenumbers.phonenumberutil import number_type
from validate_email import validate_email
import pickle
userdata = r'E:\Code_Repo\messaging\phonename.dictionary'
useremail = r'E:\Code_Repo\messaging\phoneemail.dictionary'
phonename = {}
phoneemail = {}
class Person:
def __init__(self,name,phonenumber,emailaddress):
#Constructor
self.name = name
self.phonenumber = phonenumber
self.emailaddress = emailaddress
def validate_phonenumber(self):
#Phone number validation
status = carrier._is_mobile(number_type(phonenumbers.parse(self.phonenumber)))
return status
def validate_email_addr(self):
#Email validation
is_valid = validate_email(self.emailaddress)
return is_valid
def Save_data():
guestuser1 =Person('Raj','+91-7406947660','siva@siva.com')
guestuser2 =Person('Raja','+91-7406947760','siva1@siva.com')
print(guestuser1.validate_email_addr())
print(guestuser1.validate_phonenumber())
#Save data
phonename[guestuser1.phonenumber] = guestuser1.name
phoneemail[guestuser1.emailaddress] = guestuser1.emailaddress
phonename[guestuser2.phonenumber] = guestuser2.name
phoneemail[guestuser2.emailaddress] = guestuser2.emailaddress
#Persist data
with open(userdata, 'wb') as config_dictionary_file:
pickle.dump(phonename, config_dictionary_file)
with open(useremail, 'wb') as config_dictionary_file:
pickle.dump(phoneemail, config_dictionary_file)
def Read_data():
#read data
with open(userdata, 'rb') as config_dictionary_file:
userdatavalues = pickle.load(config_dictionary_file)
for key,datavalues in userdatavalues.items():
print(key)
print(datavalues)
with open(useremail, 'rb') as config_dictionary_file:
useremailvalues = pickle.load(config_dictionary_file)
for key,datavalues in useremailvalues.items():
print(key)
print(datavalues)
Save_data()
Read_data()
Happy Learning!!!

No comments: