既有适合小白学习的零基础资料,也有适合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&#