【腾讯云 Cloud Studio 实战训练营】基于Python实现的快速抽奖系统

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

那么此时我们的 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)

执行结果如下:


在这里插入图片描述


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


在这里插入图片描述


⭐️ base用户相关功能实现

base.py 模块基础上,增加 __read_users()__write_user() 函数。

所以接下来,我们就实现一下前三个目标。

# coding:utf-8


"""
 1:导入 user.json ,文件检查
 2:导入 gift.json ,文件检查
 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
 3、确定用户表中每个用户的信息字段
 4、读取 `user.json` 文件
 5、写入 `user.json` 文件(检测该用户是否存在),存在则不可写入
"""


import os
import json
import time

from common.utils import check_file
from common.error import UserExistsError


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):    # 调用 utils 模块的公共函数 check\_file 检查 user.json 文件
        check_file(self.user_json)

    def \_\_check\_gift\_json(self):    # 调用 utils 模块的公共函数 check\_file 检查 gift.json 文件
        check_file(self.gift_json)

    def \_\_read\_user(self):          # 读取 user.json 文件
        with open(self.user_json) as f:
            data = json.loads(f.read())
        return data

    def \_\_write\_user(self, \*\*user):     # 写入用户信息(进行写入时的判断)
        if "username" not in user:
            raise ValueError("missing username")    # 缺少 username 信息
        if "role" not in user:
            raise ValueError("missing role")        # 缺少角色信息(缺少权限信息)

        user['active'] = True       # 初始化用户基础信息
        user['create\_time&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值