再将之前我们首先需要了解一下软件开发目录的规范:
开发基本目录
2.定制程序入口
1、要在start.py处要把绝对路径写出来
import sys,os # 应该把项目的根目录添加到环境变量中 # sys.path.append(r‘D:\code\SH_fullstack_s1\day15\ATM‘)
先取一个跟目录,然后再它根目录下面找
# 拿到ATM所在的文件夹 BASE_DIR=os.path.dirname(os.path.dirname(__file__))
#os.path.dirname(__file__)
#这是找根目录的路径,里面再加一次就是找到的是绝对路径
sys.path.append(BASE_DIR)
#在添加一下这个路径
from core import src
src.run()
2、设置settings路径
DB_PATH=r‘D:\code\SH_fullstack_s1\day15\ATM\db\db.txt‘
3、core编写代码:要导入conf中的setting路径和lib中的common的功能
def login(): print(‘登录....‘) with open(settings.DB_PATH,encoding=‘utf-8‘) as f: for line in f: print(line) def register(): print(‘注册....‘) def shop(): print(‘购物....‘) def pay(): print(‘支付...‘) def transter(): print(‘转账...‘) # common.logger(‘刘清政给他爹egon转账10000‘) # logger1.debug(‘刘清政给他爹egon转账10000‘) # logger1.error(‘刘清政给他爹egon转账10000,转账失败‘) logger2.error(‘刘清政给他爹egon转账10000,转账失败‘) def run(): while True: print(""" 1 登录 2 注册 3 购物 4 支付 5 转账 """) choice=input(‘>>: ‘).strip() if choice == ‘1‘: login() elif choice == ‘2‘: register() elif choice == ‘3‘: shop() elif choice == ‘4‘: pay() elif choice == ‘5‘: transter() else: print(‘输入错误指令‘)
4、编写常用的common功能模块
#from conf import settings # # def logger(msg): # with open(settings.LOG_PATH,‘a‘,encoding=‘utf-8‘) as f: # f.write(‘%s\n‘ %msg) #要导入 import logging.config import logging from conf import settings def get_logger(name): #name=‘atm‘ logging.config.dictConfig(settings.LOGGING_DIC) # 导入上面定义的logging配置 l1=logging.getLogger(name) return l1