真正的後端開發的項目,系統等,少則幾萬行代碼,多則十幾萬,幾十萬行代碼python
軟件開發,規範你的項目目錄結構,代碼規範,遵循PEP8規範等等,讓你更加清晰,合理開發。數據庫
1.代碼分類後端
加載快app
可讀性高函數
查詢修改方便3d
1.規劃固定的路徑日誌
把文件路徑換成常量,至關於讀取數據庫的地址代碼規範
2.settings.py文件code
配置文件orm
3.src.py主邏輯核心函數文件
4.common.py公共組建部分
5.start.py文件
6.register用戶數據相關,多個文件
存放用戶用戶註冊的文件
7.logging日誌文件:記錄用戶的訪問次數,用戶的動態信息
8.README 記錄項目是作什麼的
start.py文件
import sys import os #獲取項目的主目錄 # BASE_PATH =os.path.dirname(os.path.dirname(__file__)) BASE_PATH= "../../博客園" sys.path.append(BASE_PATH) from core.src import run if __name__ == '__main__': run()
settings.py文件
import os # BASE_PATH = os.path.dirname(os.path.dirname(__file__)) REGISTER_PATH = os.path.join("../", 'db', 'register') LOG_PATH = os.path.join("../", 'log', 'access.log')
src.py文件
from conf import settings from lib import common msg = """ 1.請登陸 2.請註冊 3.進入文章頁面 4.進入評論頁面 5.進入日記頁面 6.進入收藏頁面 7.註銷帳號 8.退出整個程序 >>> """ login_dic = { "username":None, "flag":False, "count":3 } def register(): name = input("請輸入要註冊的用戶名") pwd = input("請輸入密碼") with open(settings.REGISTER_PATH, "a", encoding="utf-8") as f: if name.isalnum() and 6 < len(pwd) < 14: with open(settings.REGISTER_PATH, "a+", encoding="utf-8") as f: f.seek(0) for i in f: if name in i.strip().split(":"): print("用戶名已存在") break else: print("註冊成功") f.write(f"{name}:{pwd}\n") else: print("用戶名或密碼輸入格式錯誤!") return f"{name}" def login(func=False): while login_dic["count"]: user = input("username:") pwd = input("password:") with open(settings.REGISTER_PATH, "r", encoding="utf-8") as u: for i in u: k, v = i.strip().split(":") if user == k and pwd == v: login_dic["username"] = user login_dic["flag"] = True login_dic["count"] = 0 print("登陸成功!") if func: func() else: login_dic["count"] -= 1 print(f"用戶名或密碼錯誤!剩餘次數{login_dic['count']}") common.logger.warning('logger warning message') @common.auth def article(): print("這是文章") @common.auth def comment(): print("這是評論") @common.auth def log(): print("這是日記") @common.auth def collect(): print("這是收藏") @common.auth def out(): login_dic["username"] = None login_dic["flag"] = False print("退出成功!") func_dic = { "1":login, "2":register, "3":article, "4":comment, "5":log, "6":collect, "7":out, "8":exit, } def run(): while True: chose = input(msg) if chose in func_dic: login_dic["count"] = 3 func_dic[chose]() else: print("請正確輸入內容!")
common.py
from core import src from conf import settings def auth(func): def inner(*args,**kwargs): if src.login_dic["flag"]: func() else: src.login(func) return inner import logging logger = logging.getLogger() # 建立一個logger fh = logging.FileHandler(settings.LOG_PATH,mode="a",encoding='utf-8') # 文件 ch = logging.StreamHandler() # 屏幕 formatter = logging.Formatter('%(asctime)s - %(name)s - %(filename)s - [line:%(lineno)d] - %(levelname)s - %(message)s') # 將屏幕和文件都是用以上格式 logger.setLevel(logging.DEBUG) # 設置記錄級別 fh.setFormatter(formatter) # 使用自定義的格式化內容 ch.setFormatter(formatter) logger.addHandler(fh) #logger對象能夠添加多個fh和ch對象 logger.addHandler(ch) #打印在屏幕上