从数据库打印列表

时间:2011-10-07 01:53:57

标签: python list

以下是名为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')
            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() )

我不得不接受这个程序并对其进行修改以使其更好。我已经能够完成所有事情,但打印的数据库列表包含已添加各自信息的所有人。有人可以帮助我,或者朝着正确的方向激励我......见下文:我的代码......

   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"

1 个答案:

答案 0 :(得分:2)

你需要使用比cmp()更复杂的东西;请尝试fnmatch.fnmatch(),传递'*'以查找所有条目。您可能也想查看fnmatch.filter()