Phone-book Database Help - Python

Posted by IDOntWantThat on Stack Overflow See other posts from Stack Overflow or by IDOntWantThat
Published on 2012-09-08T16:36:53Z Indexed on 2012/09/15 15:38 UTC
Read the original article Hit count: 405

Filed under:
|
|

I'm new to programming and have an assignment I've been working at for awhile. I understand defining functions and a lot of the basics but I'm kind of running into a brick wall at this point.

I'm trying to figure this one out and don't really understand how the 'class' feature works yet. I'd appreciate any help with this one; also any help with some python resources that have can dummy down how/why classes are used.


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

  2. He wants you to critically evaluate Python as a database tool.


Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry and retrieval.

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.lower(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 __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', CELL)
    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() )

I'm not sure if I'm on the right track but here is what I have so far:

def openPB():
    foo = phonedb()
    print 'Please select an option:'
    print '1 - Lookup'
    print '2 - Add'
    print '3 - Delete'
    print '4 - Quit'
    entry=int(raw_input('>> '))
    if entry==1:
            namelookup=raw_input('Please enter a name: ')
            for entry in foo.lookup(namelookup):
                    print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype() )
    elif entry==2:
            name=raw_input('Name: ')
            number=raw_input('Number: ')
            showtype=input('Type (UNKNOWN, HOME, WORK, FAX, CELL): \n>> ')
            for entry in foo.add(name, number, showtype):           #Trying to figure out this part
                    print '%-40s %s (%s)'% (entry.name, entry.number, entry.showtype() )
    elif entry==3:
            delname=raw_input('Please enter a name to delete: ')
            #                                                       #Trying to figure out this part
            print "Contact '%s' has been deleted" (delname)
    elif entry==4:
            print "Phone book is now closed"
            quit
    else:
            print "Your entry was not recognized."
            openPB()

openPB()

© Stack Overflow or respective owner

Related posts about python

Related posts about database