第十四章、 地址薄作业

#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"

  

python 教程 第十四章、 地址薄作业的更多相关文章

  1. 进击的Python【第十四章】:Web前端基础之Javascript

    进击的Python[第十四章]:Web前端基础之Javascript 一.javascript是什么 JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编 ...

  2. Flask 教程 第十四章:Ajax

    本文翻译自The Flask Mega-Tutorial Part XIV: Ajax 这是Flask Mega-Tutorial系列的第十四部分,我将使用Microsoft翻译服务和少许JavaSc ...

  3. python 教程 第二十二章、 其它应用

    第二十二章. 其它应用 1)    Web服务 ##代码 s 000063.SZ ##开盘 o 26.60 ##最高 h 27.05 ##最低 g 26.52 ##最新 l1 26.66 ##涨跌 c ...

  4. python 教程 第十五章、 结构布局

    第十五章. 结构布局 #!/usr/bin/env python #(1)起始行 "this is a module" #(2)模块文档 import sys #(3)模块导入 d ...

  5. python 教程 第十二章、 标准库

    第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1)    sys模块 import sys if len(sys.argv) ...

  6. python 教程 第十九章、 图形界面编程

    第十九章. 图形界面编程 import Tkinter top = Tkinter.Tk() hello = Tkinter.Label(top, text='Hello World!') hello ...

  7. python 教程 第十六章、 正则表达式

    第十六章. 正则表达式 1)    匹配多个表达式 记号  re1|re2 说明  匹配正则表达式re1或re2 举例  foo|bar  匹配  foo, bar 记号  {N} 说明  匹配前面出 ...

  8. C#图解教程 第十四章 事件

    事件 发布者和订阅者源代码组件概览声明事件订阅事件触发事件标准事件的用法 通过扩展EventArgs来传递数据移除事件处理程序 事件访问器 事件 发布者和订阅者 很多程序都有一个共同的需求,既当一个特 ...

  9. C#图解教程 第二十四章 反射和特性

    反射和特性 元数据和反射Type 类获取Type对象什么是特性应用特性预定义的保留的特性 Obsolete(废弃)特性Conditional特性调用者信息特性DebuggerStepThrough 特 ...

随机推荐

  1. eclipse调试鼠标放上去显示变量值

    在eclipse中调试时,鼠标移动到变量上不显示值,这个原来自己也遇到过,没注意,反正就使用ctrl+shift+i嘛,也可以的,刚查了一下,解决方法如下: Window->Preference ...

  2. 《Unix编程艺术》读书笔记(1)

    <Unix编程艺术>读书笔记(1) 这两天開始阅读该书,以下是自己的体会,以及原文的摘录,尽管有些东西还无法全然吃透. 写优雅的代码来提高软件系统的透明性:(P134) Elegance ...

  3. PatentTips - High-performance AHCI Interface

    BACKGROUND OF THE INVENTION Various storage protocols for communicating with storage devices are kno ...

  4. 【C++竞赛 F】yyy的三角形

    时间限制:2s 内存限制:32MB 问题描述 yyy对三角形非常感兴趣,他有n个木棍,他正在用这些木棍组成三角形.这时xxx拿了两根木棍过来,xxx希望yyy能给他一根木棍,使得xxx可以组成一个三角 ...

  5. web报表工具FineReport经常使用函数的使用方法总结(文本函数)

    文本函数 CHAR CHAR(number):依据指定数字返回相应的字符.CHAR函数可将计算机其它类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...

  6. [Most.js] Create Streams From Single Values With Most.js

    Most provides many means for creating streams, the simplest of which is the offunction. In this less ...

  7. react渲染和diff算法

    1.生成虚拟dom createElement的作用就是生成虚拟dom.虚拟dom到底是个啥,其实它就是个javascript对象~,这个对象的属性有props,vType,type, 而props也 ...

  8. python 爬取豆瓣的美剧

    pc版大概有500条记录,mobile大概是50部,只有热门的,所以少一点 url构造很简单,主要参数就是page_limit与page_start,每翻一页,start+=20即可,tag是&quo ...

  9. Java数组定义学习的一些随笔

    //一维数组的定义 int[] arr1 = new int[3];//arr1 = {1,2,3}: 错误 int[] arr2 = new int[]{1,2,3};//int[] arr2 = ...

  10. Android四个基本组件(2)之Service 服务与Content Provider内容提供商

    一.Service 维修: 一Service 这是一个长期的生命周期,没有真正的用户界面程序,它可以被用于开发如监视类别节目. 表中播放歌曲的媒体播放器.在一个媒体播放器的应用中.应该会有多个acti ...