一、功能简要
基本实现以下功能,但是有部分地方由于时间关系并未写,而且并未做细微的完善,大致功能完成。
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
二、代码解析
(1)文件目录:
(2)代码
start.py:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 #Author:chenxz 4 import os,sys 5 base_path=os.path.dirname(os.path.dirname(__file__)) 6 sys.path.append(base_path) 7 from core import main 8 9 10 #开始启动程序 11 if __name__ == '__main__': 12 Mainprogram_obj=main.Mainprogram() 13 Mainprogram_obj.run()
setting:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 #Author:chenxz 4 import logging 5 import os,sys 6 7 BASE_DIR=base_path=os.path.dirname(os.path.dirname(__file__)) 8 split=os.path.sep 9 #存放学校数据文件路径 10 school_file_path=base_path+split+'db'+split+'school'+split+'school_data' 11 12 13 14 #日志文件 15 LOG_FILE=BASE_DIR+split+'log'+split+'service.log' 16 #日志级别和格式设置 17 logging.basicConfig(level=logging.DEBUG, 18 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 19 datefmt='%a, %d %b %Y %H:%M:%S', 20 filename=LOG_FILE, 21 filemode='a+') 22 23 24 25 if __name__ == '__main__': 26 print(LOG_FILE)
main:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 #Author:chenxz 4 import os,sys 5 import pickle 6 base_path=os.path.dirname(os.path.dirname(__file__)) 7 sys.path.append(base_path) 8 from conf import setting 9 from core import resource 10 11 class Student_view(object): 12 ''' 13 学生视觉类 14 ''' 15 def __init__(self): 16 self.Admin_view_obj = Admin_view() 17 self.school_data = self.Admin_view_obj.school_data 18 def run_view(self): 19 ''' 20 执行学生视觉,选择登录或者注册 21 :return: 22 ''' 23 setting.logging.debug('run_student_view') 24 while True: 25 print('=====已有学校=======') 26 for i in self.school_data: 27 print('学校:%s ,地址:%s' % (i, self.school_data[i].addr)) 28 school_choice=input('请输入你要管理的学校名(q退出r返回):') 29 if school_choice in self.school_data: 30 self.school_obj=self.school_data[school_choice] 31 while True: 32 def_dict={'1':'register','2':'student_login'} 33 for i,j in enumerate(def_dict): 34 print(i+1,def_dict[j]) 35 def_choice=input('请输入你的选择(q退出r返回):') 36 if def_choice.isdigit(): 37 if 0 < int(def_choice) <= len(def_dict): 38 if hasattr(self,def_dict[def_choice]): 39 getattr(self,def_dict[def_choice])() 40 else: 41 print('没有这个功能方法') 42 continue 43 else: 44 print('输入的数字不正确!') 45 continue 46 elif def_choice == 'r': 47 break 48 elif def_choice == 'q': 49 exit() 50 else: 51 print('输入必须是数字!') 52 continue 53 elif school_choice == 'r': 54 break 55 elif school_choice == 'q': 56 exit() 57 def register(self,): 58 ''' 59 注册方法 60 :return: 61 ''' 62 setting.logging.debug('学生开始注册') 63 while True: 64 student_name=input('请输入学生名称:') 65 student_password= input('请输入密码:') 66 student_age = input('请输入学生年纪:') 67 if student_name in self.school_obj.student_dict: 68 print('此学生已经注册') 69 continue 70 else: 71 student_obj=resource.Student(student_name,student_password,student_age) 72 self.school_obj.student_dict[student_name]=student_obj 73 self.school_data[self.school_obj.name]=self.school_obj 74 f = open(setting.school_file_path, 'wb') 75 f.write(pickle.dumps(self.school_data)) 76 f.close() 77 print('成功添加学生%s' % student_name) 78 break 79 80 def student_login(self): 81 ''' 82 学生登录校验以及选择登录成功后的操作 83 :return: 84 ''' 85 while True: 86 student_user = input('请输入你的用户名:').strip() 87 student_password = input('请输入你的密码:').strip() 88 if student_user in self.school_obj.student_dict and self.school_obj.student_dict[ 89 student_user].password == student_password: 90 print('%s用户登录成功' % student_user) 91 student_obj = self.school_obj.student_dict[student_user] 92 while True: 93 def_dict = {'1': 'change_info', '2': 'tell_info', '3': 'test1', '4': 'test2', 94 '5': 'test3'} 95 for i in def_dict: 96 print(i, def_dict[i]) 97 def_choice = input("请输入你的选择(q退出r返回):") 98 if def_choice.isdigit(): 99 if 0 < int(def_choice) <= len(def_dict): 100 if hasattr(self, def_dict[def_choice]): 101 getattr(self, def_dict[def_choice])(student_obj) 102 else: 103 print('没有这个功能方法') 104 continue 105 else: 106 print('输入的数字不正确!') 107 continue 108 elif def_choice == 'r': 109 break 110 elif def_choice == 'q': 111 exit() 112 else: 113 print('输入必须是数字!') 114 continue 115 break 116 else: 117 print('用户密码不正确') 118 continue 119 120 def tell_info(self,obj): 121 ''' 122 查看学生信息 123 :param obj: 学生对象 124 :return: 125 ''' 126 print(''' 127 姓名:%s 128 年纪:%s 129 '''%(obj.name,obj.age)) 130 131 def change_info(self,obj): 132 ''' 133 修改学生信息 134 :param obj: 学生对象 135 :return: 136 ''' 137 print('change_info') 138 139 student_password = input('请输入新密码:') 140 student_age = input('请输入老师新年纪:') 141 student_name=obj.name 142 obj.password=student_password 143 obj.age=student_age 144 self.school_data[self.school_obj.name].student_name=obj 145 f = open(setting.school_file_path, 'wb') 146 f.write(pickle.dumps(self.school_data)) 147 f.close() 148 print('修改信息成功' ) 149 150 151 class Teacher_view(object): 152 ''' 153 老师视觉类 154 ''' 155 def __init__(self): 156 self.Admin_view_obj=Admin_view() 157 self.school_data=self.Admin_view_obj.school_data 158 def run_view(self): 159 ''' 160 执行老师视觉,进行登录以及选择对应的操作 161 :return: 162 ''' 163 setting.logging.debug('run Teacher_view') 164 self.Admin_view_obj.show_school_list() 165 choice_school=input('请选择你要进入的学校名称:').strip() 166 if choice_school in self.school_data: 167 self.school_obj=self.school_data[choice_school] 168 while True: 169 teacher_user=input('请输入你的用户名:').strip() 170 teacher_password=input('请输入你的密码:').strip() 171 if teacher_user in self.school_obj.teacher_dict and self.school_obj.teacher_dict[teacher_user].password==teacher_password: 172 print('%s用户登录成功'%teacher_user) 173 teacher_obj=self.school_obj.teacher_dict[teacher_user] 174 while True: 175 def_dict = {'1': 'teaching', '2': 'tell_info', '3': 'show_classes', '4': 'show_student_scord', 176 '5': 'change_info'} 177 for i in def_dict: 178 print(i, def_dict[i]) 179 def_choice=input("请输入你的选择(q退出r返回):") 180 if def_choice.isdigit(): 181 if 0 < int(def_choice) <= len(def_dict): 182 if hasattr(self, def_dict[def_choice]): 183 getattr(self, def_dict[def_choice])(teacher_obj) 184 else: 185 print('没有这个功能方法') 186 continue 187 else: 188 print('输入的数字不正确!') 189 continue 190 elif def_choice == 'r': 191 break 192 elif def_choice == 'q': 193 exit() 194 else: 195 print('输入必须是数字!') 196 continue 197 break 198 else: 199 print('用户密码不正确') 200 continue 201 else: 202 print('学校名输入错误!') 203 def teaching(self,obj): 204 ''' 205 授课 206 :param obj: 老师对象 207 :return: 208 ''' 209 print('%s is teaching'%obj.name) 210 211 def tell_info(self,obj): 212 ''' 213 查看老师信息 214 :param obj: 老师对象 215 :return: 216 ''' 217 print(''' 218 姓名:%s 219 年纪:%s 220 工资:%s 221 '''%(obj.name,obj.age,obj.salary)) 222 223 def show_classes(self,obj): 224 ''' 225 查看授课班级 226 :param obj: 老师对象 227 :return: 228 ''' 229 print('====老师授课班级===') 230 for i in obj.classes: 231 print(i) 232 233 def show_course(self,obj): 234 print('show_student_scord,未写') 235 236 def change_info(self,obj): 237 ''' 238 修改老师信息 239 :param obj: 老师对象 240 :return: 241 ''' 242 print('change_info') 243 #teacher_name = input('请输入新名称:') 244 teacher_password = input('请输入新密码:') 245 teacher_salary = input('请输入老师新工资:') 246 teacher_age = input('请输入老师新年纪:') 247 teacher_name=obj.name 248 obj.password=teacher_password 249 obj.salary=teacher_salary 250 obj.age=teacher_age 251 self.school_data[self.school_obj.name].teacher_name=obj 252 f = open(setting.school_file_path, 'wb') 253 f.write(pickle.dumps(self.school_data)) 254 f.close() 255 print('修改信息成功' ) 256 257 258 259 260 class Admin_view(object): 261 ''' 262 管理员视图类 263 ''' 264 def __init__(self): 265 self.f=open(setting.school_file_path,'rb') 266 self.school_data=pickle.loads(self.f.read()) 267 def __del__(self): 268 pass 269 def run_view(self): 270 ''' 271 执行管理员视图,并选择你要做的操作 272 :return: 273 ''' 274 setting.logging.debug('run Admin_view') 275 def_dict = {'1': 'add_school', '2': 'show_school_list', '3': 'del_school','4':'manage_school'} 276 while True: 277 print(''' 278 ==========chen_school========== 279 ''') 280 for i in def_dict: 281 print(i, def_dict[i]) 282 choice = input('请选择进入的视图(q退出,r返回):') 283 if choice.isdigit(): 284 if 0 < int(choice) <= len(def_dict): 285 if hasattr(self,def_dict[choice]): 286 getattr(self,def_dict[choice])() 287 else: 288 print('没有这个功能方法') 289 continue 290 else: 291 print('输入的数字不正确!') 292 continue 293 elif choice == 'r': 294 break 295 elif choice == 'q': 296 exit() 297 else: 298 print('输入必须是数字!') 299 continue 300 def add_school(self): 301 ''' 302 添加学校方法 303 :return: 304 ''' 305 school_name = input('请输入学校名:') 306 school_addr = input('请输入学校地址:') 307 school_obj = resource.School(school_name, school_addr) 308 self.school_data[school_name] = school_obj 309 f = open(setting.school_file_path, 'wb') 310 f.write(pickle.dumps(self.school_data)) 311 f.close() 312 print('添加学校%s成功'%school_name) 313 314 def show_school_list(self): 315 ''' 316 查看所有的学校 317 :return: 318 ''' 319 print('=====已有学校=======') 320 for i in self.school_data: 321 print('学校:%s ,地址:%s'%(i,self.school_data[i].addr)) 322 def del_school(self): 323 ''' 324 删除学校 325 :return: 326 ''' 327 print('=====已有学校=======') 328 for i in self.school_data: 329 print('学校:%s ,地址:%s' % (i, self.school_data[i].addr)) 330 del_choice=input('请选择你要删除的学校名(r返回):') 331 if del_choice in self.school_data: 332 333 del self.school_data[del_choice] 334 f = open(setting.school_file_path, 'wb') 335 f.write(pickle.dumps(self.school_data)) 336 f.close() 337 print('删除%s学校成功'%del_choice) 338 else: 339 print('你输入的学校不存在') 340 341 def manage_school(self): 342 ''' 343 管理学校方法,进行管理老师学生课程班级等作用 344 :return: 345 ''' 346 while True: 347 print('=====已有学校=======') 348 for i in self.school_data: 349 print('学校:%s ,地址:%s' % (i, self.school_data[i].addr)) 350 school_choice=input('请输入你要管理的学校名(q退出r返回):') 351 if school_choice in self.school_data: 352 school_obj=self.school_data[school_choice] 353 while True: 354 manage_list=['add_teacher','add_classes','add_course','show_teacher','show_classes','show_course','del_teaher','del_classes','del_course'] 355 for i,j in enumerate(manage_list): 356 print(i+1,j) 357 manage_choice=input('请输入你的选择(q退出r返回):') 358 if manage_choice.isdigit(): 359 if 0 < int(manage_choice) <= len(manage_list): 360 if hasattr(self, manage_list[int(manage_choice)-1]): 361 getattr(self, manage_list[int(manage_choice)-1])(school_obj) 362 else: 363 print('没有这个功能方法') 364 continue 365 else: 366 print('输入的数字不正确!') 367 continue 368 elif manage_choice == 'r': 369 break 370 elif manage_choice == 'q': 371 exit() 372 else: 373 print('输入必须是数字!') 374 continue 375 elif school_choice == 'r': 376 break 377 elif school_choice == 'q': 378 exit() 379 else: 380 print('输入的学校不存在') 381 continue 382 def add_teacher(self,school_obj): 383 ''' 384 添加老师 385 :param school_obj: 要管理的学校对象 386 :return: 387 ''' 388 while True: 389 teacher_name=input('请输入老师名称:') 390 teacher_password= input('请输入密码:') 391 teacher_salary = input('请输入老师工资:') 392 teacher_age = input('请输入老师年纪:') 393 if teacher_name in school_obj.teacher_dict: 394 print('此老师已经注册') 395 continue 396 else: 397 teacher_obj=resource.Teacher(teacher_name,teacher_password,teacher_salary,teacher_age) 398 school_obj.teacher_dict[teacher_name]=teacher_obj 399 self.school_data[school_obj.name]=school_obj 400 f = open(setting.school_file_path, 'wb') 401 f.write(pickle.dumps(self.school_data)) 402 f.close() 403 print('成功添加老师%s' % teacher_name) 404 break 405 def add_classes(self,school_obj): 406 ''' 407 添加班级 408 :param school_obj: 要管理的学校对象 409 :return: 410 ''' 411 while True: 412 classes_name=input('请输入班级名称:') 413 if classes_name in school_obj.classes_dict: 414 print('此班级已经存在') 415 continue 416 else: 417 classes_obj=resource.Classes(classes_name) 418 school_obj.classes_dict[classes_name]=classes_obj 419 self.school_data[school_obj.name]=school_obj 420 f = open(setting.school_file_path, 'wb') 421 f.write(pickle.dumps(self.school_data)) 422 f.close() 423 print('成功添加班级%s' % classes_name) 424 break 425 def add_course(self,school_obj): 426 ''' 427 添加课程 428 :param school_obj: 要管理的学校对象 429 :return: 430 ''' 431 while True: 432 course_name=input('请输入课程名称:') 433 course_pay=input('请输入课程价格:') 434 if course_name in school_obj.course_dict: 435 print('此课程已经存在') 436 continue 437 else: 438 course_obj=resource.Course(course_name,course_pay) 439 school_obj.course_dict[course_name]=course_obj 440 self.school_data[school_obj.name]=school_obj 441 f = open(setting.school_file_path, 'wb') 442 f.write(pickle.dumps(self.school_data)) 443 f.close() 444 print('成功添加课程%s' % course_name) 445 break 446 def show_teacher(self,school_obj): 447 ''' 448 查看老师 449 :param school_obj: 要管理的学校对象 450 :return: 451 ''' 452 teacher_dict=self.school_data[school_obj.name].teacher_dict 453 for i in teacher_dict: 454 print('姓名:%s'%i) 455 def show_classes(self,school_obj): 456 ''' 457 查看班级 458 :param school_obj: 要管理的学校对象 459 :return: 460 ''' 461 classes_dict=self.school_data[school_obj.name].classes_dict 462 for i in classes_dict: 463 print('班级:%s'%i) 464 def show_course(self,school_obj): 465 ''' 466 查看课程 467 :param school_obj: 要管理的学校对象 468 :return: 469 ''' 470 course_dict=self.school_data[school_obj.name].course_dict 471 for i in course_dict: 472 print('课程:%s'%i) 473 def del_teaher(self,school_obj): 474 print('删除老师,未写') 475 def del_classes(self,school_obj): 476 print('删除班级,未写') 477 def del_course(self,school_obj): 478 print('删除课程,未写') 479 480 481 class Mainprogram(object): 482 ''' 483 主程序类,开启程序,并选择不同视图进入 484 ''' 485 def __init__(self): 486 pass 487 def run(self): 488 ''' 489 按需选择不同视图 490 :return: 491 ''' 492 setting.logging.debug('开始启动程序...') 493 view_dict = {'1': 'Student_view', '2': 'Teacher_view', '3': 'Admin_view'} 494 view_list = [Student_view, Teacher_view, Admin_view] 495 while True: 496 print(''' 497 ==========chen_school========== 498 ''') 499 for i in view_dict: 500 print(i, view_dict[i]) 501 choice = input('请选择进入的视图(q退出):') 502 if choice.isdigit(): 503 if 0 < int(choice) <= len(view_dict): 504 obj=view_list[int(choice) - 1]() 505 obj.run_view() 506 else: 507 print('输入的数字不正确!') 508 continue 509 elif choice=='q': 510 break 511 else: 512 print('输入必须是数字!') 513 break 514 515 516 517 if __name__ == '__main__': 518 Mainprogram_obj=Mainprogram() 519 Mainprogram_obj.run()
resource.py:
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 #Author:chenxz 4 5 6 class School(object): 7 ''' 8 学校类 9 ''' 10 def __init__(self,name,addr,): 11 self.name=name 12 self.addr=addr 13 self.teacher_dict={} 14 self.student_dict={} 15 self.classes_dict={} 16 self.course_dict={} 17 18 class Teacher(object): 19 ''' 20 老师类 21 ''' 22 def __init__(self,name,password,salary,age): 23 self.name=name 24 self.salary=salary 25 self.age=age 26 self.password=password 27 self.classes={} 28 self.course={} 29 30 class Classes(object): 31 ''' 32 班级类 33 ''' 34 def __init__(self,name): 35 self.name=name 36 self.student={} 37 self.course={} 38 39 class Course(object): 40 ''' 41 课程类 42 ''' 43 def __init__(self,name,pay): 44 self.name=name 45 self.pay=pay 46 self.student={} 47 48 class Student(object): 49 ''' 50 学生类 51 ''' 52 def __init__(self,name,password,age): 53 self.name=name 54 self.age=age 55 self.password=password
schoool.py:
1 import pickle 2 import os,sys 3 base_path=os.path.dirname(os.path.dirname(os.path.dirname(__file__))) 4 split=os.path.sep 5 sys.path.append(base_path) 6 from core import resource 7 from conf import setting 8 9 10 def add_achool(): 11 ''' 12 重置学校文件数据的方法 13 :return: 14 ''' 15 school_name=input('请输入学校名:') 16 school_addr=input('请输入学校地址:') 17 school_obj=resource.School(school_name,school_addr) 18 schoo_data={school_name:school_obj} 19 schoo_data_path=setting.school_file_path 20 data=pickle.dumps(schoo_data) 21 f=open(schoo_data_path,'wb') 22 f.write(data) 23 f.close() 24 25 def open_school_file(): 26 ''' 27 查看学校文件 28 :return: 29 ''' 30 f = open(setting.school_file_path, 'rb') 31 school_data = pickle.loads(f.read()) 32 print(school_data['beijing'].teacher_list[0].name) 33 34 if __name__ == '__main__': 35 add_achool()