2012-09-08 55 views
0

我是新來的編程和有一個任務,我一直在工作一段時間。我明白定義函數和很多基礎知識,但是現在我有點碰壁。電話簿數據庫幫助 - Python

我試圖找出這一個,並不真正瞭解'類'功能如何工作。我會很感激這個人的任何幫助;還有一些python資源的幫助可以虛擬下如何/爲什麼使用類。


你一直在工作一段時間的數據庫項目。你的老闆鼓勵你用Python編程數據庫。你不同意,認爲Python不是一種數據庫語言,但你的老闆通過提供下面的示例電話數據庫的源代碼持續存在。

他要求你做兩件事情:

  1. 評估現有的源代碼,並擴展它,使之成爲在該公司管理人員非常有用。 (您不需要GUI界面,只需處理數據庫方面:數據輸入和檢索 - 當然,您必須讓程序運行或正常工作

  2. 他希望您批判性地評估Python作爲數據庫工具。


導入下面的示例代碼到Python IDLE,提升它,運行和調試。通過提供簡單的數據錄入和檢索添加功能,使之成爲一個更現實的數據庫工具。

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()) 

我不知道如果我在正確的軌道上,但這裏是我到目前爲止有:

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() 

回答