ATM目录
start.py
import os,sys from core import src base_path=os.path.dirname(os.path.dirname(__file__)) sys.path.append(base_path) if __name__=='__main__': src.run()
配置文件setting.py
import os base_path=os.path.dirname(os.path.dirname(__file__)) DB=os.path.join(base_path,'db') BASE_LOG_LOCAL=os.path.join(base_path,'log') LOG_PATH=r'%s\log\user.log'%base_path ADMIN_LOG_PATH=r'%s\log\admin.log'%base_path # 定义三种日志输出格式 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字 simple_format = '[task_id:%(name)s][%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' id_simple_format = '[task_id:%(name)s][%(levelname)s][%(asctime)s] %(message)s' # log配置字典 LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': simple_format }, 'id_simple':{ 'format':id_simple_format } }, 'filters': {}, 'handlers': { 'stream': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'simple' }, 'user': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'standard', 'filename': LOG_PATH, 'maxBytes': 1024*1024*5, 'backupCount': 5, 'encoding': 'utf-8', }, 'admin': { 'level': 'DEBUG', 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'id_simple', 'filename': ADMIN_LOG_PATH, 'maxBytes': 1024*1024*5, 'backupCount': 5, 'encoding': 'utf-8', }, }, 'loggers': { '': { 'handlers': ['user', 'stream','user','admin'], 'level': 'DEBUG', 'propagate': False, }, }, }
主体函数src.py、admin.py、user.py
src.py
from core import user,admin user_dic={ '1':admin.run, '2':user.run, } def run(): while True: print(''' 1、管理员 2、用户 ''') choice=input('>>>>:').strip() if choice=='8':break if choice not in user_dic:continue user_dic[choice]()
admin.py
from lib import common data={'name':None} def login(): if data['name']: print('已经登录') return while True: name=input('账号').strip() pwd=input('密码').strip() flog,msg=admin_interface.login(name,pwd) if flog: print(msg) data['name']=name break else: print(msg) #解冻信用卡 @common.login_auth(auth_name='admin') def thaw(): while True: name=input('请输入要解冻用户的名字或输入q退出').strip() if name=='q':break user_dic=user_interface.select(name) if not user_dic: print('用户不存在') continue if user_dic['card_lick']: print('用户未被冻结') continue admin_interface.card_thaw(user_dic) print('信用卡解冻成功') #冻结信用卡 @common.login_auth(auth_name='admin') def card_lick(): while True: name = input('请输入要冻结用户的名字或输入q退出').strip() if name == 'q': break user_dic=user_interface.select(name) if not user_dic: print('用户不存在') continue if not user_dic['card_lick']: print('信用卡已被冻结') continue admin_interface.card_lick(user_dic) print('冻结成功') #解除用户锁定 @common.login_auth(auth_name='admin') def thaw_user(): while True: name = input('请输入要锁定用户的名字或输入q退出').strip() if name == 'q': break user_dic=user_interface.select(name) if not user_dic: print('用户不存在') continue if user_dic['user_lick']: print('用户未被锁定') continue admin_interface.user_thaw(user_dic) print('解除锁定成功') #锁定用户 @common.login_auth(auth_name='admin') def user_lick(): while True: name = input('请输入要解锁用户的名字或输入q退出').strip() if name == 'q': break user_dic=user_interface.select(name) if not user_dic: print('用户不存在') continue if not user_dic['card_lick']: print('用户已被锁定') continue admin_interface.user_lick(user_dic) print('锁定成功') user_dic={ '1':login, '2':thaw, '3':card_lick, '4':thaw_user, '5':user_lick } def run(): while True: print(''' 1、登录 2、解冻信用卡 3、冻结信用卡 4、解除用户锁定 5、锁定用户 6、退出 ''') choice=input('>>>>:').strip() if choice=='6':break if choice not in user_dic:continue user_dic[choice]()
user.py
from interface import bank_interface,user_interface from lib import common data={'name':None} shops=[{'name':'至尊宝','price':5000}, {'name':'韦小宝','price':200}, {'name':'宋小宝','price':100}, {'name':'唐玉小宝','price':1000}] def register(): while True: name=input('输入姓名或输入q退出:').strip() if name=='q':break user=user_interface.select(name) if not user: pwd=input('密码').strip() pwd2=input('再次输入密码').strip() if pwd==pwd2: user_interface.update(name,pwd) print('注册成功') break else: print('两次密码不一样') else: print('用户已存在') # register() def login(): if data['name']: print('你已经登陆过') return count=1 while True: name=input('输入姓名或输入q退出:').strip() if name == 'q': break if not name:continue user_dic=user_interface.select(name) if not user_dic: print('用户名不存在') continue if not user_dic['user_lick']: print('被锁定,请联系管理员') return pwd=input('密码').strip() if pwd==user_dic['pwd']: print('登陆成功') data['name']=name break else: if count<3: print('密码错误,请重新输入') count+=1 continue else: print('密码错误次数超过三次,被锁定') user_interface.login(user_dic) break # login() #还款 @common.login_auth(auth_name='user') def repayment(): user_dic=user_interface.select(data['name']) if user_dic['bal'] >= 15000:print('无欠款') else: print('你需要还%s'%(15000-user_dic['bal'])) while True: money=input('请输入还款金额或输入q退出:') if money=='q':break if money.isdigit(): bank_interface.repayment(user_dic,int(money)) print('还款%s¥'%money) else: print('金额必须是数字') # repayment() #流水 @common.login_auth(auth_name='user') def detailed_list(): user_dic=user_interface.select(data['name']) bank_interface.detailed_list(user_dic) #取现 @common.login_auth(auth_name='user') def loan(): user_dic=user_interface.select(data['name']) if not user_dic['card_lick']: print('你的信用卡账户被冻结,无法取现') return while True: money=input('输入取现金额或输入q退出').strip() if money=='q':break if not money.isdigit(): print('金额必须是数字') continue else: flog,msg=bank_interface.loan(user_dic,int(money)) if flog: print(msg) else: print(msg) #购物 @common.login_auth(auth_name='user') def shopping(): while True: user_dic = user_interface.select(data['name']) print(''' 编号 商品名 价格 1 至尊宝 5000 2 韦小宝 200 3 宋小宝 100 4 唐玉小宝 1000 ''') choice=input('输入编号或输入q退出').strip() if choice=='q':break choice=int(choice)-1 if choice<=len(shops): if not user_dic['card_lick']: print('你的信用卡账户被冻结,无法购买') return flog,msg=bank_interface.shopping(user_dic,shops[choice]) if flog: print(msg) else: print(msg) #转账 @common.login_auth(auth_name='user') def transfer_accounts(): while True: user_dic=user_interface.select(data['name']) if not user_dic['card_lick']: print('你的信用卡账户被冻结,无法转账') return to_user_name=input('输入对方的名字或q退出').strip() if to_user_name=='q':break if to_user_name==data['name']: print('不能转给自己') continue to_user_dic=user_interface.select(to_user_name) if not to_user_dic: print('该用户不存在') continue money=input('转账金额').strip() if not money.isdigit(): print('金额必须是数字') continue flog,msg=bank_interface.transfer_accounts(user_dic,to_user_dic,int(money)) if flog: print(msg) else: print(msg) user_dic={ '1':register, '2':login, '3':repayment, '4':detailed_list, '5':loan, '6':shopping, '7':transfer_accounts } def run(): while True: print(''' 1、注册 2、登录 3、还款 4、信用卡清单 5、取现 6、购物 7、转账 8、退出 ''') choice=input('>>>>:').strip() if choice=='8':break if choice not in user_dic:continue user_dic[choice]()
接口:admin_interface.py,bank_interface.py,user_interface.py
admin_interface.py
from db import db_handler from lib import common logger_admin=common.get_logger('admin') def login(name,pwd): admin = db_handler.select(name) if admin: if pwd==admin['pwd']: return True,'登陆成功' else: return False,'密码错误' else: return False,'用户不存在' def card_lick(user_dic): user_dic['card_lick']=False db_handler.update(user_dic) logger_admin.info('管理员冻结了%s的信用卡'%user_dic['name']) def card_thaw(user_dic): user_dic['card_lick']=True db_handler.update(user_dic) logger_admin.info('管理员解冻了%s的信用卡' % user_dic['name']) def user_lick(user_dic): user_dic['user_lick']=False db_handler.update(user_dic) logger_admin.info('管理员锁定了%s的账号' % user_dic['name']) def user_thaw(user_dic): user_dic['user_lick']=True db_handler.update(user_dic) logger_admin.info('管理员解锁了%s的账号' % user_dic['name'])
bank_interface.py
from db import db_handler from lib import common from lib import common logger_user=common.get_logger('admin') def repayment(user_dic,money): user_dic['bal']+=money print(user_dic['bal']) user_dic['ls'].append('你还款%s'%(money)) db_handler.update(user_dic) logger_user.debug('%s还款%s' % (user_dic['name'],money)) def detailed_list(user_dic): if not user_dic['ls'] :print('你还没有消费过') for line in user_dic['ls']:print(line) def loan(user_dic,money): if user_dic['bal']<money: return False,'你的额度不够' user_dic['bal']-=money*1.05 user_dic['ls'].append('你取现%s' % money) db_handler.update(user_dic) logger_user.debug('%s取现%s' % (user_dic['name'], money)) return True,'取现成功' def transfer_accounts(user_dic,to_user_dic,money): if user_dic['bal']<money: return False,'你的额度不够' user_dic['bal']-=money user_dic['ls'].append('你给%s转了%s¥'%(to_user_dic['name'],money)) db_handler.update(user_dic) to_user_dic['bal']+=money to_user_dic['ls'].append('你收到%s转账%s¥'%(user_dic['name'],money)) db_handler.update(to_user_dic) logger_user.debug('%s转账给%s%s¥' % (user_dic['name'],to_user_dic['name'], money)) return True,'转账成功' def shopping(user_dic,shop): if user_dic['bal']<shop['price']: return False,'你的额度不够' user_dic['bal']-=shop['price'] user_dic['ls'].append('你购买%s花了%s¥' % (shop['name'],shop['price'])) db_handler.update(user_dic) logger_user.info('%s在商城购买了%s花了%s¥' % (user_dic['name'], shop['name'], shop['price'])) return True,'购买成功'
user_interface.py
from db import db_handler def select(name): return db_handler.select(name) def update(name,pwd): user_dic={'name':name,'pwd':pwd,'card_lick':True,'user_lick':True,'bal':15000,'imin':15000,'ls':[]} db_handler.update(user_dic) def login(user_dic): user_dic['user_lick'] = False db_handler.update(user_dic)
装饰器以及常用功能common.py
from conf import setting import logging.config def login_auth(auth_name): from core import user, admin def login(func): def wrapper(*args,**kwargs): if auth_name=='user': if not user.data['name']: print('请先登录') user.login() else: return func(*args, **kwargs) if auth_name=='admin': if not admin.data['name']: print('请先登录') admin.login() else: return func(*args, **kwargs) return wrapper return login def get_logger(name): logging.config.dictConfig(setting.LOGGING_DIC) # 导入上面定义的logging配置 logger = logging.getLogger(name) # 生成一个log实例 return logger
来源:https://www.cnblogs.com/songxuexiang/p/8919540.html