#!/usr/bin/python
###########################################################
#This is telephone book software developed by Chiru Toleti#
###########################################################
import sys,os,re
def print_phone_book(book):
"""To display all the numbers in a formatted manner"""
print ''.center(40,'~')
print "NAME".ljust(15)," ---> ","PHONE NUMBER".ljust(13)
print ''.center(40,'~')
for key in sorted(book,key=str.lower):
print key.ljust(15)," ---> ",book[key].ljust(15)
print "END OF THE BOOK".center(40,'~')
def load_nos_into_book():
"""Loads all the numbers into python dictionary(book)"""
global book
in_file=open('/home/ctoleti/File-System/Python/nos.txt','r')
for each in open('/home/ctoleti/File-System/Python/nos.txt','r'):#.xreadlines():
if each is "":
break
else:
name,no=each.strip().split(':')
book[name]=no
def add_no_to_book():
"""To add number to the book dictionary and file"""
global book
name=no=""
name=raw_input("Enter name : ")
no=raw_input("Enter phone number : ")
print "NAME : ",name
if name=="" or no=="":
print "ER : You did not enter name or number"
elif name !="" or no != "":
book[name]=no
out_file=open('/home/ctoleti/File-System/Python/nos.txt','a')
out_file.write(name+":"+no+"\n")
print "Added".center(40,'~')
def search_for_no():
"""To search- takes partial/full name/number and returns"""
global book
to_search=raw_input("Enter name/number to search :")
flag=0
if to_search !="":
for each in book.keys():
if re.search(to_search,each):# in for each in book.values()):
print each.ljust(15)," ---> ",book[each].ljust(15)
flag=1
for each in book.values():
if re.search(to_search,each):# in for each in book.values()):
for key in book.keys():
if book[key]==each:
print key.ljust(15)," ---> ",book[key].ljust(15)
flag=1
if to_search in book.keys():
print to_search.ljust(15)," ---> ",book[to_search].ljust(15)
flag=1
elif to_search in book.values():
for each in book.keys():
if book[each]==to_search:
print each.ljust(15)," ---> ",book[each].ljust(15)
flag=1
else:
print "ER : Did not enter anything."
if flag==1:
print "SEARCHING DONE".center(40,'~')
else:
print "NOT FOUND".center(40,'~')
def menu():
"""Menu to take input from user"""
print "MENU".center(40,'~')
choice=raw_input("1 -> PRINT PHONE BOOK\n2 -> ADD A NEW NUMBER TO PHONE BOOK\n3 -> SEARCH FOR A PHONE NUMBER\n4 -> EXIT\nWhat is your choice ? :")
if choice=="1":
print_phone_book(book);
elif choice=="2":
add_no_to_book();
elif choice=="3":
search_for_no();
elif choice=="4":
sys.exit()
else:
print "ER : Invalid Choice\nTake another chance <>"
book={}
load_nos_into_book();
os.system('clear')
while True:
raw_input("")
os.system('clear')
print "_/\_".center(40,'~')
print "TELE PHONE BOOK SOFTWARE".center(40)
menu();
###########################################################
#This is telephone book software developed by Chiru Toleti#
###########################################################
import sys,os,re
def print_phone_book(book):
"""To display all the numbers in a formatted manner"""
print ''.center(40,'~')
print "NAME".ljust(15)," ---> ","PHONE NUMBER".ljust(13)
print ''.center(40,'~')
for key in sorted(book,key=str.lower):
print key.ljust(15)," ---> ",book[key].ljust(15)
print "END OF THE BOOK".center(40,'~')
def load_nos_into_book():
"""Loads all the numbers into python dictionary(book)"""
global book
in_file=open('/home/ctoleti/File-System/Python/nos.txt','r')
for each in open('/home/ctoleti/File-System/Python/nos.txt','r'):#.xreadlines():
if each is "":
break
else:
name,no=each.strip().split(':')
book[name]=no
def add_no_to_book():
"""To add number to the book dictionary and file"""
global book
name=no=""
name=raw_input("Enter name : ")
no=raw_input("Enter phone number : ")
print "NAME : ",name
if name=="" or no=="":
print "ER : You did not enter name or number"
elif name !="" or no != "":
book[name]=no
out_file=open('/home/ctoleti/File-System/Python/nos.txt','a')
out_file.write(name+":"+no+"\n")
print "Added".center(40,'~')
def search_for_no():
"""To search- takes partial/full name/number and returns"""
global book
to_search=raw_input("Enter name/number to search :")
flag=0
if to_search !="":
for each in book.keys():
if re.search(to_search,each):# in for each in book.values()):
print each.ljust(15)," ---> ",book[each].ljust(15)
flag=1
for each in book.values():
if re.search(to_search,each):# in for each in book.values()):
for key in book.keys():
if book[key]==each:
print key.ljust(15)," ---> ",book[key].ljust(15)
flag=1
if to_search in book.keys():
print to_search.ljust(15)," ---> ",book[to_search].ljust(15)
flag=1
elif to_search in book.values():
for each in book.keys():
if book[each]==to_search:
print each.ljust(15)," ---> ",book[each].ljust(15)
flag=1
else:
print "ER : Did not enter anything."
if flag==1:
print "SEARCHING DONE".center(40,'~')
else:
print "NOT FOUND".center(40,'~')
def menu():
"""Menu to take input from user"""
print "MENU".center(40,'~')
choice=raw_input("1 -> PRINT PHONE BOOK\n2 -> ADD A NEW NUMBER TO PHONE BOOK\n3 -> SEARCH FOR A PHONE NUMBER\n4 -> EXIT\nWhat is your choice ? :")
if choice=="1":
print_phone_book(book);
elif choice=="2":
add_no_to_book();
elif choice=="3":
search_for_no();
elif choice=="4":
sys.exit()
else:
print "ER : Invalid Choice\nTake another chance <>"
book={}
load_nos_into_book();
os.system('clear')
while True:
raw_input("")
os.system('clear')
print "_/\_".center(40,'~')
print "TELE PHONE BOOK SOFTWARE".center(40)
menu();