第十四章、 地址薄作业

#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. [D3] Load and Inspect Data with D3 v4

    You probably use a framework or standalone library to load data into your apps, but what if that’s o ...

  2. SpringMVC+Spring+Mybatis+Mysql项目搭建

    眼下俺在搭建一个自己的个人站点玩玩.一边练习.一边把用到的技术总结一下,日后好复习. 站点框架大致例如以下图所看到的: 眼下仅仅用到了SpringMVC+Spring+Mybatis+Mysql.把它 ...

  3. 使用DatagramSocket与DatagramPacket传输数据 分类: B1_JAVA 2013-10-12 13:00 1936人阅读 评论(0) 收藏

    参考传智播客毕向东JAVA视频. 注: DatagramSocket发送的每个包都需要指定地址,而Socket则是在首次创建时指定地址,以后所有数据的发送都通过此socket. A socket is ...

  4. 微信小程序初步运营方案

    小程序的运营方案有很多种,目前我们遇到两个事情需要解决:1.问答的内容,这块也是大家比较关心的话题.内容的定位和细节. 2.预热与推广,就这两个问题,我列出了一些自己的想法和小程序初步运营方案,有不足 ...

  5. 【codeforces 742A】Arpa’s hard exam and Mehrdad’s naive cheat

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. [React] Update Component State in React With Ramda Lenses

    In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...

  7. [Compose] 10. Capture Side Effects in a Task

    We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...

  8. 嵌入式linux串口设置(一)

    在linux中,所有的设备文件一般都位于“/dev”下,串口1和串口2对应的设备名依次为“/dev/ttyS0”, “/dev/ttyS1”,而且USB转串口的设备名通常为“/dev/ttyUSB0” ...

  9. 复制相关参数学习笔记--slave上的参数

    server_id server_uuid   relay_log io_thread 读取过来的本地日志. relaylog文件名前缀,可以是全路径.   relay_log_index relay ...

  10. [NPM] Add comments to your npm scripts

    The need for comments in your package.json file becomes desirable the more and more npm scripts you ...