目标:
1.使用序列化cPickle
2.账户中钱要大于花费的钱,否则提示请存钱
2.编写函数,实现存钱,花钱,查询及退出功能
1.序列化
pickle是python实现序列化的模块,次模块存在使用C语言编写模块,用法相同,但执行效率更高,所以优先使用C模块编写的序列化模块cPickle。
2.编写函数,实现存钱,花钱,查询及退出功能
代码如下:
[root@localhost python]# cat new_account.py
#!/usr/bin/env python # -*- coding: utf-8 -*- import os,time import cPickle as p
def save_money(wallet, record, amount, comment): date = time.strftime("%Y-%m-%d") with open(wallet) as fobj: balance = p.load(fobj) + amount with open(wallet, 'wb') as fobj: p.dump(balance, fobj) with open(record, 'a') as fobj: fobj.write( "%-12s%-8s%-8s%-10s%-20s " % ( date, 'N/A', amount, balance, comment ) ) def cost_money(wallet, record, amount, comment): date = time.strftime("%Y-%m-%d") with open(wallet) as fobj: balance = p.load(fobj) - amount if balance < 0: print "余额不足,请先存钱或进行其他操作!" else: with open(wallet, 'wb') as fobj: p.dump(balance, fobj) with open(record, 'a') as fobj: fobj.write( "%-12s%-8s%-8s%-10s%-20s " % ( date, amount, 'N/A', balance, comment ) ) def query_money(wallet, record): print "%-12s%-8s%-8s%-10s%-20s" % ( 'date', 'cost', 'save', 'balance', 'comment' ) with open(record) as fobj: for line in fobj: print line, with open(wallet) as fobj: print "New Balance: %s" % p.load(fobj) def show_menu(): w_file = 'wallet.data' r_file = 'record.txt' cmds = { '0': save_money, '1': cost_money, '2': query_money } prompt = """(0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): """ if not os.path.isfile(w_file): with open(w_file, 'w') as fobj: p.dump(0, fobj) if not os.path.isfile(r_file): os.mknod(r_file) while True: args = (w_file, r_file) choice = raw_input(prompt).strip()[0] if choice not in '0123': print "Invalid input, Try again." continue if choice in '01': amount = int(raw_input("Amount: ")) comment = raw_input("Comment: ") args = (w_file, r_file, amount, comment) if choice == '3': break cmds[choice](*args) if __name__ == '__main__': print show_menu()
•运行代码,测试效果
[root@localhost python]# python new_account.py (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment New Balance: 0 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 100 Comment: cost 100 余额不足,请先存钱或进行其他操作! (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 0 Amount: 100 Comment: save 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment 2017-01-06 N/A 100 100 save 100 New Balance: 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 101 Comment: cost 101 余额不足,请先存钱或进行其他操作! (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 1 Amount: 100 Comment: cost 100 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3): 2 date cost save balance comment 2017-01-06 N/A 100 100 save 100 2017-01-06 100 N/A 0 cost 100 New Balance: 0 (0) save money (1) spend money (2) query detail (3) quit Please input your choice(0/1/2/3):
*附录
1.如下是自己初次编写的代码,函数不具备通用性功能。
#!/usr/bin/env python #coding:utf8 import os,sys import time ''' 1.运行该脚本会生成一个balance.txt文件,并设置初始账户余额:¥10000 2.运行该脚本会生成一个account.txt文件,并记录账户消费信息详情。 ''' def save(): date = time.strftime("%Y-%m-%d") cost = 0 while 1: try: save = int(raw_input("请输入存款金额: ").strip()) except ValueError: print "