Codes of learning python

The first chapter Why have standardized directory

The real back-end development projects, systems, ranging from tens of thousands of lines of code, as many as hundreds of thousands, hundreds of thousands of lines of code

Software development, specification of your project directory structure, code specifications, follow PEP8 norms, etc., make you more clear and reasonable development.

1. Classification Code

Load fast

High readability

Easy to modify the query

The second chapter analyzes standardized directory

1. Plan a fixed path

The file path into constant, the equivalent of an address read from the database

2.settings.py file

Profiles

3.src.py main logic core function file

4.common.py form part of the public

5.start.py file

6.register user data related to multiple files

Registered user to store user files

7.logging log file: Record number of visits of the user, the user's dynamic information

8.README recording project is doing what

Chapter III practical exercise

start.py file

import sys
import os

#获取项目的主目录
BASE_PATH =os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
from core.src import run
if __name__ == '__main__':

    run()

settings.py file

import sys
import os
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
REGISTER_PATH = os.path.join(BASE_PATH, 'db', 'register')

src.py file

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.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("请正确输入内容!")

Guess you like

Origin www.cnblogs.com/zdqc/p/11284404.html