第十四章、 地址薄作业

#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. js进阶ajax函数封装(匿名函数作为参数传递)(封装函数引入文件的方式非常好用)

    js进阶ajax函数封装(匿名函数作为参数传递)(封装函数引入文件的方式非常好用) 一.总结 2.匿名函数作为参数传递 二.js进阶ajax函数封装 ajax1.js function ajax(ur ...

  2. A Guide to Python's Magic Methods

    Book Source:[https://rszalski.github.io/magicmethods/] magic methods: 名称前后有双下划线的方法 构造函数和初始化 初始化类实例时, ...

  3. hdu 1166 敌兵布阵 线段树 点更新

    // hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...

  4. ImageView一例 分类: H1_ANDROID 2013-10-30 23:02 1812人阅读 评论(0) 收藏

    参考自<疯狂android讲义>2.4节 效果如下: 当点击图上某点时,将之附近放大至下图. 布局文件: <LinearLayout xmlns:android="http ...

  5. 【a702】贷款利率

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 当一个人从银行贷款后,在一段时间内他将不得不每月尝还固定的分期付款.这个问题要求计算机出贷款者向银行支付的利 ...

  6. [TypeScript] Create random integers in a given range

    Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int betwee ...

  7. Android动态修改图片颜色的实现方式分析

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.修改色相.饱和度.亮度 参看:http://blog.csdn.NET/sjf0115/article/details/7267063 2.使 ...

  8. HBase总结(十一)hbase Java API 介绍及使用演示样例

    几个相关类与HBase数据模型之间的相应关系 java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) H ...

  9. 关于MD5值加密算法

    public static string getMD5(string str)//该方法获取字符串的md5加密 通经常使用来验证数据     {         System.Security.Cry ...

  10. php的标准输入与输出是什么?

    php的标准输入与输出是什么? 一.总结 php的标准输入与输出(STDIN是一个文件句柄,等同于fopen("php://stdin", 'r')) 1.STDIN是一个文件句柄 ...