Please I need help with this program. I tried different approach, but doesn't seem to be getting no where.
QUESTION:
You've been going to work on a database project at work for sometime now. Your boss encourages you to program the database in Python. You disagree, arguing that Python is not a database language but your boss persists by providing the source code below for a sample telephone database. He asks you to do two things: 1. Evaluate the existing source code and extend it to make it useful for managers in the firm. (You do not need a GUI interface, just work on the database aspects: data entry and retrieval - of course you must get the program to run or properly work, and 2. He wants you to critically evaluate Python as a database tool.
import shelve
import string
def print_menu():
print 'Welcome to the Phone Record'
print '1. Add Name'
print '2. Delete Name'
print '3. Look Up Name'
print '9. Quit'
numbers = ()
menu_choice = 0
print_menu()
while menu_choice != 9:
menu_choice = input("Type in a number (1-9):")
if menu_choice == 1:
print "Add Number:"
Name = raw_input("Name:")
Work = raw_input("Work Number:")
Home = raw_input("Home Number:")
Fax = raw_input("Fax Number:")
Cell = raw_input("Cell NUmber:")
elif menu_choice == 2:
print "Delete Name;"
Name = raw_input("Name:")
elif menu_choice == 3:
print "Look Up Name;"
Name = raw_input ("Name:")
print "Name:" 'x' "\tNumber:", numbers[x]
print
UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4
class phoneentry:
def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN):
self.name = name
self.number = number
self.type = type
# create string representation
def __repr__(self):
return ('%s:%d' % (self.name, self.type))
# fuzzy compare or two items
def __cmp__(self, that):
this = string.lowe(str(self))
that = string.lower(that)
if string.find(this, that) >= 0:
return (0)
return (cmp(this, that))
def showtype(self):
if self.type == UNKNOWN: return ('Unknown')
if self.type == HOME: return ('Home')
if self.type == WORK: return ('Work')
if self.type == FAX: return ('Fax')
if self.type == CELL: return ('Cellular')
class phonedb:
def __int__(self, dbname = 'phonedata'):
self.dbname = dbname;
self.shelve = shelve.open(self.dbname);
def __del__(self):
self.shelve.close()
self.shelve = None
def add(self, name, number, type = HOME):
e = phoneentry(name, number, type)
def lookup(self, string):
list = []
for key in self.shelve.keys():
e = self.shelve[key]
if cmp(e, string) == 0:
list.append(e)
return (list)
# if not being loaded as a module, run a small test
if __name__ == '__main__':
foo = phonedb()
foo.add('Sean Reifschneider', '970-555-1111', HOME)
foo.add('Sean Reifschneider', '970-555-2222', FAX)
foo.add('Evelyn Mitchell', '970-555-1111', HOME)
print 'First lookup:'
for entry in foo.lookup('reifsch'):
print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
print
print 'Second lookup:'
for entry in foo.lookup('e'):
print '%-40 %s (%s)' % (entry.name, entry.number, entry.showtype())
I got stuck while running the program with the following error:
Welcome to the Phone Record
1. Add Name
2. Delete Name
3. Look Up Name
9. Quit
Type in a number (1-9):1
Add Number:
Name:Elizabeth
Work Number:718-555-1111
Home Number:718-555-2222
Fax Number:718-555-2223
Cell NUmber:718-555-1122
Type in a number (1-9):9
First lookup:
Traceback (most recent call last):
File "C:\Python25\phoneroster.py", line 95, in <module>
for entry in foo.lookup('reifsch'):
File "C:\Python25\phoneroster.py", line 80, in lookup
for key in self.shelve.keys():
AttributeError: phonedb instance has no attribute 'shelve'
>>>