开发者

printing a list from a database

开发者 https://www.devze.com 2023-04-11 03:01 出处:网络
Here is the program called Telephone: import shelve import string UNKNOWN = 0 HOME = 1 WORK = 2 FAX = 3 CELL = 4

Here is the program called Telephone:

 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')
        开发者_如何学JAVA    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 had to take this program and modify it to make it better. I've been able to accomplish everything but print a list of the database that contains everyone that has been added with their respective information. Can someone assist me or stir me in the right direction...see below: my code...

   from Telephone import *


pb = phonedb()

while (1):
    print "Welcome to the Telephone Database!"
    print
    print "Please make a selection:"
    print
    print "1) Add new number"
    print
    print "2) Find a number"
    print
    print "3) list all in the directory "
    print
    print "E) exit"
    print
    myinput = raw_input(':')
    if myinput == "1":
        print "you pushed 1"
    if myinput == "2":
        print "you pushed 2"
    if myinput == "3":
        print "you pushed 3"

    print
    if myinput == "1":

        print "What is the name of the person you want to add? :"
        p = raw_input(':')
        print "What is the number you want specified for this person? :"
        n = raw_input(':')
        print "What type of communication device is this : (0 = UNKNOWN, 1 = HOME, 2 = WORK, 3 = FAX, 4 = CELL)"
        t = raw_input(':')
        if t == '0':
            pb.add(p, n, UNKNOWN)
        if t == '1':
            pb.add(p, n, HOME)
        if t == '2':
            pb.add(p, n, WORK)
        if t == '3':
            pb.add(p, n, FAX)
        if t == '4':
            pb.add(p, n, CELL)

    if myinput == "2":

        print "Type the name of the person whos number you are looking for :"
        search = raw_input(':')
        for entry in pb.lookup(search):
            print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())

    if myinput == "3":   # THIS IS WHERE I NEED ASSISTANCE...********
        for entry in pb.lookup(
            print '%-40s %s (%s)' % (enter.name, entry.number, entry.showtype())





    if myinput == "e" or myinput == "E":
        break

print "Thanks for playing -- El Gooey"


You need to use something more sophisticated than cmp(); try fnmatch.fnmatch() instead, passing '*' to find all entries. You may want to look at fnmatch.filter() as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号