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'
>>>

What is the original code and what have you added to it? The way it is now, it does not make much sense.

Here is the original code

import shelve
import string
 
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)
        self.shelve[str(e)] = e
 
    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())

And I was trying to create phone record through data input. So, this is what I added:

import shelve, _bsddb
filename = 'C:/Pyhton25/phonerecord.py'
 
def dele(name, ptype):
    key = name + ':' + ptype
    db = shelve.open(filename)
    try:
        try:
            del db[key]
            print 'Entry deleted.'
        except _bsddb.DBNotFoundError:
            print 'No Entries with', key, 'exist!'
    finally:
        db.close()
 
b = 1
foo = phonedb()
while b != 4:
    print
    print "Welcome to the Phone Database"
    print "Please choose from the following"
    print
    print "If you would like to display Phone Number(s) select 1"
    print
    print "If you would like to add an entry select 2"
    print
    print "If you would like to delete for an entry select 3"
    print
    print "To quit select 4 "
 
    b = input(':')
    if b == 1:
        print 'Enter the lookup up key to find (For example for all names with ''Rob'' in them type Rob)'
        print
        a = raw_input(':')
 
        for entry in foo.lookup(a):
            print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() )
            print
 
if b == 2:
    print "Please enter the full name: (Example: John Smith)"
    n = raw_input(':')
    print "Please enter the phone number: (Example: 970-432-5432)"
    p = raw_input(':')
    print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work, 3 = Fax, 4 = Cell)"
 
    t = raw_input(':')
    if t == '0':
        foo.add(n, p, UNKNOWN)
    if t == '1':
        foo.add(n, p, HOME)
    if t == '2':
        foo.add(n, p, WORK)
    if t == '3':
        foo.add(n, p, FAX)
    if t == '4':
        foo.add(n, p, CELL)

You have mistyped __init__ :)

It's __int__ in your phonedb class, look carefully...

You have mistyped __init__ :)

It's __int__ in your phonedb class, look carefully...

Thank you so much for pointing out this mistake. I have checked through several times and I didn't for once noticed it. I was so confused when I couldn't get the expected result, and didn't know what else to do, not knowing it was my mistake. Thank you once again. I almost gave up on the program. For those who might want to run something similar, below is the correct version of the program, I had tested it and it's running smoothly.

class phonedb:
    def __init__(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)
        self.shelve[str(e)] = e
 
    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 '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.