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.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 教程 第十四章、 地址薄作业的更多相关文章
- 进击的Python【第十四章】:Web前端基础之Javascript
进击的Python[第十四章]:Web前端基础之Javascript 一.javascript是什么 JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编 ...
- Flask 教程 第十四章:Ajax
本文翻译自The Flask Mega-Tutorial Part XIV: Ajax 这是Flask Mega-Tutorial系列的第十四部分,我将使用Microsoft翻译服务和少许JavaSc ...
- python 教程 第二十二章、 其它应用
第二十二章. 其它应用 1) Web服务 ##代码 s 000063.SZ ##开盘 o 26.60 ##最高 h 27.05 ##最低 g 26.52 ##最新 l1 26.66 ##涨跌 c ...
- python 教程 第十五章、 结构布局
第十五章. 结构布局 #!/usr/bin/env python #(1)起始行 "this is a module" #(2)模块文档 import sys #(3)模块导入 d ...
- python 教程 第十二章、 标准库
第十二章. 标准库 See Python Manuals ? The Python Standard Library ? 1) sys模块 import sys if len(sys.argv) ...
- python 教程 第十九章、 图形界面编程
第十九章. 图形界面编程 import Tkinter top = Tkinter.Tk() hello = Tkinter.Label(top, text='Hello World!') hello ...
- python 教程 第十六章、 正则表达式
第十六章. 正则表达式 1) 匹配多个表达式 记号 re1|re2 说明 匹配正则表达式re1或re2 举例 foo|bar 匹配 foo, bar 记号 {N} 说明 匹配前面出 ...
- C#图解教程 第十四章 事件
事件 发布者和订阅者源代码组件概览声明事件订阅事件触发事件标准事件的用法 通过扩展EventArgs来传递数据移除事件处理程序 事件访问器 事件 发布者和订阅者 很多程序都有一个共同的需求,既当一个特 ...
- C#图解教程 第二十四章 反射和特性
反射和特性 元数据和反射Type 类获取Type对象什么是特性应用特性预定义的保留的特性 Obsolete(废弃)特性Conditional特性调用者信息特性DebuggerStepThrough 特 ...
随机推荐
- 关于spring获取webApplication.getBean多种途径和简单解释
ApplicationContext ac1 = new FileSystemXmlApplicationContext("com/spark/system/applicationConte ...
- ios开发核心动画五:转场动画
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- iOS开发Quartz2D十二:手势解锁实例
一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...
- JAVA SkipList 跳表 的原理和使用例子
跳跃表是一种随机化数据结构,基于并联的链表,其效率可比拟于二叉查找树(对于大多数操作需要O(log n)平均时间),并且对并发算法友好. 关于跳跃表的具体介绍可以参考MIT的公开课:跳跃表 跳跃表的应 ...
- 【C++竞赛 F】yyy的三角形
时间限制:2s 内存限制:32MB 问题描述 yyy对三角形非常感兴趣,他有n个木棍,他正在用这些木棍组成三角形.这时xxx拿了两根木棍过来,xxx希望yyy能给他一根木棍,使得xxx可以组成一个三角 ...
- php实现 坐标移动
php实现 坐标移动 一.总结 一句话总结:伪代码,带函数逻辑,函数这样的方式写算法程序会节约超多的时间. 1.为什么算法题数据输入最好用多组数据输入的方式? 因为都是多组数据测试,而且多组数据输入 ...
- Xshell Update
http://blog.netsarang.com/1629/xshell-update-5-0-1332/ Xshell Update (5.0.1332) By Alan Kim Thursday ...
- 【hdu 1517】A Multiplication Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- HDU 3215 The first place of 2^n (数论-水题)
The first place of 2^n Problem Description LMY and YY are mathematics and number theory lovers. They ...
- javascript中隐藏显示的样式表属性
display属性 隐藏不占据位置 visibility属性 隐藏占据位置 //使用display的样式属性 隐藏 显示 //隐藏后不占据文档流位置 function showAddForm(){ v ...