• python名片本程序


      1 # FileName: cardbook.py
    2
    3 # this program helps u manage your card book.
    4 # it has 6 functions:
    5 # 1. add a record 4. print the cardbook
    6 # 2. del a record 5. save the cardbook
    7 # 3. search a record 6. exit...
    8
    9 # card book will be saved in c:\\python_code\\cardbook.data
    10
    11 # First released by wgx. 2012/3/21
    12 import pickle as p
    13 import os
    14 import sys
    15
    16 class personCard:
    17 def __init__(self, cardid, name, address, phone, email, classfy):
    18 self.card = {'cardid' : cardid,
    19 'name' : name,
    20 'address' : address,
    21 'phone' : phone,
    22 'email' : email,
    23 'classfy' : classfy
    24 }
    25 #print 'Successful to create a personCard', self.card
    26
    27 class cardBook:
    28 CardId = 0
    29 def getCardid(self):
    30 '''Get the current cardid from file.'''
    31 def __init__(self):
    32 '''Read from file. if doesnt exist, then make one.'''
    33 self.aBook = []
    34 def addRecord(self, aCard):
    35 '''Add a record to the cardbook.'''
    36 self.aBook.append(aCard.card)
    37 cardBook.CardId += 1
    38 #print 'Successful to add a record', aCard.card
    39
    40 def delRecord(self):
    41 '''Delete a record from the cardbook.'''
    42 print 'who do you want to deltete from the cardbook?'
    43 name = raw_input('plz input a name: ')
    44 abook2 = self.search_by_name(name)
    45 if abook2 == []:
    46 print "%s doesn't exist." %name
    47 else:
    48 del_book_seq = []
    49 i = len(abook2) - 1
    50 while i >= 0:
    51 for j in range(0, len(self.aBook)):
    52 if abook2[i] == self.aBook[j]:
    53 del_book_seq.append(j)
    54 i = i - 1
    55 # print 'del_book_seq', del_book_seq
    56 print 'the following record(s) has the name %s:' %name
    57 self.prt_tmp_cardbook(abook2)
    58 print '\nDo you really want to delete? Y/N'
    59 third_order = raw_input('plz input: ')
    60 if third_order == 'Y' or third_order == 'y':
    61 if len(abook2) >= 2:
    62 print "delete 'a'll or delete 'o'ne?"
    63 forth_order = raw_input("plz input('a' or 'o'): ")
    64 if forth_order == 'a' or forth_order == 'A':
    65 for k in del_book_seq:
    66 del self.aBook[k]
    67 print 'DEL successfully!'
    68 elif forth_order == 'o' or forth_order == 'O':
    69 fifth_order = raw_input('plz input the NUM: ')
    70 print 'len(del_book_seq) = ', len(del_book_seq)
    71 if int(fifth_order) > len(del_book_seq) or int(fifth_order) <= 0:
    72 print 'no such NUM!'
    73 else:
    74 del self.aBook[del_book_seq[int(fifth_order) - 1]]
    75 print 'DEL successfully!'
    76 else:
    77 print "input 'a' or 's'!"
    78
    79 elif len(abook2) == 1: # only one record should delete
    80 for k in del_book_seq:
    81 del self.aBook[k]
    82 print 'DEL successfully!'
    83 elif third_order == "N" or third_order == 'n':
    84 print 'u choose dont del anything..'
    85 else:
    86 print 'wrong command..'
    87
    88 #print 'Successful to del a record', aCard.card
    89
    90
    91 # search a certain record by its name.
    92 # return a list of records which contains the name.
    93 def search_by_name(self, name):
    94 '''Search a record by name.'''
    95 abook1 = []
    96 for arecord in self.aBook:
    97 if arecord['name'] == name:
    98 abook1.append(arecord)
    99 return abook1
    100
    101 def cal_spa(self, astring1, astring2):
    102 i = 12 + len(astring1) - len(astring2)
    103 return astring2 + i * ' '
    104
    105 def prt_cardbook(self):
    106 '''Print the current cardbook.'''
    107 print 'your query result is:\n'
    108 print 'name add phone email classfy'
    109 for i in range(0, len(self.aBook)):
    110 name = self.aBook[i]['name']
    111 address = self.aBook[i]['address']
    112 phone = self.aBook[i]['phone']
    113 email = self.aBook[i]['email']
    114 classfy = self.aBook[i]['classfy']
    115 print self.cal_spa('name', name) + self.cal_spa('add', address) + self.cal_spa('phone', phone) + self.cal_spa('email', email) + classfy
    116
    117 def prt_tmp_cardbook(self, a_tmp_book):
    118 '''Print the a_tmp_book.'''
    119 print 'your query result is:\n'
    120 print 'num name add phone email classfy'
    121 for i in range(0, len(a_tmp_book)):
    122 tmp_m = `i + 1`
    123 tmp_num = tmp_m + (4 - len(tmp_m)) * ' '
    124 tmp_name = a_tmp_book[i]['name']
    125 tmp_address = a_tmp_book[i]['address']
    126 tmp_phone = a_tmp_book[i]['phone']
    127 tmp_email = a_tmp_book[i]['email']
    128 tmp_classfy = a_tmp_book[i]['classfy']
    129 print tmp_num + self.cal_spa('name', tmp_name) + self.cal_spa('add', tmp_address) + self.cal_spa('phone', tmp_phone) + self.cal_spa('email', tmp_email) + tmp_classfy
    130
    131
    132 class get_set_cardbook:
    133 def get_cardbook(self, acardBook):
    134 '''Get the cardbook from file.'''
    135 cardbookfile = 'c:\\python_code\\cardbook.data'
    136 if os.path.exists(cardbookfile):
    137 f = file(cardbookfile)
    138 acardBook = p.load(f)
    139 return acardBook
    140
    141 def set_cardbook(self, acardBook):
    142 '''Save the cardbook to the file.'''
    143 cardbookfile = 'c:\\python_code\\cardbook.data'
    144 # test whether the route 'c:\\python_code' exists.
    145 # If not, create it.
    146 if os.path.exists('c:\\python_code'):
    147 f = file(cardbookfile, 'w')
    148 p.dump(acardBook, f)
    149 else:
    150 try:
    151 os.mkdir('c:\\python_code')
    152 except Exception:
    153 pass
    154 f = file(cardbookfile, 'w')
    155 p.dump(acardBook, f)
    156
    157
    158 def main_menu():
    159 print ''
    160 print '------------------MAIN MENU----------------------'
    161 print '1. add a record 4. print the cardbook'
    162 print '2. del a record 5. save the cardbook'
    163 print '3. search a record 6. exit...'
    164 print '-------------------------------------------------'
    165 print ''
    166 return raw_input('plz choose a command: ')
    167 print ''
    168
    169 def interact(firstorder):
    170 is_change = False
    171 is_save = False
    172 if firstorder == '1':
    173 is_change = True
    174 print '\nyou can add an record, just follow me.'
    175 while True:
    176 name = raw_input('name: ')
    177 address = raw_input('address: ')
    178 phone = raw_input('phone: ')
    179 email = raw_input('email: ')
    180 # classfy must be classmate, colleague, relative or friend
    181 while True:
    182 tmp_classfy = raw_input("classfy(classmate, colleague, relative, friend): ")
    183 if tmp_classfy == 'classmate' or tmp_classfy == 'colleague' or tmp_classfy == 'relative' or tmp_classfy == 'friend':
    184 classfy = tmp_classfy
    185 break
    186 cardid = cardBook.CardId
    187 aperCard = personCard(cardid, name, address, phone, email, classfy)
    188 acardBook.addRecord(aperCard)
    189 firstorder1 = raw_input('do u want to ADD another record?(Y/N): ')
    190 if firstorder1 == 'N' or firstorder1 == 'n':
    191 print 'back to the main menu:'
    192 break
    193 elif firstorder == '2':
    194 is_change = True
    195 while True:
    196 acardBook.delRecord()
    197 firstorder2 = raw_input('do u want to DEL another record?(Y/N): ')
    198 if firstorder2 == 'N' or firstorder2 == 'n':
    199 print 'back to the main menu:'
    200 break
    201 elif firstorder == '3':
    202 tmp_name = raw_input('plz input the NAME u want to search: ')
    203 tmp_book = acardBook.search_by_name(tmp_name)
    204 if tmp_book == []:
    205 print "sorry, %s doesn't exist in the cardbook..." %tmp_name
    206 else:
    207 acardBook.prt_tmp_cardbook(tmp_book)
    208 elif firstorder == '4':
    209 acardBook.prt_cardbook()
    210 elif firstorder == '5':
    211 gs.set_cardbook(acardBook)
    212 is_save = True
    213 elif firstorder == '6':
    214 if is_change == True:
    215 if is_save == True:
    216 print 'quit...'
    217 sys.exit()
    218 else:
    219 print "\nWARNNING: you havn't SAVE the cardbook."
    220 firstorder3 = raw_input('do u want to SAVE now? (Y/N): ')
    221 if firstorder3 == 'Y' or firstorder3 == 'y':
    222 gs.set_cardbook(acardBook)
    223 else:
    224 print 'quit will NOT save the changes.'
    225 print 'quit...'
    226 sys.exit()
    227 else:
    228 sys.exit()
    229 else:
    230 print 'UNKNOW command!'
    231 # get the card book from 'c:\\python_code\\cardbook.data' if existed.
    232 # else create a new cardBook.
    233 acardBook = cardBook()
    234 gs = get_set_cardbook()
    235 acardBook = gs.get_cardbook(acardBook)
    236
    237 print '******************************************************************'
    238 print '\n'
    239 print ' Hello, welcome to CARDBOOK!'
    240 print '\n'
    241 print ' first released by wgx'
    242 print ' 2012.12.21 Version1.0'
    243 print '******************************************************************'
    244 print 'you can: '
    245 while True:
    246 firstorder = main_menu()
    247 interact(firstorder)
  • 相关阅读:
    内联函数(inline function)
    被extern "C" 修饰后按照C语言编译
    函数重载-name mangling或mame decoration技术-看看反汇编
    cin、cout
    下载visual studio 环境搭建
    配置共享文件夹
    EFS加密文件系统
    十二、字符串(1)
    十五、文件
    十一、指针
  • 原文地址:https://www.cnblogs.com/forstudy/p/2409135.html
Copyright © 2020-2023  润新知