標籤:color style data strftime ogg hdr ftime func settings
目錄結構:
Package|------bin |------start.py|------conf |------settings.py|------core |------src.py|------db |------table.db|------lib |------common.py|------log |------transaction.log|------README
1.在src中編寫核心代碼
from lib import commondef shopping(): print(‘購物‘)def pay(): print() common.logger(‘XXXXXXXXXX‘)def transfer(): print()def withdraw(): print(‘提現‘)func_dic={ ‘1‘: shopping, ‘2‘: pay, ‘3‘: transfer, ‘4‘: withdraw}#讓使用者選擇啟動哪個方法def run(): while True: print(""" 0 退出 1 購物 2 支付 3 轉賬 4 提現 """) choice =input(‘請輸入您的操作:‘).strip() if choice == ‘0‘:break if choice not in func_dic: print("輸入的指令不存在,請重新輸入") continue func_dic[choice]()
2.bin/start為程式啟動入口
os.path.dirname(os.path.dirname(__file__))================>動態擷取軟體的根目錄,解決移植問題
import sysimport osfrom core import srcBASE_DIR = os.path.dirname(os.path.dirname(__file__))sys.path.append(BASE_DIR)if __name__ == ‘__main__‘: src.run()
3.conf/settings.py中設定日誌路徑(以及資料庫路徑)
同樣是動態擷取軟體的根目錄,再拼接到軟體日誌的指定目錄,解決移植問題
import osBASE_DIR=os.path.dirname(os.path.dirname(__file__))TRANSACTION_LOG_PATH =os.path.join(BASE_DIR, ‘log‘, ‘transaction.log‘)
4.lib/common.py中放通用的方法
from conf import settingsimport timedef logger(msg): with open(settings.TRANSACTION_LOG_PATH,‘at‘,encoding=‘utf-8‘) as f: f.write(‘%s %s\n‘ %(time.strftime(‘%Y-%m-%d %H:%M:%S‘), msg))
python學習_軟體開發的目錄規範以及範例程式碼(解決軟體移植的路徑問題)