readme
分析:
1 本周作业 2 3 角色:学校、学员、课程、讲师 4 要求: 5 1. 创建北京、上海 2 所学校 6 2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开 7 3. 课程包含,周期,价格,通过学校创建课程 8 4. 通过学校创建班级, 班级关联课程、讲师 9 5. 创建学员时,选择学校,关联班级 10 5. 创建讲师角色时要关联学校, 11 6. 提供两个角色接口 12 6.1 学员视图, 可以注册, 交学费, 选择班级, 13 6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩 14 6.3 管理视图,创建讲师, 创建班级,创建课程 15 16 7. 上面的操作产生的数据都通过pickle序列化保存到文件里 17 18 角色:学校、学员、课程、讲师 19 要求: 20 21 创建北京、上海 2 所学校 22 23 学校 24 25 属性:名字 地址 电话 课程列表 (目前可以将课程存到学校中 后期要反过来 在课程中存储学校信息) 26 27 行为: 创建课程 28 29 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开 30 31 课程 32 33 属性:课程名称 价格 周期 学校名称 34 35 行为: 36 37 课程包含,周期,价格,通过学校创建课程 38 39 40 41 通过学校创建班级, 班级关联课程、讲师 42 43 班级 44 45 属性:班级名称 课程名称 老师名称 46 47 行为: 48 49 50 51 创建学员时,选择学校,关联班级 52 53 学生 54 55 属性:姓名,性别,年龄 学校名称 班级名称 56 57 行为:选择校区 选择课程 加入班级 交学费 注册 58 59 60 61 创建讲师角色时要关联学校, 62 63 讲师 64 65 属性:姓名年龄,性别,学校名称 66 67 行为: 修改学生成绩 选择班级 查看班级学员列表 68 69 70 71 72 73 提供三个角色接口 74 75 学员: 登录 注册 选课 交学费 查看自己的分数 76 77 老师: 登录, 选择班级 , 修改学员成绩 查看学员信息 78 79 管理: 登录,注册, 创建校区,创建讲师,创建课程,创建班级 80 81 82 83 84 85 管理员: 86 87 属性: 账号 密码 88 89 行为:创建学校,创建班级,创建讲师, 创建课程 90 91 92 93 上面的操作产生的数据都通过pickle序列化保存到文件里 94 95 pickle 可以直接存储任何类型对象 96 97 需求分析 : 98 99 明确要做的程序 具备哪些功能,并分析 每个功能是否可以用代码实现, 产生需求文档 100 101 技术选型: 102 103 1.要使用的技术是否已经成熟 (使用的人多,有完整官文档,还有相对活跃的社区) 104 105 需要考虑开发团队是否能够hold住该技术 106 107 108 109 面向对象 还是 面向过程 110 111 如果项目体积较大,后续可能需要修改需求或是增加需求,优先考虑 面向对象 112 113 114 115 1.分析需求 确定需要哪些对象和类 ,分别有哪些特征和行为 116 117 三层: 118 119 用户层V 接口层C 数据访问层M 120 121 用户层 :与用户交互 输出数据 或者接收输入 122 123 接口层: 1.判断 验证 组装 数据 交给给数据访问层 来存储 2.从数据访问层拿到数据 组装成用户可以看懂的格式 交给用户层 124 125 数据访问: 提供数据的读取和存储 126 127 128 129 model 模型 就是一个对象 专门用于描述数据 相当于数据容器 130 131 需要的文件应该放在那一层 132 133 与用户直接打交道的 例如请输入功能序号 放到用户层 134 135 user_view 136 137 teacher_view 138 139 admin_view 140 141 一堆类 例如学生 课程 等 放在数据访问层 142 143 接口层: 144 145 user_interface 146 147 teacher_interface 148 149 admin_interface 150 151 数据访问层: 152 153 DBHandler 154 155 标准文件目录: 156 bin 157 158 start.py 159 160 conf 161 162 settings.py 163 164 lib 165 166 common.py 167 168 core 169 170 user_view 171 172 teacher_view 173 174 admin_view 175 176 db 177 178 DBHandler 179 180 models 181 182 interface 183 184 user_interface 185 186 teacher_interface 187 188 admin_interface 189 190 logs 191 192 一堆日志文件 193 194 195 196 从用户层开始写 功能选择 197 198 用户的登录 注册 199 200 先后顺序 不是必须的 201 202 管理员 203 204 学生 205 206 老师
执行文件
bin
start.py
1 import os 2 import sys 3 4 # 添加环境变量 5 sys.path.append(os.path.dirname(os.path.dirname(__file__))) 6 7 # 导模块 8 from core import src 9 10 # 自运行 11 if __name__ == '__main__': 12 src.run()
用户功能层
core
admin.py
1 from interface import common_interface,admin_interface 2 from lib import common 3 # 1.注册 4 def register(): 5 while True: 6 name = input("admin_name>>>:").strip() 7 type = 'admin' 8 flag = common_interface.select_name(type,name) 9 if flag: 10 print('账号已存在,请重新输入') 11 else: 12 pwd = input("pwd>>>:").strip() 13 re_pwd = input("re_pwd>>>:").strip() 14 if pwd and re_pwd: 15 if pwd == re_pwd: 16 # 调用接口层 17 msg = common_interface.register_interface(type,name,pwd) 18 if msg: 19 print(msg) 20 break 21 else: 22 print("两次密码不一致") 23 else: 24 print("密码和验证密码不可为空") 25 26 # 2.登录 27 def login(): 28 while True: 29 name = input("admin_name>>>:").strip() 30 type = 'admin' 31 flag = common_interface.select_name(type,name) 32 if flag: 33 print('账号输入正确,请输入密码') 34 pwd = input("pwd>>>:").strip() 35 if pwd: 36 # 调用接口层 37 flag,msg = common_interface.login_interface(type, name, pwd) 38 if flag: 39 print(msg) 40 from core import src 41 src.user_info['name'] = name 42 src.user_info['type'] = type 43 break 44 else: 45 print(msg) 46 else: 47 print("密码不可为空") 48 else: 49 print('账户输入错误') 50 # 3.创建校区 51 @common.login_auth 52 def create_school(): 53 while True: 54 name = input("name>>>:").strip() 55 type = 'school' 56 flag = common_interface.select_name(type,name) 57 if flag: 58 print('账号已存在,请重新输入') 59 else: 60 addr = input("addr>>>:").strip() 61 if addr: 62 # 调用接口层 63 from core import src 64 admin_name = src.user_info['name'] 65 msg = admin_interface.create_school_interface(admin_name,name,addr) 66 if msg: 67 print(msg) 68 break 69 else: 70 print('地址不可为空') 71 72 # 4.创建讲师 73 @common.login_auth 74 def create_teacher(): 75 from core import src 76 admin_name = src.user_info['name'] 77 while True: 78 teacher_name = input("teacher_name>>>:").strip() 79 if teacher_name == 'q': return 80 type = 'teacher' 81 flag = common_interface.select_name(type, teacher_name) 82 if flag: 83 print('账号已存在,请重新输入') 84 else: 85 # 选择学校 86 school_name = common_interface.select_type_class('school') 87 flag, msg = admin_interface.create_teacher(admin_name,school_name,teacher_name) 88 if flag: 89 print(msg) 90 91 else: 92 print(msg) 93 continue 94 # 5.创建课程 95 @common.login_auth 96 def create_course(): 97 from core import src 98 admin_name = src.user_info['name'] 99 # 输入查询类型 100 school_name = common_interface.select_type_class('school') 101 while True: 102 name = input("course_name>>>:").strip() 103 type = 'Course' 104 flag = common_interface.select_name(name, type) 105 if flag: 106 print('课程存在') 107 else: 108 if name == 'q': return 109 price = input("price>>>:").strip() 110 if price == 'q': return 111 cycle = input("cycle>>>:").strip() 112 if cycle == 'q': return 113 flag, msg = admin_interface.create_course(school_name, admin_name, name, price, cycle) 114 if flag: 115 print(msg) 116 continue 117 else: 118 print(msg) 119 continue 120 # 6.创建班级 121 @common.login_auth 122 def create_classes(): 123 from core import src 124 admin_name = src.user_info['name'] 125 # 输入查询类型 126 school_name = common_interface.select_type_class('school') 127 while True: 128 classes_name = input("classes_name>>>:").strip() 129 if classes_name == 'q': return 130 type = 'classes' 131 flag = common_interface.select_name(type,classes_name) 132 if flag: 133 print('班级已经存在') 134 else: 135 flag, msg = admin_interface.create_classes(school_name, admin_name, classes_name) 136 if flag: 137 print(msg) 138 continue 139 else: 140 print(msg) 141 continue 142 # 7.关联讲师-课程-班级 143 @common.login_auth 144 def choose_teacher_course_classes(): 145 from core import src 146 admin_name = src.user_info['name'] 147 while True: 148 # 管理员找学校 149 school_name = common_interface.select_type_class('school') 150 flag1,msg1 = admin_interface.admin_choose_school(admin_name,school_name) 151 if flag1: 152 print(msg1) 153 else: 154 print(msg1) 155 # 学校找老师 156 teacher_name = common_interface.select_type_class('teacher') 157 flag2, msg2 = admin_interface.school_choose_teacher(school_name,teacher_name) 158 if flag2: 159 print(msg2) 160 else: 161 print(msg2) 162 # 老师选课程 163 course_name = common_interface.select_type_class('course') 164 flag3, msg3 = admin_interface.teacher_choose_course(teacher_name,course_name) 165 if flag3: 166 print(msg3) 167 else: 168 print(msg3) 169 # 课程定班级 170 classes_name = common_interface.select_type_class('classes') 171 flag4, msg4 = admin_interface.course_choose_classes(course_name,classes_name) 172 if flag4: 173 print(msg4) 174 break 175 else: 176 print(msg4) 177 # 8.注销 178 @common.login_auth 179 def logout(): 180 # 注销 181 from core import src 182 name = src.user_info['name'] 183 type = src.user_info['type'] 184 msg = common_interface.logout(type, name) 185 print(msg) 186 func_dic = { 187 "1":register, 188 "2":login, 189 "3":create_school, 190 "4":create_teacher, 191 "5":create_course, 192 "6":create_classes, 193 "7":choose_teacher_course_classes, 194 "8":logout, 195 } 196 def run(): 197 print("welcome to admin view!!!") 198 while True: 199 print(""" 1.注册 2.登录 3.创建校区 4.创建讲师 5.创建课程 6.创建班级 7.关联讲师-课程-班级 8.注销q.quit """) 200 choose = input("choose(q.quit)>>>:").strip() 201 if choose == 'q': 202 return 203 elif choose in func_dic: 204 func_dic[choose]() 205 else: 206 print('choose not in func_dic!!!') 207 continue
src.py
1 from core import admin,teacher,student 2 3 # 保存登录状态 4 user_info = { 5 'name':None, 6 'type':None 7 } 8 9 func_dic = { 10 "1":admin.run, 11 "2":teacher.run, 12 "3":student.run, 13 } 14 15 def run(): 16 print('welcome to choose course !!!') 17 while True: 18 print(""" 1.管理员视图 2.老师视图 3.学生视图 q.quit """) 19 choose = input("choose(q.quit)>>>:").strip() 20 if choose == 'q':return 21 elif choose in func_dic: 22 func_dic[choose]() 23 else: 24 print('choose not in func_dic!!!') 25 continue
student.py
1 from interface import common_interface,student_interface 2 from lib import common 3 # 1.注册 4 def register(): 5 while True: 6 name = input("admin_name>>>:").strip() 7 type = 'student' 8 flag = common_interface.select_name(type,name) 9 if flag: 10 print('账号已存在,请重新输入') 11 else: 12 pwd = input("pwd>>>:").strip() 13 re_pwd = input("re_pwd>>>:").strip() 14 if pwd and re_pwd: 15 if pwd == re_pwd: 16 # 调用接口层 17 msg = common_interface.register_interface(type,name,pwd) 18 if msg: 19 print(msg) 20 break 21 else: 22 print("两次密码不一致") 23 else: 24 print("密码和验证密码不可为空") 25 # 2.登录 26 def login(): 27 while True: 28 name = input("name>>>:").strip() 29 type = 'student' 30 flag = common_interface.select_name(type,name) 31 if flag: 32 print('账号输入正确,请输入密码') 33 pwd = input("pwd>>>:").strip() 34 if pwd: 35 # 调用接口层 36 flag,msg = common_interface.login_interface(type, name, pwd) 37 if flag: 38 print(msg) 39 from core import src 40 src.user_info['name'] = name 41 src.user_info['type'] = type 42 break 43 else: 44 print(msg) 45 else: 46 print("密码不可为空") 47 else: 48 print('账户输入错误') 49 # 3.选课 50 @common.login_auth 51 def choose_stu_course(): 52 from core import src 53 student_name = src.user_info['name'] 54 school_name = common_interface.select_type_class('school') 55 course_name = common_interface.select_type_class('course') 56 msg = student_interface.choose_student_course_interface(student_name, school_name, course_name) 57 print(msg) 58 # 4.交学费 59 @common.login_auth 60 def buy_course(): 61 from core import src 62 student_name = src.user_info['name'] 63 # 班级 64 classes_name = common_interface.select_type_class('classes') 65 66 # 课程 67 course_name = common_interface.select_type_class('course') 68 69 # 价格 70 price = student_interface.course_price_interface(course_name) 71 print(price) 72 # 支付 73 msg = student_interface.is_buy_student_interface(student_name,course_name,classes_name) 74 print(msg) 75 # 5.查看自己的分数 76 @common.login_auth 77 def select_score(): 78 from core import src 79 student_name = src.user_info['name'] 80 msg = student_interface.select_score_student_interface(student_name) 81 print(msg) 82 # 6.注销 83 @common.login_auth 84 def logout(): 85 from core import src 86 name = src.user_info['name'] 87 type = src.user_info['type'] 88 msg = common_interface.logout(type, name) 89 print(msg) 90 func_dic = { 91 "1":register, 92 "2":login, 93 "3":choose_stu_course, 94 "4":buy_course, 95 "5":select_score, 96 "6":logout, 97 }
teacher.py
1 from interface import common_interface,teacher_interface 2 from lib import common 3 4 # 1.登录 5 def login(): 6 while True: 7 name = input("name>>>:").strip() 8 type = 'teacher' 9 flag = common_interface.select_name(type,name) 10 if flag: 11 print('账号输入正确,请输入密码') 12 pwd = input("pwd>>>:").strip() 13 if pwd: 14 # 调用接口层 15 flag,msg = teacher_interface.login_interface(type, name, pwd) 16 if flag: 17 print(msg) 18 from core import src 19 src.user_info['name'] = name 20 src.user_info['type'] = type 21 break 22 else: 23 print(msg) 24 else: 25 print("密码不可为空") 26 else: 27 print('账户输入错误') 28 # 2.选择班级 29 @common.login_auth 30 def choose_classes(): 31 # 校区 32 school_name = common_interface.select_type_class('school') 33 # 课程: 34 course_name = common_interface.select_type_class('course') 35 # 班级 36 classes_name = common_interface.select_type_class('classes') 37 msg = teacher_interface.choose_classes_interface(school_name,course_name,classes_name) 38 print(msg) 39 # 3.修改学员成绩 40 @common.login_auth 41 def update_score(): 42 while True: 43 student_name = input("student_name>>>:").strip() 44 student_score = input("student_score>>>").strip() 45 if student_score.isdigit(): 46 student_score = int(student_score) 47 msg = teacher_interface.select_interface_student(student_name,student_score) 48 print(msg) 49 break 50 else: 51 print('成绩必需为数字') 52 # 4.查看学员信息 53 @common.login_auth 54 def select_student_info(): 55 while True: 56 student_name = input("student_name>>>:").strip() 57 msg = teacher_interface.student_info_interface(student_name) 58 print(msg) 59 break 60 # 5.注销 61 @common.login_auth 62 def logout(): 63 from core import src 64 name = src.user_info['name'] 65 type = src.user_info['type'] 66 msg = common_interface.logout(type, name) 67 print(msg) 68 69 func_dic = { 70 "1":login, 71 "2":choose_classes, 72 "3":update_score, 73 "4":select_student_info, 74 "5":logout, 75 } 76 def run(): 77 print("welcome to admin view!!!") 78 while True: 79 print(""" 1.登录 2.选择班级 3.修改学员成绩 4.查看学员信息 5.注销 q.quit """) 80 choose = input("choose(q.quit)>>>:").strip() 81 if choose == 'q': 82 return 83 elif choose in func_dic: 84 func_dic[choose]() 85 else: 86 print('choose not in func_dic!!!') 87 continue
接口层
interface
admin_interface.py
1 from db.modul import * 2 from lib import common 3 # 创建学校 4 def create_school_interface(admin_name,name,addr): 5 school = School.select(name) 6 if school: 7 return False,f'{name}学校存在' 8 else: 9 admin = Admin.select(admin_name) 10 if admin: 11 admin.add_school(name,addr) 12 return True,f'{name}学校创建成功' 13 else: 14 return False,'管理员不可能不存在,逻辑有问题,重试' 15 16 # 创建老师 17 def create_teacher(admin_name,school_name,teacher_name): 18 # pwd = common.get_pwd(pwd) 19 teacher = Teacher.select(teacher_name) 20 if teacher: 21 return False, f'{teacher_name}老师存在' 22 else: 23 admin = Admin.select(admin_name) 24 if admin: 25 admin.add_teacher(teacher_name,school_name) 26 return True, f'{teacher_name}老师创建成功' 27 else: 28 return False, '管理员不可能不存在,逻辑有问题,重试' 29 # 创建课程 30 def create_course(school_name,admin_name,name,price,cycle): 31 school = School.select(school_name) 32 if school: 33 admin = Admin.select(admin_name) 34 admin.add_course(name,price,cycle) 35 return True, f'{name}课程创建成功' 36 else: 37 print( f'{school_name}学校不存在,去创建学校') 38 from core import admin 39 admin.create_school() 40 print('创建完成,重新选择') 41 42 # 创建班级 43 def create_classes(school_name,admin_name,name): 44 45 school = School.select(school_name) 46 if school: 47 admin = Admin.select(admin_name) 48 admin.add_classes(name) 49 return True, f'{name}班级创建成功' 50 else: 51 print( f'{school_name}学校不存在,去创建学校') 52 from core import admin 53 admin.create_school() 54 print('创建完成,重新选择') 55 56 # 关联管理员和学校 57 def admin_choose_school(admin_name,school_name): 58 admin = Admin.select(admin_name) 59 # if admin.school_list == school_name: 60 # for name in admin.school_list: 61 # if school_name == name: 62 # msg = f'{school_name}学校已经在管理员名下' 63 # return False,msg 64 # else: 65 admin.admin_choose_school(school_name) 66 return True,f'添加{school_name}学校到管理员名下' 67 68 69 # 关联学校和老师 70 def school_choose_teacher(school_name,teacher_name): 71 school = School.select(school_name) 72 # if school: 73 # 74 # old_teacher_list = School.teacher_list 75 # for name in old_teacher_list: 76 # if teacher_name == name: 77 # msg = f'{teacher_name}学校已经在管理员名下' 78 # return False,msg 79 # else: 80 # 81 # return True,f'添加{teacher_name}学校到管理员名下' 82 # else: 83 # from core import admin 84 # admin.create_school() 85 school.school_choose_teacher(teacher_name) 86 return True, f'添加{teacher_name}老师到学校名下' 87 # 关联老师和课程 88 def teacher_choose_course(teacher_name,course_name): 89 teacher = Teacher.select(teacher_name) 90 # if teacher: 91 # old_course_list = Teacher.course_list 92 # for name in old_course_list: 93 # if teacher_name == name: 94 # msg = f'{course_name}学校已经在管理员名下' 95 # return False,msg 96 # else: 97 # old_course_list.append(course_name) 98 # return True,f'添加{teacher_name}学校到管理员名下' 99 # else: 100 # from core import admin 101 # admin.create_teacher() 102 teacher.teacher_choose_course(course_name) 103 return True, f'添加{course_name}课程到老师名下' 104 # 关练课程和班级 105 def course_choose_classes(course_name,classes_name): 106 course = Course.select(course_name) 107 # if course: 108 # 109 # old_course_list = Course.classes_list 110 # for name in old_course_list: 111 # if classes_name == name: 112 # msg = f'{classes_name}学校已经在管理员名下' 113 # return False,msg 114 # else: 115 # old_course_list.append(classes_name) 116 # return True,f'添加{classes_name}学校到管理员名下' 117 # else: 118 # from core import admin 119 # admin.create_course() 120 course.course_choose_classes(classes_name) 121 return True, f'添加{classes_name}班级到课程名下'
common_interface.py
1 from db.modul import * 2 from lib import common 3 4 # 查询账号是否存在 5 def select_name(type,name): 6 if type == 'admin': 7 admin = Admin.select(name) 8 if admin: 9 return True 10 if type == 'teacher': 11 teacher = Teacher.select(name) 12 if teacher: 13 return True 14 if type == 'student': 15 student = Student.select(name) 16 if student: 17 return True 18 if type == 'school': 19 school = School.select(name) 20 if school: 21 return True 22 if type == 'course': 23 course = Course.select(name) 24 if course: 25 return True 26 if type == 'classes': 27 classes = Classes.select(name) 28 if classes: 29 return True 30 else: 31 return False 32 # 注册 33 def register_interface(type,name,pwd): 34 pwd = common.get_pwd(pwd) 35 if type == 'admin': 36 admin = Admin(name,pwd) 37 admin.save() 38 return f"{name}注册完成" 39 if type == 'student': 40 student = Student(name,pwd) 41 student.save() 42 return f"{name}注册完成" 43 else: 44 return False 45 # 登录 46 def login_interface(type,name,pwd): 47 pwd = common.get_pwd(pwd) 48 if type == 'admin': 49 admin = Admin.select(name) 50 if admin.pwd == pwd: 51 msg = f'{name}登陆成功' 52 return True,msg 53 else: 54 return False,f'{name}密码错误' 55 if type == 'student': 56 student = Student.select(name) 57 if student.pwd == pwd: 58 msg = f'{name}登陆成功' 59 return True, msg 60 else: 61 return False, f'{name}密码错误' 62 else: 63 return False,False 64 # 注销 65 def logout(type,name): 66 from core import src 67 src.user_info['name'] = None 68 src.user_info['type'] = None 69 return f"{type}/{name}账号注销成功" 70 # 查询所有 71 # 查看admin/teacher/student/school/course/classes所有的信息 72 def selct_all_name(type): 73 if type == 'school': 74 name_list = School.select_all() 75 res = [] 76 for names in name_list: 77 res.append(names.name) 78 return res 79 if type == 'course': 80 name_list = Course.select_all() 81 res = [] 82 for names in name_list: 83 res.append(names.name) 84 return res 85 if type == 'classes': 86 name_list = Classes.select_all() 87 res = [] 88 for names in name_list: 89 res.append(names.name) 90 return res 91 if type == 'teacher': 92 name_list = Teacher.select_all() 93 res = [] 94 for names in name_list: 95 res.append(names.name) 96 return res 97 98 if type == 'student': 99 name_list = Student.select_all() 100 res = [] 101 for names in name_list: 102 res.append(names.name) 103 return res 104 if type == 'admin': 105 name_list = Admin.select_all() 106 res = [] 107 for names in name_list: 108 res.append(names.name) 109 return res 110 111 def select_type_class(type): 112 while True: 113 # type = input("type(admin/teacher/student/school/course/classes)>>>:").strip() 114 name_list = selct_all_name(type) 115 for name in enumerate(name_list, start=1): 116 print(name) 117 choose = input("choose>>>:").strip() 118 if choose.isdigit(): 119 choose = int(choose) 120 if choose in range(1,len(name_list)+1): 121 type_name = name_list[choose-1] 122 return type_name 123 else: 124 print('超出范围') 125 126 else: 127 print("choose必须为数字") 128 continue
student_interface.py
1 from db.modul import * 2 # 选课接口 3 def choose_student_course_interface(student_name,school_name,course_name): 4 student = Student.select(student_name) 5 if student: 6 student.school_name = school_name 7 student.course_name = course_name 8 student.save() 9 msg = f'{student_name}学员课程已经选好,是[{school_name}/下的{course_name}]' 10 return msg 11 else: 12 msg = f'{student}学员不存在' 13 return msg 14 # 从学生中找班级 15 def select_classes_student_interface(name,classes_name): 16 student = Student.select(name) 17 if student: 18 if student.classes_name: 19 return student.classes_name 20 else: 21 student.classes_name = classes_name 22 student.save() 23 return student.classes_name 24 # 查看班级类中的课程 25 def classes_course_interface(classes_names,course_name): 26 classes = Classes.select(classes_names) 27 if classes: 28 if classes.course_name: 29 return classes.course_name 30 else: 31 classes.course_name = course_name 32 classes.save() 33 return classes.course_name 34 # 从课程中找价格 35 def course_price_interface(course_name): 36 course = Course.select(course_name) 37 if course: 38 if course.price: 39 return course.price 40 else: 41 return '课程不存在' 42 # 付学费 43 def is_buy_student_interface(student_name,course_name,classes_name): 44 student = Student.select(student_name) 45 if student: 46 course = Course.select(course_name) 47 if course: 48 if course.is_buy == True: 49 return f'{student_name}同学支付情况为[{course.is_buy}]' 50 else: 51 course.is_buy = True 52 student.save() 53 return f'{student_name}同学支付情况为[{course.is_buy}]' 54 # 查看学生成绩 55 def select_score_student_interface(name): 56 student = Student.select(name) 57 if student: 58 obj = student.score 59 return obj 60 else: 61 return f'{name}账户不存在'
teacher_interface.py
1 from db.modul import * 2 from lib import common 3 def login_interface(type,name,pwd): 4 # pwd = common.get_pwd(pwd) 5 if type == 'teacher': 6 teacher = Teacher.select(name) 7 if teacher.pwd == pwd: 8 msg = f'{name}登陆成功' 9 return True,msg 10 else: 11 return False,f'{name}密码错误' 12 else: 13 return False,False 14 # 2.选择班级 15 def choose_classes_interface(school_name,course_name,classes_name): 16 from core import src 17 name = src.user_info['name'] 18 teacher = Teacher.select(name) 19 if teacher: 20 teacher.school_name = school_name 21 teacher.teacher_choose_course(course_name) 22 teacher.classes_name = classes_name 23 24 msg = f'{name}老师班级选好了' 25 return msg 26 else: 27 msg = f'{name}老师不存在' 28 return msg 29 # 查看学员,修改成绩 30 def select_interface_student(student_name,student_score): 31 student = Student.select(student_name) 32 if student: 33 student.score = student_score 34 student.save() 35 msg = f'{student_name}成绩修改成功,目前成绩为[{student_score}]' 36 return msg 37 else: 38 msg = f'{student_name}不存在' 39 return msg 40 # 查询学员信息 41 def student_info_interface(student_name): 42 student = Student.select(student_name) 43 if student: 44 msg = f'{student.__dict__}' 45 return msg 46 else: 47 msg = f'{student_name}不存在' 48 return msg
公共层
lib
commom
1 import hashlib 2 from functools import wraps 3 # 加密 4 def get_pwd(pwd): 5 md5 = hashlib.md5() 6 md5.update(pwd.encode('utf-8')) 7 return md5.hexdigest() 8 9 # 装饰器 10 def login_auth(func): 11 from core import src 12 @wraps(func) 13 def inner(*args,**kwargs): 14 if src.user_info['type'] == 'admin': 15 res = func(*args,**kwargs) 16 return res 17 elif src.user_info['type'] == 'student': 18 res = func(*args,**kwargs) 19 return res 20 elif src.user_info['type'] == 'teacher': 21 res = func(*args,**kwargs) 22 return res 23 else: 24 print('无此类型,请重新去登陆') 25 type = input("type(Admin/Teacher/Student)>>>:").strip() 26 if type == 'Admin': 27 from core import admin 28 admin.login() 29 elif type == 'Student': 30 from core import student 31 student.login() 32 elif type == 'Teacher': 33 from core import teacher 34 teacher.login() 35 return inner
配置层
conf
settings
1 import os 2 3 # 添加环境变量 4 BEST_PATH = os.path.dirname(os.path.dirname(__file__)) 5 DB_PATH = os.path.join(BEST_PATH,"db","dates")
数据库
db
db_hangdler
1 import os 2 import pickle 3 from conf import settings 4 # 收 5 def db_save(obj): 6 dir = os.path.join(settings.DB_PATH,obj.__class__.__name__) 7 print(obj.__class__.__name__) 8 # 判断路径 9 if not os.path.exists(dir): 10 os.makedirs(dir) 11 file_path = os.path.join(dir,obj.name) 12 with open(file_path,'wb')as f: 13 pickle.dump(obj,f) 14 f.flush() 15 return 16 17 # 查 18 # 查一 19 def db_select(cls,name): 20 dir = os.path.join(settings.DB_PATH, cls,name) 21 # 判断路径 22 if os.path.exists(dir): 23 with open(dir,'rb')as f: 24 return pickle.load(f) 25 else: 26 return False 27 # 查所有 28 def db_select_all(cls): 29 dir = os.path.join(settings.DB_PATH, cls) 30 # 判断路径 31 if os.path.exists(dir): 32 res = [] 33 for name in os.listdir(dir): 34 obj = db_select(cls,name) 35 res.append(obj) 36 return res 37 else: 38 return False
modul
1 from db import db_handler 2 # 基类 3 class Base: 4 def save(self): 5 print(self.__class__,'1111111111111') 6 7 print(self.__class__.__name__,'33333') 8 return db_handler.db_save(self) 9 @classmethod 10 def select(cls,name): 11 print(cls.__name__, '22222222') 12 return db_handler.db_select(cls.__name__,name) 13 @classmethod 14 def select_all(cls): 15 return db_handler.db_select_all(cls.__name__) 16 # 学校类 17 class School(Base): 18 def __init__(self,school_name,addr): 19 self.name = school_name 20 self.addr = addr 21 self.teacher_list = [] 22 self.save() 23 def school_choose_teacher(self,teacher_name): 24 self.teacher_list.append(teacher_name) 25 self.save() 26 # 课程类 27 class Course(Base): 28 def __init__(self,course_name,price,cycle): 29 self.name = course_name 30 self.price = price 31 self.cycle = cycle 32 self.is_buy = False 33 self.classes_list = [] 34 self.save() 35 def course_choose_classes(self,course_name): 36 self.classes_list.append(course_name) 37 self.save() 38 39 # 班级类 40 class Classes(Base): 41 def __init__(self,classes_name): 42 self.name = classes_name 43 self.student_list = [] 44 self.course = None 45 self.save() 46 def classes_choose_student(self,student_name): 47 self.student_list.append(student_name) 48 self.save() 49 # 管理员类 50 class Admin(Base): 51 def __init__(self,name,pwd): 52 self.name = name 53 self.pwd = pwd 54 self.school_list = [] 55 self.save() 56 def add_school(self,school_name,addr): 57 School(school_name,addr) 58 def add_teacher(self,name,school_name): 59 Teacher(name,school_name) 60 def add_course(self,course_name,price,cycle): 61 Course(course_name,price,cycle) 62 def add_classes(self,classes_name): 63 Classes(classes_name) 64 def admin_choose_school(self,school_name): 65 self.school_list.append(school_name) 66 self.save() 67 # 老师类 68 class Teacher(Base): 69 def __init__(self,name,school_name): 70 self.name = name 71 self.pwd = '123' 72 self.course_list = [] 73 self.class_name = None 74 self.school_name = school_name 75 self.save() 76 def teacher_choose_course(self, course_name): 77 self.course_list.append(course_name) 78 self.save() 79 80 def check_course_student(self,course_name): 81 course_obj = Course.select(course_name) 82 return course_obj.student_list 83 84 def change_score(self, course_name, student_name, score): 85 student_obj = Student.select(student_name) 86 student_obj.score[course_name] = score 87 student_obj.save() 88 # 学生类 89 class Student(Base): 90 def __init__(self,name,pwd): 91 self.name = name 92 self.pwd = pwd 93 self.course_name = None 94 self.score = None 95 self.save()
补充:日志
log
log_log
1 # 日志 2 import logging.config # 配置,布局 3 """ 4 知识点1:日志输出的格式 5 """ 6 # 定义三种日志输出的格式 # 开始 7 # standart 标准格式 8 standart_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 9 '[%(levelname)s][%(message)s]' # name 为getlogger 指定的名字 10 # simple 简单格式 11 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' 12 # 定义日志输出的格式 # 结束 13 14 """ 15 # 知识点2 16 下面的两个变量对应的值 需要你手动修改 17 """ 18 # # log文件的目录 19 logfile_dir = os.path.dirname(os.path.dirname(__file__)) 20 # log文件名 21 logfile_name = 'log.log' 22 23 24 """ 25 # 知识点3 26 # 如果不存在定义的日志目录就创建一个 27 """ 28 # os.path.isdir 判断指定路径是否存在 29 if not os.path.isdir(logfile_dir): 30 # os.mkdir 创建新文件 31 os.mkdir(logfile_dir) 32 """ 33 知识点4 34 log文件的全路径 35 """ 36 # 把指定路径拼接到名字中 37 logfile_path = os.path.join(logfile_dir,f'{logfile_dir}/{logfile_name}.log') 38 39 """ 40 知识点5 41 log配置文件 42 """ 43 LOGGING_DIC = { 44 'version': 1, # version版本 45 'disable_existing_loggers': False, # disable_existing_loggers 禁用现有记录器 46 'formatters': { 47 'standard': { 48 'format': standart_format 49 }, # format 格式 50 'simple': { 51 'format': simple_format 52 }, 53 }, 54 'filters': {}, # 过滤日志 55 'handlers': { 56 #打印到终端的日志 57 'console': { 58 'level': 'DEBUG', 59 'class': 'logging.StreamHandler', # 打印到屏幕 60 'formatter': 'simple' 61 }, 62 #打印到文件的日志,收集info及以上的日志 63 'default': { 64 'level': 'DEBUG', 65 'class': 'logging.handlers.RotatingFileHandler', # 保存到文件 66 'formatter': 'standard', 67 'filename': logfile_path, # 日志文件 68 'maxBytes': 1024*1024*5, # 日志大小 5M 69 'backupCount': 5, 70 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 71 }, 72 }, 73 'loggers': { 74 #logging.getLogger(__name__)拿到的logger配置 75 '': { 76 'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 77 'level': 'DEBUG', 78 'propagate': True, # 向上(更高level的logger)传递 79 }, # 当键不存在的情况下 默认都会使用该k:v配置 80 }, 81 }
lib
common
1 import logging.config 2 # 生成日志 3 def get_log(type_name): 4 # 自动加载字典中的配置 5 logging.config.dictConfig(settings.LOGGING_DIC) 6 # 负责生产日志 7 logger1 = logging.getLogger(type_name) 8 # 记录日志 9 return logger1
interface
接口层使用
1 common.get_log('').debug(msg) 2 实际为 3 logger1.debug(日志内容)