Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5834
  • 博文数量: 4
  • 博客积分: 112
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-06 00:11
文章分类
文章存档

2010年(4)

我的朋友
最近访客

分类: Python/Ruby

2010-03-21 12:39:20

#!/usr/bin/python -d
# -*- coding: UTF-8 -*-

import sys
import getopt
import cPickle
import os

class CABinfo:
def __init__(self, name, email):
self.name = name
self.email = email
def __repr__(self):
return 'name = %s, email = %s' % (self.name, self.email)

class CAB:
def __init__(self, filename):
self.filename = filename
self.addr_infos = {}
if os.path.isdir(filename):
print '%s is a dir!' % filename
sys.exit()
if os.path.isfile(filename) and os.stat(filename)[6]:
f = file(filename, 'r+')
self.addr_infos = cPickle.load(f)

def __del__(self):
f = file(self.filename, 'w')
cPickle.dump(self.addr_infos, f)

def add_one(self, name, email):
if self.addr_infos.has_key(name):
print 'add failed. name(%s) already exists!' % name
else:
self.addr_infos[name] = CABinfo(name, email)
print 'add successful. name(%s) -> email(%s)' % (name, email)
def del_one(self, name):
if self.addr_infos.has_key(name):
del self.addr_infos[name]
print 'del successful. name(%s)' % name
else:
print 'del failed. name(%s) dose not exist' % name
def mod_one(self, name, email):
if self.addr_infos.has_key(name):
self.addr_infos[name] = CABinfo(name, email)
print 'mod successful. name(%s) -> email(%s)' % (name, email)
else:
print 'mod failed. name(%s) dose not exist' % name
def list_all(self):
if len(self.addr_infos):
print 'list all name and email-------------------------'
for n, e in self.addr_infos.items():
print 'name(%s) -> email(%s)' % (n, e)
print 'list over---------------------------------------'
else:
print 'empty now!'
def clear(self):
self.addr_infos.clear()
self.addr_infos={}
print 'clear successful!'

def query_one(self, name):
if self.addr_infos.has_key(name):
print 'query successful. name(%s) -> email(%s)' % (name, self.addr_infos[name])
else:
print 'query failed. name(%s) dose not exist' % name

def usage():
print '''
-f`file_name` -a -n`name` [-e`email`]
-f`file_name` -c
-f`file_name` -d -n`name`
-f`file_name` -l
-f`file_name` -m -n`name` [-e`email`]
-f`file_name` -q -n`name`
-h
'''
sys.exit()

def main():
filename = ''
email = ''
name = ''
is_add = False
is_clear = False
is_del = False
is_list = False
is_mod = False
is_query  = False
action_count = 0

try:
opts, args = getopt.getopt(sys.argv[1:], "e:f:n:acdlmqh")
except:
usage()
for o, v in opts:
#print 'o', o, 'v', v
if o == "-e":
email = v
elif o == "-f":
filename = v
elif o == "-n":
name = v
elif o == "-a":
is_add = True
action_count += 1
elif o == "-c":
is_clear = True
action_count += 1
elif o == "-d":
is_del = True
action_count += 1
elif o == "-m":
is_mod = True
action_count += 1
elif o == "-q":
is_query = True
action_count += 1
elif o == "-l":
is_list = True
action_count += 1
elif o == "-h":
usage()

if action_count > 1:
print u"只能指定一个查询动作"
usage()

if not filename:
print u"必需同时指定 -f"
usage()

#######################################################################
ab = CAB(filename)
if is_add:
if not name:
usage()
ab.add_one(name, email)
elif is_clear:
ab.clear()
elif is_del:
if not name:
usage()
ab.del_one(name)
elif is_list:
ab.list_all()
elif is_mod:
if not name:
usage()
ab.mod_one(name, email)
elif is_query:
if not name:
usage()
ab.query_one(name)
else:
#理论上不会走这个分支
pass
del ab
#######################################################################

if __name__ == '__main__':
main()

阅读(625) | 评论(0) | 转发(0) |
0

上一篇:了解虚拟主机

下一篇:没有了

给主人留下些什么吧!~~