python 教程 第十四章、 地址薄作业

简介: 第十四章、 地址薄作业 #A Byte of Python #!/usr/bin/env python import cPickle import os #define the contacts file, list global file_contacts global list_contacts file_contacts = 'AddressBook.

第十四章、 地址薄作业

#A Byte of Python
#!/usr/bin/env python
import cPickle
import os 
#define the contacts file, list
global file_contacts
global list_contacts
file_contacts = 'AddressBook.txt'
list_contacts = [] 
#delete the file
try:
    file_path = os.getcwd() + os.sep + file_contacts
    if os.path.isfile(file_path):
        os.remove(file_contacts)
        f = file(file_contacts,'w')
        f.close() 
     #define the class of contacts and implement the methods
    class class_contacts:
        def __init__(self, list_contacts):
            self.contacts = list_contacts
        def append(self, name, mail):
            dict_contacts = {'Name': name, 'Mail': mail}
            self.contacts.append(dict_contacts)
        def find(self, name):
            found = False
            for instance_contacts in list_contacts:
                if name == instance_contacts['Name']:
                    found = True
                    print " Found:", name, ", PASS"
                    break
            if not found:
                print " Found:", name, ", FAIL" 
        def modify(self, name, mail):
            for instance_contacts in list_contacts:
                if name == instance_contacts['Name']:
                    instance_contacts['Mail'] = mail
                    break
        def delete(self, name):
            index = -1
            for instance_contacts in list_contacts:
                index += 1
                if name == instance_contacts['Name']:
                    del list_contacts[index]
        def show(self):
            for list in list_contacts:
                print list
        def save(self):
            fa = file(file_contacts, "a")
            cPickle.dump(self.contacts, fa)
            fa.close() 
    i = class_contacts(list_contacts)
    i.append('joan', 'joan@123.com')
    i.append('adny', 'adny@123.com')
    i.append('xixi', 'xixi@123.com')
    i.find('joan')
    i.find('joab')
    print "Original List:"
    i.show()
    print "after modify adny"
    i.modify('adny', 'adnX@123.com')
    i.show()
    print "after del joan"
    i.delete('joan')
    i.show()
    i.save() 
except TypeError:
    print "TypeError"
except:
print "Other Error occured" 

  

相关文章
|
1月前
|
数据处理 Python
Python教程:生成Excel并更改表头
Python教程:生成Excel并更改表头
24 0
|
1月前
|
数据可视化 计算机视觉 Python
Python教程:如何获取颜色的RGB值
Python教程:如何获取颜色的RGB值
21 0
|
1月前
|
Python
Python教程第10章 | 通俗易懂学装饰器
本章通过案例讲述了Python装饰器的基本概念与实战用法
18 0
Python教程第10章 | 通俗易懂学装饰器
|
1月前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
63 1
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
165 0
|
1月前
|
存储 安全 API
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
【Python 基础教程 21】Python3 文件操作全面指南:从入门到精通的综合教程
77 0
|
27天前
|
JSON 数据格式 Python
13 Python 阶段性总结抽奖系统(文末附代码地址)
13 Python 阶段性总结抽奖系统(文末附代码地址)
54 0
13 Python 阶段性总结抽奖系统(文末附代码地址)
|
1月前
|
存储 算法 数据挖掘
【Python 基础教程 25】全面入门指南:深度解析Python3的命名空间,作用域及变量使用教程
【Python 基础教程 25】全面入门指南:深度解析Python3的命名空间,作用域及变量使用教程
54 0
|
1月前
|
存储 机器学习/深度学习 数据安全/隐私保护
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
【Python 基础教程 24】全面入门Python面向对象编程:深度探索与实战教程
79 0
|
1月前
|
Linux 数据库连接 C++
【Python 基础教程 23】Python3 错误与异常处理全面指南:从入门到精通的实用教程
【Python 基础教程 23】Python3 错误与异常处理全面指南:从入门到精通的实用教程
110 0