1. 程式人生 > 程式設計 >PHP之mysql位運算案例講解

PHP之mysql位運算案例講解

承接上文軟體開發目錄規範

**軟體開發目錄規範(僅供參考,沒有統一標準)**

假設我要開發一個叫ATM軟體
ATM為頂級目錄名字
# 執行程式放在bin(binary)下面
# corf 配置檔案——》定製化的檔案(比如產生的日誌放在一個路徑下面)
# core 核心程式碼 ,被匯入的模組丟這裡
# lib  共享一些功能 庫(模組),自定義的庫,存放功能的倉庫
# logs 專門存放日誌的資料夾
# readme 說明手冊

bin目錄下start.py檔案(未改動)

執行程式放在bin下面
import sys
sys.path.append(r'D:\PycharmProject\s14\week03\day05分支\ATM')

from core.src import run

if __name__ == '__main__':
    run()

conf目錄下settings.py(#內容——>原檔案內容)

**知識點**
# BASE_DIR 返回路徑中的目錄部分
# print(__file__) 當前檔案的絕對路徑
# os.path.dirname(__file__)  返回指令碼路徑

**舉例說明**

內容
import os  #匯入標準庫os(作業系統介面模組)
1. print(__file__)
2. print(os.path.dirname(__file__))
3. print( os.path.dirname(os.path.dirname(__file__)))

對應結果:
1. D:\PycharmProject\ATM\conf\settings.py
2. D:\PycharmProject\ATM\conf
3. D:\PycharmProject\ATM
# # 配置檔案——》定製化的檔案(比如產生的日誌放在一個路徑下面)
#
#
# import os
#
# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
#
# LOG_PATH = r'%s\logs\access.log' % BASE_DIR
#
# # 配置路徑寫活了

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

LOG_PATH = r'%s\logs\access.log' % BASE_DIR

LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        '格式1': {
            'format': '%(asctime)s %(name)s %(filename)s:%(lineno)d %(levelname)s:%(message)s'
        },
        '格式2': {
            'format': '%(asctime)s :%(message)s'
        },
    },
    'filters': {},
    'handlers': {
        '螢幕': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 列印到螢幕
            'formatter': '格式2'
        },
        '檔案': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',  # 儲存到檔案
            'formatter': '格式1',
            'filename': LOG_PATH,
            'encoding': 'utf-8',
        },
    },
    'loggers': {
         '交易日誌': {
            'handlers': ['螢幕','檔案'],
            'level': 'DEBUG',
            'propagate': False,
        },

        '': {
            'handlers': ['螢幕','檔案'],
            'level': 'DEBUG',
            'propagate': False,
        },

    },
}

core目錄下src.py(#內容——>原檔案內容)

#  core 核心程式碼 ,被匯入的模組丟這裡
# from lib.common import log
#
#
# def logout():
#     print("退出".center(50,'*'))
#     exit()
#
# def login():
#     print('登入'.center(50,'*'))
#     log("egon剛剛登入了")
#
# def register():
#     print('註冊'.center(50,'*'))
#
# def transfer():
#     print('轉賬'.center(50,'*'))
#     log("egon剛剛給劉sir轉了10個億")
#
# def withdraw():
#     print('提現'.center(50,'*'))
#
# def pay():
#     print('支付'.center(50,'*'))
#
#
# func_dic = {
#     '0': ['退出',logout],
#     '1': ['登入',login],
#     '2': ['註冊',register],
#     '3': ['轉賬',transfer],
#     '4': ['提現',withdraw],
#     '5': ['支付',pay],
# }
#
# def run():
#     while True:
#         for k in func_dic:
#             print(k,func_dic[k][0])
#
#         choice = input("請輸入指令編號: ").strip()
#
#         if choice in func_dic:
#             func_dic[choice][1]()
#         else:
#             print("輸入的指令錯誤")

from lib.common import log

def logout():
    print("退出".center(50,'*'))
    exit()

def login():
    print('登入'.center(50,'*'))
    log("egon剛剛登入了","登入日誌","info")
def register():
    print('註冊'.center(50,'*'))

def transfer():
    print('轉賬'.center(50,'*'))
    log("egon剛剛給劉sir轉了10個億","交易日誌",'info')

def withdraw():
    print('提現'.center(50,'*'))
    log("egon提現了10個億","交易日誌",'info')

def pay():
    print('支付'.center(50,'*'))
    log('egon支出失敗',"交易日誌",'error')

func_dic = {
    '0': ['退出',logout],
    '1': ['登入',login],
    '2': ['註冊',register],
    '3': ['轉賬',transfer],
    '4': ['提現',withdraw],
    '5': ['支付',pay],
}

def run():
    while True:
        for k in func_dic:
            print(k,func_dic[k][0])

        choice = input("請輸入指令編號: ").strip()

        if choice in func_dic:
            func_dic[choice][1]()
        else:
            print("輸入的指令錯誤")

lib目錄下common.py(#內容——>原檔案內容)

# import time
# from conf import settings
#
# def log(msg):
#     with open(r'%s' % settings.LOG_PATH,mode='at',encoding='utf-8') as f:
#         f.write("%s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"),msg))
#
#  日誌路徑換成 配置檔案可定製
#

import time
import logging.config
from conf import settings

logging.config.dictConfig(settings.LOGGING_DIC)

def log(msg,name,level='info'):
    logger = logging.getLogger(name)
    if level == 'info':
        logger.info(msg)
    elif level == 'warn':
        logger.warning(msg)
    elif level == 'error':
        logger.error(msg)
    elif level == 'critical':
        logger.critical(msg)