2011-10-07 63 views
1

这里打印列表程序中调用电话:从数据库

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" 

回答

2

你需要使用一些更复杂的比cmp();改为尝试fnmatch.fnmatch(),通过'*'查找所有条目。你也可以看看fnmatch.filter()

+0

我不熟悉fnmatch()。任何想法如何摆脱列表中的重复? –

+0

这与打印列表有什么关系? –

+0

@ ignacio我的错误。当我能够打印目录列表时,出现了一些重复内容......试图找出如何摆脱cmp()函数中的内容。 –