0
点赞
收藏
分享

微信扫一扫

Python语法进阶篇 - 78 - 综合实战- 抽奖系统之项目介绍、环境搭建、搭建项目基础类

万叶集
🎉 隐约雷鸣,阴霾天空。 🎉
🎉 但盼风雨来,能留你在此。 🎉

接下来就是 Python语法进阶篇 的重头戏 — > 综合实战,主题将会是一个通过命令行执行抽奖的操作。效果虽然简单,但是如果想要完美的展现出来,还需要更好的定义结构,接下来为大家介绍 “抽奖系统中的业务功能模块” 。


🐳 抽奖系统项目介绍

该系统一共有三个模块。

第一个模块:base 模块,即基础模块。

该模块实现的功能为:

第二个模块:admin 模块,这是给管理员的模块。

该模块实现的功能为:

知识点:

第三个功能模块: user 模块,主要实现用户的验证与抽奖操作

该模块的功能未:

知识点:

🐳 抽奖系统三大区域介绍


在这里插入图片描述


🐬 抽奖系统 - common 模块的功能介绍


在这里插入图片描述


🐬 抽奖系统 - storage 模块

严格意义来说,storage 其实并不是一个模块,而是一个文件夹。 这里会存放两个文件:user.jsongift.json


在这里插入图片描述


🐳 抽奖系统代码结构图


在这里插入图片描述


🐳 项目基础类 - 文件检查

接下来通过 base.py 来书写基础类,当前要实现的基本功能就是导入 user.json 与 gift.json 进行文件检查

base.py 基础类文件检查示例如下:

# coding:utf-8


"""
    1:导入 user.json ,文件检查
    2:导入 gift.json ,文件检查
"""


import os
from common import error


class Base(object):
    def __init__(self, user_json, gift_json):
        self.user_json = user_json
        self.gift_json = gift_json

        self.__check_user_json()
        self.__check_gift_json()

    def __check_user_json(self):
        if not os.path.exists(self.user_json):  # 判断文件地址路径是否存在
            raise error.NotPathError("not found {} ".format(self.user_json))

        if not self.user_json.endswith('.json'):   # 判断文件是否是 json 格式
            raise error.FormatError()

        if not os.path.isfile(self.user_json):     # 判断是否是文件
            raise error.NotFileError()

    def __check_gift_json(self):
        if not os.path.exists(self.gift_json):  # 判断文件地址路径是否存在
            raise error.NotPathError("not found {} ".format(self.gift_json))

        if not self.gift_json.endswith('.json'):  # 判断文件是否是 json 格式
            raise error.FormatError()

        if not os.path.isfile(self.gift_json):
            raise error.NotFileError()


if __name__ == '__main__':

    user_path = os.path.join(os.getcwd(), "storage", "user.json")
    gift_path = os.path.join(os.getcwd(), "storage", "gift.json")
    print(user_path)
    print(gift_path)

    base = Base(user_json=user_path, gift_json=gift_path)
    print(base)

common 模块error.py 脚本的代码如下:

# coding:utf-8


class NotPathError(Exception):		# 文件路径错误
    def __init__(self, message):
        self.message = message


class FormatError(Exception):		# 文件格式后缀错误
    def __init__(self, message="file need json format"):
        self.message = message


class NotFileError(Exception):		# 非文件错误
    def __init__(self, message="It's not file"):
        self.message = message

此时在 base.py 基础模块中我们发现, __check_user_json()__check_gift_json() 函数的基本功能是一样的,都是检查文件。这个时候就可以将其封装为一个公共函数 check_file 用以调用,所以我们可以在 utils.py 模块定义一个 check_file 函数。

utils.py 模块 check_file 函数示例如下:

# coding:utf-8


import os
from .error import NotPathError, NotFileError, FormatError


def check_file(path):
    if not os.path.exists(path):  # 判断文件地址路径是否存在
        raise NotPathError("not found {} ".format(path))

    if not path.endswith('.json'):  # 判断文件是否是 json 格式
        raise FormatError()

    if not os.path.isfile(path):  # 判断是否是文件
        raise NotFileError()

那么此时我们的 base.py 基础模块优化后的脚本代码就如下:

# coding:utf-8


"""
    1:导入 user.json ,文件检查
    2:导入 gift.json ,文件检查
"""


import os
from common.utils import check_file


class Base(object):
    def __init__(self, user_json, gift_json):
        self.user_json = user_json
        self.gift_json = gift_json

        self.__check_user_json()
        self.__check_gift_json()

    def __check_user_json(self):
        check_file(self.user_json)

    def __check_gift_json(self):
        check_file(self.gift_json)


if __name__ == '__main__':

    user_path = os.path.join(os.getcwd(), "storage", "user.json")
    gift_path = os.path.join(os.getcwd(), "storage", "gift.json")
    print(user_path)
    print(gift_path)

    base = Base(user_json=user_path, gift_json=gift_path)
    print(base)

执行结果如下:


在这里插入图片描述


尝试做几个文件异常的判断:


在这里插入图片描述


在这里插入图片描述


举报

相关推荐

0 条评论