posts - 97,  comments - 93,  trackbacks - 0
          這兩天在學(xué)習(xí)python,主要參考簡(jiǎn)明教程http://www.swaroopch.com/byteofpython/ , 入門(mén)很不錯(cuò),有興趣推薦參考。
          在16章中,作者建議讀者寫(xiě)一個(gè)地址簿程序,還記得寫(xiě)它都是在大一學(xué)c語(yǔ)言的年代了,不過(guò)下午還是抽出了一點(diǎn)時(shí)間來(lái)寫(xiě)這個(gè)程序,寫(xiě)了兩個(gè)版本,一個(gè)是使用python 字典來(lái)存儲(chǔ)名字和地址,另一個(gè)版本則使用python的面向?qū)ο筇匦裕瑏?lái)鏈?zhǔn)酱鎯?chǔ)聯(lián)系人對(duì)象。
          版本一代碼:
            1import cPickle as p,sys,re
            2
            3# Author:nicky(nicky.jcoder@gmail.com)
            4# July 24th,2008
            5
            6def readAdd():
            7    f = file(ftxt)
            8    map = p.load(f)
            9    f.close()
           10    return map
           11
           12def autoSave():
           13    f=file(ftxt,'w')
           14    p.dump(book,f)
           15    f.close()
           16    
           17def run():
           18   while True:
           19      try:
           20         com = int(raw_input(welcome+features))
           21      except:
           22         print "\ninput error"
           23         run()
           24      if com == 1:
           25          info = raw_input("input the name and address:")
           26          infoList = re.split(' ',info)
           27          add(infoList[0],infoList[1])
           28          run()
           29          autoSave()
           30          book = readAdd()
           31      elif com == 2:
           32          info = raw_input("input the name:")
           33          search(info)
           34          run()
           35          autoSave()
           36          book = readAdd()
           37      elif com == 3:
           38          info = raw_input("input the name and address:")
           39          infoList = re.split(' ',info)
           40          modify(infoList[0],infoList[1])
           41          run()
           42          autoSave()
           43          book = readAdd()        
           44      elif com == 4:
           45          info = raw_input("input the name:")
           46          delete(info)
           47          run()
           48          autoSave()
           49          book = readAdd()
           50      elif com ==5:
           51          print 'All the people and their addresses are listed:'
           52          browseAll()
           53      elif com == 6:
           54          autoSave()
           55          sys.exit()
           56      else:
           57          print 'your input is invalid'
           58          run()
           59
           60def add(people,address):
           61    if people not in book:
           62        book[people]=address
           63        print '\nadd successfully'
           64    else:
           65        print '\nthe '+people+' exists in the address book'
           66        
           67def search(people):
           68    if people in book:
           69        print "\n"+book[people]
           70    else:
           71        print '\nthe'+people+' is not in the address book'
           72
           73def modify(people,address):
           74    if people in book:
           75        book[people]=address
           76        print '\nmodify successfully'
           77    else:
           78        print '\nthe'+people+' is not in the address book'
           79
           80def delete(people):
           81    if people in book:
           82        del book[people]
           83        print '\ndelete successfully'
           84    else:
           85        print '\nsome errors has been arised'
           86
           87def browseAll():
           88    for people,address in book.items():
           89        print people+':'+address
           90
           91def judgeAddressbook():
           92    f = file(ftxt,'w')
           93    count =0
           94    while True:
           95        line=f.readline()
           96        if len(line)==0:
           97         break
           98        count=count+1
           99    if count == 0:
          100        book={'test':'test'}
          101        autoSave()
          102
          103if __name__ == '__main__':
          104  welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
          105  features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
          106  ftxt = 'addressbook.txt'
          107  book={}
          108  judgeAddressbook()    
          109  book=readAdd()
          110  run()
          版本二代碼:
            1import cPickle as p,sys,re
            2
            3# Author:nicky(nicky.jcoder@gmail.com)
            4# July 24th,2008
            5
            6def readAll():
            7    f = file(ftxt)
            8    list = p.load(f)
            9    f.close()
           10    return list
           11
           12def autoSave():
           13    f=file(ftxt,'w')
           14    p.dump(book,f)
           15    f.close()
           16 
           17class People:
           18    def __init__(self,name,address):
           19        self.name=name
           20        self.address=address
           21    def getName(self):
           22        return self.name
           23    def getAddress(self):
           24        return self.address
           25    def setAddress(address):
           26        self.address=address
           27        
           28def run():
           29   while True:
           30      try:
           31         com = int(raw_input(welcome+features))
           32      except:
           33         print "\ninput error"
           34         run()
           35      if com == 1:
           36          info = raw_input("input the name and address:")
           37          infoList = re.split(' ',info)
           38          add(infoList[0],infoList[1])
           39          run()
           40          autoSave()
           41          book = readAdd()
           42      elif com == 2:
           43          info = raw_input("input the name:")
           44          search(info)
           45          run()
           46          autoSave()
           47          book = readAdd()
           48      elif com == 3:
           49          info = raw_input("input the name and address:")
           50          infoList = re.split(' ',info)
           51          modify(infoList[0],infoList[1])
           52          run()
           53          autoSave()
           54          book = readAdd()        
           55      elif com == 4:
           56          info = raw_input("input the name:")
           57          delete(info)
           58          run()
           59          autoSave()
           60          book = readAdd()
           61      elif com ==5:
           62          print 'All the people and their addresses are listed:'
           63          browseAll()
           64      elif com == 6:
           65          autoSave()
           66          sys.exit()
           67      else:
           68          print 'your input is invalid'
           69          run()
           70
           71def add(people,address):
           72    flag = -1
           73    for item in book:
           74        if people != item.getName():
           75            instance = People(people,address)
           76            book.append(instance)
           77            print '\nadd successfully'
           78            flag =0
           79    if flag == -1:
           80      print '\nthe '+people+' exists in the address book'
           81        
           82def search(people):
           83    flag=-1
           84    for item in book:
           85      if people == item.getName():
           86          print "\n"+item.getAddress()
           87          flag=0
           88    if flag ==-1:
           89        print '\nthe'+people+' is not in the address book'
           90
           91def modify(people,address):
           92    flag=-1
           93    for item in book:
           94       if people == item.getName:
           95               item.setAddress(address)
           96               flag=0
           97               print '\nmodify successfully'
           98    if flag==-1:
           99      print '\nthe'+people+' is not in the address book'
          100       
          101
          102def delete(people):
          103    flag =-1
          104    for item in book:
          105        if people == item.getName:
          106            del item
          107            flag=0
          108            print '\ndelete successfully'
          109    if flag ==-1:
          110       print '\nsome errors has been arised'
          111        
          112
          113def browseAll():
          114    for item in book:
          115        print item.getName()+":"+item.getAddress()
          116
          117def judgeAddressbook():
          118    f = file(ftxt,'w')
          119    count =0
          120    while True:
          121        line=f.readline()
          122        if len(line)==0:
          123         break
          124        count=count+1
          125    if count == 0:
          126        instance=People("test1","test1")
          127        book.append(instance)
          128        autoSave()
          129
          130if __name__ == '__main__':
          131  welcome = '\nWelcome to use AddressBook made by nicky.jcoder@gmail.com'
          132  features="\nyou can use the features listed:\n1:add people and their address information\n2:search\n3:modify\n4:delete\n5:Browse all the info in the addressbook\n6:exit the address book application\nInput your choice:"
          133  ftxt = 'addressbook_object.txt'
          134  book=[]
          135  judgeAddressbook()    
          136  book=readAll()
          137  run()

          運(yùn)行方法應(yīng)該不用說(shuō)了
          python 模塊
          效果圖為

           

          posted on 2008-07-24 17:51 wqwqwqwqwq 閱讀(1569) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): python

          FeedBack:
          # re: python地址簿程序(過(guò)程存儲(chǔ),對(duì)象存儲(chǔ))[未登錄](méi)
          2008-10-19 08:57 | LJ
          你好,想請(qǐng)教個(gè)問(wèn)題,我運(yùn)行了你的第一個(gè)python地址簿程序,輸入6退出后,再進(jìn)入,就會(huì)從新建立一個(gè)新的空白的addressbook.txt文檔,覆蓋了之前那個(gè)已經(jīng)編輯好的,怎么解決這個(gè)問(wèn)題呢?謝謝!  回復(fù)  更多評(píng)論
            
          # re: python地址簿程序(過(guò)程存儲(chǔ),對(duì)象存儲(chǔ))
          2008-10-19 09:23 | nickyqu
          主程序中
          #book={}
          #judgeAddressbook()
          具體的對(duì)其它的影響我沒(méi)細(xì)看,add容錯(cuò)沒(méi)加,你可以加進(jìn)去。  回復(fù)  更多評(píng)論
            
          # Blogosfera
          2009-05-18 08:43 | Blogosfera
          Could you help me. Good judgment comes from experience, and experience comes from bad judgment.
          I am from Ghana and also now am reading in English, tell me right I wrote the following sentence: "Seo design uses web design and search engine optimization to form a superior ecommerce shopping cart.I offer free seo services to small and mid sized companies."

          Thanks for the help :-D, Kalil.  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2009年5月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456




          常用鏈接

          留言簿(10)

          隨筆分類(lèi)(95)

          隨筆檔案(97)

          文章檔案(10)

          相冊(cè)

          J2ME技術(shù)網(wǎng)站

          java技術(shù)相關(guān)

          mess

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          校園夢(mèng)網(wǎng)網(wǎng)絡(luò)電話,中國(guó)最優(yōu)秀的網(wǎng)絡(luò)電話
          主站蜘蛛池模板: 兴海县| 云霄县| 定结县| 师宗县| 祥云县| 天台县| 舞钢市| 环江| 玉溪市| 昌黎县| 岳西县| 电白县| 虞城县| 独山县| 青龙| 浦东新区| 黔西县| 钟山县| 合川市| 梁平县| 仪征市| 舒兰市| 黎城县| 巧家县| 绥滨县| 湾仔区| 沅江市| 东丽区| 泾阳县| 图们市| 精河县| 辉县市| 岑巩县| 若羌县| 五莲县| 什邡市| 江津市| 临江市| 公主岭市| 陈巴尔虎旗| 固安县|