程序框图 (消费模块暂未写入)
bin:程序执行
1 import os 2 import sys 3 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 4 print(base_dir) 5 sys.path.append(base_dir) 6 7 from core import main 8 9 10 if __name__ == '__main__': #当作为脚本直接运行的时候,此时__name__等于__main__,当作为模块导入的时候,__name__为文件名但不带.py,故不运行if后语句。 11 main.run()
config:配置文件
1 import os 2 import sys 3 import logging 4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 6 7 DATABASE = { 8 'engine': 'file_storage', #support mysql,postgresql in the future 9 'name':'accounts', 10 'path': "%s/db" % BASE_DIR 11 } 12 13 14 LOG_LEVEL = logging.INFO 15 LOG_TYPES = { 16 'transaction': 'transactions.log', 17 'access': 'access.log', 18 '11111':'11111.log' 19 } 20 21 TRANSACTION_TYPE = { 22 'repay':{'action':'plus', 'interest':0}, 23 'withdraw':{'action':'minus', 'interest':0.05}, 24 'transfer':{'action':'minus', 'interest':0.05}, 25 'consume':{'action':'minus', 'interest':0}, 26 }
core:程序主要代码
1 import json 2 import time 3 from core import db_handler 4 from conf import settings 5 6 7 def load_current_balance(account_id): 8 ''' 9 return account balance and other basic info 10 :param account_id: 11 :return: 12 ''' 13 db_path = db_handler.db_handler(settings.DATABASE) 14 account_file = "%s/%s.json" %(db_path,account_id) 15 with open(account_file) as f: 16 acc_data = json.load(f) 17 return acc_data 18 def dump_account(account_data): 19 ''' 20 after updated transaction or account data , dump it back to file db 21 :param account_data: 22 :return: 23 ''' 24 db_path = db_handler.db_handler(settings.DATABASE) 25 account_file = "%s/%s.json" %(db_path,account_data['id']) 26 with open(account_file, 'w') as f: 27 acc_data = json.dump(account_data,f) 28 29 return True
1 import os 2 from core import db_handler 3 from conf import settings 4 from core import logger 5 import json 6 import time 7 8 def acc_auth(account,password): 9 ''' 10 account auth func 11 :param account: credit account number 12 :param password: credit card password 13 :return: if passed the authentication , retun the account object, otherwise ,return None 14 ''' 15 db_path = db_handler.db_handler(settings.DATABASE) 16 account_file = "%s/%s.json" %(db_path,account) 17 print(account_file) #base_dir + accounts + account.json 18 if os.path.isfile(account_file): #判断文件名是否存在,存在执行下面语句 19 with open(account_file,'r') as f: 20 account_data = json.load(f) 21 if account_data['password'] == password: 22 exp_time_stamp = time.mktime(time.strptime(account_data['expire_date'], "%Y-%m-%d")) 23 if time.time() >exp_time_stamp: 24 print("