python通讯录实现

1.什么(分析)

Python 创建你自己的命令行 地址簿 程序;

在这个程序中,你可以添加、修改、删除和搜索你的联系人(朋友、家人和同事等等)以及它们的信息(诸如电子邮件地址和/或电话号码)。这些详细信息应该被保存下来以便以后提取。

 

2.如何(设计)

创建一个类来表示一个人的信息。使用字典储存每个人的对象,把他们的名字作为键。使用cPickle模块永久地把这些对象储存在你的硬盘上。使用字典内建的方法添加、删除和修改人员信息。

 

3.编写(实施)

 #D:\360Downloads\a_python\test
#Filename: addressbook1.py


import cPickle as p
import sys
phonelistfile = 'AddressBook.data.txt'
phonelist = { }


class Opt:
  
    def __init__(self,name,phone,email,type):
        self.name = name
 self.phone = phone
 self.email = email
 self.type = type


    def Add(self):
        print self.name+'Add is OK~'
        phonelist[self.name]=self.phone,self.email,self.type
 f = file(phonelistfile, 'w')
        p.dump(phonelist, f)
        f.close()
        f = file(phonelistfile)
        storedlist = p.load(f)
        print '\n'
        print storedlist

    def Del(self):
       
 del storedlist[self.name]
 print self.name+'Del is OK~'
        f = file(phonelistfile, 'w')
        p.dump(phonelist, f)
        f.close()

    def Edit(self):
       
 phonelist[self.name]=self.phone,self.email,self.type
 print self.name+' Edit is OK~'
        f = file(phonelistfile, 'w')
        p.dump(phonelist, f)
        f.close()

if len(sys.argv) < 2:
    print 'Welcome to Adair python lab.  2012.09.18  Email:[email protected]'
    print '************** AddressBook 1.0  *******************'
    print 'Usage---'
    print 'Add  -- python addressbook1.py --Add'
    print 'List -- python addressbook1.py --List'
    print 'Edit -- python addressbook1.py --Edit'
    print 'Del  -- python addressbook1.py --Del'
    print 'Good Luck~'
    sys.exit()


if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:]
    if option == 'Add':
        print '--------Add--------'
        name = raw_input('name:')
        phone = raw_input('phone:')
        email = raw_input('email:')
        type = raw_input('type:')
        Opt =Opt(name,phone,email,type)
        Opt.Add()


    elif option == 'Del':
        print '--------Del--------'
 f = file(phonelistfile)
        storedlist = p.load(f)
        print '\n'
   
 for name, phone in storedlist.items():
            print ' %s , %s' % (name, phone)

 Opt =Opt(name,phone,'aa','bb')
 Opt.Del()


    elif option == 'List':
        print '--------List--------'
       
        f = file(phonelistfile)
        storedlist = p.load(f)
        print '\n'
        print storedlist

 

    elif option == 'Edit':
        print '--------Edit--------'
       
        f = file(phonelistfile)
        storedlist = p.load(f)
        print '\n'
        for name, phone in storedlist.items():
            print ' %s , %s' % (name, phone)

 phone = raw_input('phone:')
        email = raw_input('email:')
        type = raw_input('type:')
        Opt =Opt(name,phone,email,type)
 Opt.Edit()

4.测试(测试与调用)

 python通讯录实现_第1张图片

python通讯录实现_第2张图片

5.使用(实施或开发)

 

6.维护(优化)

通讯录

你可能感兴趣的:(python学习)