python學習_軟體開發的目錄規範以及範例程式碼(解決軟體移植的路徑問題)

來源:互聯網
上載者:User

標籤: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學習_軟體開發的目錄規範以及範例程式碼(解決軟體移植的路徑問題)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.