zoukankan      html  css  js  c++  java
  • 规范化目录

    规范化目录

    代码分类:加载快,代码可读性高,查询修改方便

    分析规范化目录

    1.start.py 启动项目  run()函数 存放在bin文件夹下
    2.settings.py 变量,静态路径,静态文件,配置文件。基本存放变量 存放在conf文件夹下
    3.src.py 存放主逻辑函数,核心函数 存放在core文件夹下
    4.common.py 公用组件,放置公用函数,功能业务需求 比如放装饰器 存放在lib文件夹下
    5.类似于register 用户信息,数据相关,多个文件,例如注册信息 存放在db文件夹下
    6.logging日志文件 记录用户的访问信息,操作记录等 存放在log文件夹下
    README说明介绍项目
    将以上文件存放在统一的客园文件下
    
    ![](https://img2018.cnblogs.com/blog/1730057/201908/1730057-20190812190624937-98260534.png)
    
    
    
    
    ```python
    配置start.py
    start.py
    import sys
    import os
    BASIC_PATH = os.path.dirname(os.path.dirname(__file__))#项目总文件夹路径(blog)
    sys.path.append(BASIC_PATH)#项目文件的路径,添加到自定义模块,其他文件都可以互相调用文件中功能
    from core.src import run 
    if __name__ == '__main__':#测试接口,只能在此文件下运行,在其他py文件下调用run不会执行
        run()
    
    配置settings.py
    import sys
    import os
    BASIC_PATH = os.path.dirname(os.path.dirname(__file__))
    REGISTER_PATH = os.path.join(BASIC_PATH,'bd','register')#路径拼接,为了获取动态路径 register是账号密码存放文件 文件名,静态路径,出现次数多
    
    配置common 公用的函数 装饰器
    from core import src
    def auth(func):
        def inner(*args, **kwargs):
            if src.status_dic['status']:##src.status_dic
                ret = func(*args, **kwargs)
                return ret
            else:
                print('33[1;31;0m 请先进行登录 33[0m')
                if src.login():##src.login
                    ret = func(*args, **kwargs)
                    return ret
        return inner
    
    配置src
    文件名和装饰器多要改
    from conf import settings
    from lib import common
    status_dic = {
        'username': None,
        'status': False,
    }
    flag = True
    def login():
        i = 0
        with open(settings.REGISTER_PATH, encoding='utf-8') as f1:
            dic = {i.strip().split('|')[0]: i.strip().split('|')[1] for i in f1}
        while i < 3:
            username = input('请输入用户名:').strip()
            password = input('请输入密码:').strip()
            if username in dic and dic[username] == password:
                print('登录成功')
                status_dic["username"] = True
                status_dic["status"] = True
                return True
            else:
                print('用户名密码错误,请重新登录')
                i += 1
    def register():
        with open(settings.REGISTER_PATH, encoding='utf-8') as f1:
            dic = {i.strip().split('|')[0]: i.strip().split('|')[1] for i in f1}
        while 1:
            print('33[1;45m 欢迎来到注册页面 33[0m')
            username = input('请输入用户名:').strip()
            if not username.isalnum():
                print('33[1;31;0m 用户名有非法字符,请重新输入 33[0m')
                continue
            if username in dic:
                print('33[1;31;0m 用户名已经存在,请重新输入 33[0m')
                continue
            password = input('请输入密码:').strip()
            if 6 <= len(password) <= 14:
                with open(settings.REGISTER_PATH, encoding='utf-8', mode='a') as f1:
                    f1.write(f'
    {username}|{password}')
                status_dic['username'] = str(username)
                status_dic['status'] = True
                print('33[1;32;0m 恭喜您,注册成功!已帮您成功登录~ 33[0m')
                return True
            else:
                print('33[1;31;0m 密码长度超出范围,请重新输入 33[0m')
    
    @common.auth
    def article():
        print(f'33[1;32;0m 欢迎{status_dic["username"]}访问文章页面33[0m')
        
    @common.auth
    def diary():
        print(f'33[1;32;0m 欢迎{status_dic["username"]}访问日记页面33[0m')
    
    @common.auth
    def comment():
        print(f'33[1;32;0m 欢迎{status_dic["username"]}访问评论页面33[0m')
    
    @common.auth
    def enshrine():
        print(f'33[1;32;0m 欢迎{status_dic["username"]}访问收藏页面33[0m')
    
    def login_out():
        status_dic['username'] = None
        status_dic['status'] = False
        print('33[1;32;0m 注销成功 33[0m')
    
    def exit_program():
        global flag
        flag = False
        return flag
    
    choice_dict = {
        1: login,
        2: register,
        3: article,
        4: diary,
        5: comment,
        6: enshrine,
        7: login_out,
        8: exit_program,
    }
    def run():
        while flag:
            print('''
            欢迎来到博客园首页
            1:请登录
            2:请注册
            3:文章页面
            4:日记页面
            5:评论页面
            6:收藏页面
            7:注销
            8:退出程序''')
            choice = input('请输入您选择的序号:').strip()
            if choice.isdigit():
                choice = int(choice)
                if 0 < choice <= len(choice_dict):
                    choice_dict[choice]()
                else:
                    print('33[1;31;0m 您输入的超出范围,请重新输入 33[0m')
            else:
                print('33[1;31;0m 您您输入的选项有非法字符,请重新输入 33[0m')
    
  • 相关阅读:
    ZooKeeperACL机制
    windows结束端口对应的进程
    facenet模型训练
    sourcetree git合并问题
    人脸识别学习
    爬虫 第八天
    WCF nginx反向代理遇到的问题
    WPF WindowChrome 自定义窗口
    WPF svg 转 xmal
    WPF MVVM笔记
  • 原文地址:https://www.cnblogs.com/tangjian219/p/11341971.html
Copyright © 2011-2022 走看看