python学习笔记之入门

要求:创建你自己的命令行 地址簿 程序。在这个程序中,你可以添加、修改、删除和搜索你的联系人以及它们的信息(诸如电子邮件地址或电话号码)。这些详细信息应该被保存下来以便以后提取。

分析:创建一个类来表示一个人的信息。使用字典储存每个人的对象,把他们的名字作为键。使用cPickle模块永久地把这些对象储存在你的硬盘上。使用字典内建的方法添加、删除和修改人员信息。 

#!/usr/bin/python
# Filename: iaddress.py

import os
import sys
import cPickle as p

class Contact:
	def __init__(self, name, email, phone):
		self.name = name
		self.email = email
		self.phone = phone
	def modname(self,name):
		self.name = name
	def modemail(self,email):
		self.email = email
	def modphone(self,phone):
		self.phone = phone
	def getname(self):
		return self.name
	def getemail(self):
		return self.email
	def getphone(self):
		return self.phone
	def display(self):
		print "name:",self.name,"| email:",self.email,"| phone:",self.phone

def Help():
	print '''\
This program add/mod/del/search/display Contacts.
Options include:
	--add <name> <email> <phone>
	--mod <name> <email> <phone>
	--del <name>
	--search <name=xxx> | <email=xxx> | <phone=xxx>
	--display : Display all contacts in the address book file
	--version : Prints the version number
	--help : Display this help'''
	sys.exit()

def Checkemail(email):
	if email.find('@') == -1:
		print "ERR: your email address is not validated. Please check email address."
		sys.exit()

def Checkphone(phone):
	firstno = phone[0:1]
	if firstno != "1":
		print "ERR: your phone No. is not validated. Phone No. must start with 1."
		sys.exit()
	if len(phone) != 11:
		print "ERR: your phone No. is not validated. Check longth."
		sys.exit()

def Store(abfile, ab):
        f = file(abfile, 'w')
        p.dump(ab, f)
        f.close()

def Loadabfile(abfile):
	if os.path.isfile(abfile) == False:
		print "ERR: the address book file:",abfile," is not exist!"
		sys.exit()
	ab = { }
	f = file(abfile, 'r')
	ab = p.load(f)
	f.close()
	return ab

abfile = "iaddress.data"
ab = { }

if len(sys.argv) < 2:
	Help()

if sys.argv[1].startswith('--'):
	option = sys.argv[1][2:]
	if option == 'add':
		if len(sys.argv) == 5:
			name = sys.argv[2]
			email = sys.argv[3]
			phone = sys.argv[4]
			Checkemail(email)
			Checkphone(phone)
			c = Contact(name, email, phone)
			if os.path.isfile(abfile) == True:
				ab = Loadabfile(abfile)
			ab[name] = c
			Store(abfile, ab)
			print "Add contact successfully."
		else:
			Help()
	elif option == 'mod':
		if len(sys.argv) == 5:
			name = sys.argv[2]
			email = sys.argv[3]
			phone = sys.argv[4]
			Checkemail(email)
			Checkphone(phone)
			ab = Loadabfile(abfile)
			if name in ab:
				ab[name].modemail(email)
				ab[name].modphone(phone)
				Store(abfile, ab)
				print "Mod contact successfully."
			else:
				print "ERR: contact name:",name," is not exists!"
				sys.exit()
		else:
			Help()
	elif option == 'del':
		if len(sys.argv) == 3:
			name = sys.argv[2]
			ab = Loadabfile(abfile)
			if name in ab:
				del ab[name]
				Store(abfile, ab)
				print "Del contact successfully."
			else:
				print "ERR: contact name:",name," is not exists!"
				sys.exit()
		else:
			Help()
	elif option == 'search':
		if len(sys.argv) == 3:
			count = 0
			argument = sys.argv[2]
			index = argument.find('=')
			if index == -1:
				Help()
			argumentlist = argument.split('=')
			ab = Loadabfile(abfile)
			if argumentlist[0] == "name":
				name = argumentlist[1]
				if name in ab:
					ab[name].display()
					count = 1
				else:
					print "ERR: contact name:",name," is not exists!"
					sys.exit()
			elif argumentlist[0] == "email":
				email = argumentlist[1]
				Checkemail(email)
				for name, contact in ab.items():
					if contact.getemail() == email:
						contact.display()
						count = 1
			elif argumentlist[0] == "phone":
				phone = argumentlist[1]
				Checkphone(phone)
					for name, contact in ab.items():
						if contact.getphone() == phone:
							contact.display()
					        count = 1
			else:
				Help()
			if count == 0:
				print "Warning: there are no contacts searched!"
		else:
			Help()
	elif option == 'display':
		ab = Loadabfile(abfile)
		for name, contact in ab.items():
			contact.display()
	elif option == 'version':
		print "Iaddress Version 1.0"
	elif option == 'help':
		Help()
	else:
		Help()
else:
	Help()

# python iaddress.py --add ray [email protected] 23533998877
ERR: your phone No. is not validated. Phone No. must start with 1.

# python iaddress.py --add ray [email protected] 1353399887
ERR: your phone No. is not validated. Check longth.

# python iaddress.py --add ray [email protected] 13533998877
Add contact successfully.

# python iaddress.py --search [email protected]
name: ray | email: [email protected] | phone: 13533998877

# python iaddress.py --search name=ray
name: ray | email: [email protected] | phone: 13533998877

# python iaddress.py --del ray
Del contact successfully.

你可能感兴趣的:(python学习笔记之入门)