python开发选课系统
2018-09-18 06:43:21来源:博客园 阅读 ()
一、选课系统(面向对象作业)
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件
第一步:需求分析
1. 创建北京、上海 2 所学校 ======》创建学校
2. 创建linux、python、go 三个课程 =====》创建课程,课程关联学校
3. 管理员创建学校、老师、课程 =======》 创建学校,创建老师,创建课程
4. 课程包含课程名(周期、价格等) =====》课程属性
5. 学校包含学校名称,地址等属性 =====》学校属性
6. 创建老师角色关联学校 ======》给老师角色添加学校属性
7. 学生注册登录后,可以选择学校、选择课程、查看成绩
8. 老师登录后,可以查看教授课程,选择想要教授的课程,查看课程下的学生,修改学生成绩等。
管理员视图:
注册、登录
创建学校
创建老师
创建课程
教师视图:
登录
选择课程
查看课程
查看学生
修改学生成绩
学生视图:
注册、登录
选择学校
选择课程
查看成绩
第二步:框架搭建
框架如图
第三步:写程序
选课系统目录结构 |-----conf | |-----settings.py 路径配置+日志配置 | |-----core 用户核心 | |-----src.py 二级启动文件 | |-----admin.py 管理员功能层 | |-----teacher.py 教师功能层 | |-----student.py 学生功能层 | |-----db 数据 | |-----db_handler.py 数据处理 | |-----modules.py 类及方法 | |-----interface 接口层 | |-----common_interface.py 公共接口 | |-----admin_interface.py 管理员接口 | |-----teacher_interface.py 教师接口 | |-----student_interface.py 学生接口 | |-----lib 公共方法 | |-----common.py 公共功能 | |-----log 日志数据 | |-----operation.log 日志文件 | |-----start.py 程序启动文件 |-----Readme.txt 程序文档注释
#:coding:utf-8 import os BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DB=os.path.join(BASE_DIR,'db') standard_format = '[%(asctime)s][task_id:%(name)s]' \ '[%(levelname)s]< %(message)s >' # 其中name为getlogger指定的名字 id_simple_format = '[%(asctime)s][task_id:%(name)s] %(message)s' LOG_PATH = os.path.join(BASE_DIR, 'log', 'operation.log') # 日志路径 LOGGING_DIC = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': standard_format }, 'simple': { 'format': id_simple_format }, }, 'filters': {}, 'handlers': { # 打印到终端的日志,使用的格式为 simple_format 'ch': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', # 打印到屏幕 'formatter': 'simple' }, # 打印到文件的日志,使用的格式为 standard_format 'default': { 'level': 'DEBUG', 'class': 'logging.FileHandler', # 保存到文件 'formatter': 'standard', 'filename': LOG_PATH, # 日志文件 'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了 }, }, 'loggers': { '': { 'handlers': ['ch', 'default'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕 'level': 'DEBUG', 'propagate': False, }, }, }
#:coding:utf-8 # =========================src.py========================= from core import admin, teacher, student tag = True choice_dict = { '1': admin.admin_main, '2': teacher.teacher_main, '3': student.student_main } def run(): global tag while tag: print( ''' 1. 管理员视图 2. 教师视图 3. 学生视图 q. 退出 ''' ) choice = input('请选择:').strip() if choice == 'q': tag = False break if choice not in choice_dict: continue choice_dict[choice]()
#:coding:utf-8 from core import src from interface import admin_interface, common_interface, teacher_interface, student_interface from lib import common admin_info = { 'name': None } def admin_registe(): print('管理员注册'.center(40, '-')) if admin_info['name']: print('您已登录,无法注册') return while True: name = input('请输入名字:').strip() password = input('请输入密码:').strip() password1 = input('请确认密码:').strip() if not name or not password: continue if password == password1: state, msg = admin_interface.admin_registe_interface(name, password) if state: print(msg) break else: print(msg) else: print('两次密码不一致') def admin_login(): print('管理员登录'.center(40, '-')) if admin_info['name']: print('您已登录') return while True: name = input('请输入名字:').strip() password = input('请输入密码:').strip() if not name or not password: continue state, msg = common_interface.login_interface('admin', name, password) if state: print(msg) admin_info['name'] = name break else: print(msg) @common.auth(user_type='admin') def create_school(): print('创建学校'.center(40, '-')) name = input('请输入学校名称:').strip() addr = input('请输入学校地址:').strip() if not name or not addr: print('不可为空') return state, msg = admin_interface.create_school_interface(admin_info['name'], name, addr) if state: print(msg) else: print(msg) @common.auth(user_type='admin') def create_teacher(): print('创建教师'.center(40, '-')) name = input('请输入教师名字:').strip() if not name: print('不可为空') return state, msg = admin_interface.create_teacher_interface(admin_info['name'], name) if state: print(msg) else: print(msg) @common.auth(user_type='admin') def create_corse(): print('创建课程'.center(40, '-')) while True: school_list = common_interface.check_all_school() if school_list: for i, school in enumerate(school_list): print('%s : %s' % (i, school)) choice = input('请选择学校:').strip() if choice.isdigit(): choice = int(choice) if choice >= len(school_list): continue name = input('请输入课程名字:').strip() price = input('请输入课程价格:').strip() period = input('请输入课程周期:(mouth)').strip() if not name or not price or not period: continue state, msg = admin_interface.create_course_interface(admin_info['name'], name, price, period, school_list[choice]) if state: print(msg) break else: print(msg) else: print('请输入整数') @common.auth(user_type='admin') def check_log(): common_interface.check_log_interface(admin_info['name']) choice_dict = { '1': admin_registe, '2': admin_login, '3': create_school, '4': create_teacher, '5': create_corse, '6': check_log } def admin_main(): while True: print('管理员界面'.center(60, '-')) print( ''' 1. 注册 2. 登录 3. 创建学校 4. 创建老师 5. 创建课程 6. 查看操作日志 q. 退出 ''' ) choice = input('请选择(管理员):').strip() if choice == 'q': src.tag = False break if choice not in choice_dict: continue choice_dict[choice]()
#:coding:utf-8 from core import src from interface import common_interface, teacher_interface from lib import common teacher_info = { 'name': None } def teacher_login(): print('教师登录'.center(40, '-')) if teacher_info['name']: print('您已登录,无需再登录') return while True: name = input('请输入名字:').strip() password = input('请输入密码:').strip() if not name or not password: continue state, msg = common_interface.login_interface('teacher', name, password) if state: print(msg) teacher_info['name'] = name break else: print(msg) @common.auth(user_type='teacher') def choose_course(): print('选择课程'.center(40,'-')) course_list = common_interface.check_all_course() if course_list is None: print('暂无课程') else: for i, course in enumerate(course_list): print('%s : %s' % (i, course)) choice = input('请选择(课程):').strip() if choice.isdigit(): choice = int(choice) if choice >= len(course_list): return state, msg = teacher_interface.choose_course_interface(teacher_info['name'], course_list[choice]) if state: print(msg) else: print(msg) else: print('请输入整数') @common.auth(user_type='teacher') def check_course(): print('查看课程'.center(40, '-')) state, msg, _ = teacher_interface.check_tea_course_interface(teacher_info['name']) if state: pass else: print(msg) @common.auth(user_type='teacher') def check_student(): print('查看学生'.center(40, '-')) _, _, course_list = teacher_interface.check_tea_course_interface(teacher_info['name']) if course_list is None: return for i, course in enumerate(course_list): print('%s :课程 %s' % (i, course)) choice = input('请选择课程:').strip() if choice.isdigit(): choice = int(choice) if choice >= len(course_list): print('输入的数字超过范围') else: state,student_list = teacher_interface.check_student_interface(course_list[choice]) if state: for k, v in enumerate(student_list): print('%s :学生 %s' % (k, v)) else:print(student_list) else: print('请输入数字') @common.auth(user_type='teacher') def set_stu_score(): print('为学生打分'.center(40, '-')) _, _, course_list = teacher_interface.check_tea_course_interface(teacher_info['name']) if course_list is None: print('暂无课程,请先去选择课程') return while True: for i, course in enumerate(course_list): print('%s :课程 %s' % (i, course)) choice_course = input('请选择课程:').strip() if choice_course.isdigit(): choice_course = int(choice_course) if choice_course >= len(course_list): print('输入的数字超过范围') continue else: state,student_list = teacher_interface.check_student_interface(course_list[choice_course]) if state: for k, v in enumerate(student_list): print('%s :学生 %s' % (k, v)) choice_stu = input('请选择要打分的学生:').strip() if choice_stu.isdigit(): choice_stu = int(choice_stu) if choice_stu >= len(course_list): print('输入的数字超过范围') else: score = input('请输入分数(打分):').strip() if score.isdigit(): msg = teacher_interface.mark_student_interface(teacher_info['name'], student_list[choice_stu], course_list[choice_course], score) print(msg) break else: print('请输入数字,亲') else: print('请输入数字,亲') else: print(student_list) else: print('请输入数字,亲') @common.auth(user_type='teacher') def check_log(): common_interface.check_log_interface(teacher_info['name']) choice_dict = { '1': teacher_login, '2': choose_course, '3': check_course, '4': check_student, '5': set_stu_score, '6': check_log } def teacher_main(): while src.tag: print( ''' 1. 登录 2. 选择课程 3. 查看课程 4. 查看学生 5. 修改学生成绩 6. 查看操作日志 q. 退出 ''' ) choice = input('请选择(教师):').strip() if choice == 'q': src.tag = False break if choice not in choice_dict: continue choice_dict[choice]()
#:coding:utf-8 from core import src from interface import student_interface, common_interface from lib import common student_info = { 'name': None } def student_registe(): print('学生注册'.center(40, '-')) if student_info['name']: print('您已登录,无法注册') return while True: name = input('请输入名字:').strip() password = input('请输入密码:').strip() password1 = input('请确认密码:').strip() if not name or not password: continue if password == password1: state, msg = student_interface.student_registe_interface(name, password) if state: print(msg) break else: print(msg) else: print('两次密码不一致') def student_login(): print('学生登录'.center(40, '-')) if student_info['name']: print('您已登录,无需再登录') return while True: name = input('请输入名字:').strip() password = input('请输入密码:').strip() if not name or not password: continue state, msg = common_interface.login_interface('student', name, password) if state: print(msg) student_info['name'] = name break else: print(msg) @common.auth(user_type='student') def choose_school(): print('选择学校'.center(40, '-')) while True: school_list = common_interface.check_all_school() if school_list: for i, school in enumerate(school_list): print('%s : %s' % (i, school)) choice = input('请选择(学校):').strip() if choice.isdigit(): choice = int(choice) if choice >= len(school_list): continue state, msg = student_interface.choice_school_interface(student_info['name'], school_list[choice]) if state: print(msg) break else: print(msg) break else: print('请输入数字') else: print('暂无学校') break @common.auth(user_type='student') def choose_course(): print('选择课程'.center(40, '-')) while True: state, msg = student_interface.check_all_course(student_info['name']) if state: for i, course in enumerate(msg): print('%s : %s' % (i, course)) choice = input('选择课程(学生):').strip() if choice.isdigit(): choice = int(choice) if choice >= len(msg): continue flag, mssg = student_interface.choice_course_interface(student_info['name'], msg[choice]) if flag: print(mssg) break else: print(mssg) break else: print('请输入数字') else: print(msg) break @common.auth(user_type='student') def check_score(): print('查看成绩'.center(40, '-')) score_dict = student_interface.check_score_interface(student_info['name']) for k, v in score_dict.items(): print('课程:%s\t 成绩:%s' % (k, v)) @common.auth(user_type='student') def check_log(): common_interface.check_log_interface(student_info['name']) @common.auth(user_type='student') def check_course(): print('查看课程信息'.center(40, '-')) state, msg = student_interface.check_stu_course_interface(student_info['name']) if state: pass else: print(msg) choice_dict = { '1': student_registe, '2': student_login, '3': choose_school, '4': choose_course, '5': check_score, '6': check_log, '7': check_course } def student_main(): while True: print( ''' 1. 注册 2. 登录 3. 选择学校 4. 选择课程 5. 查看成绩 6. 查看操作日志 7. 查看课程信息 q. 退出 ''' ) choice = input('请选择(学生):').strip() if choice == 'q': src.tag = False break if choice not in choice_dict: continue choice_dict[choice]()
#:coding:utf-8 from conf import settings import os, pickle def save(obj): '''数据处理保存''' path_dir = os.path.join(settings.BASE_DB, obj.__class__.__name__.lower()) if not os.path.isdir(path_dir): os.mkdir(path_dir) path_obj = os.path.join(path_dir, obj.name) with open(path_obj, 'wb') as f: pickle.dump(obj, f) def select(obj_name, dir_name): '''数据处理查询''' path_dir = os.path.join(settings.BASE_DB, dir_name) if not os.path.isdir(path_dir): os.mkdir(path_dir) path_obj = os.path.join(path_dir, obj_name) if os.path.exists(path_obj): with open(path_obj, 'rb')as f: return pickle.load(f) else: return None
#:coding:utf-8 from db import db_handler class BaseClass(): '''超类''' def save(self): db_handler.save(self) @classmethod def get_obj_name(cls, name): return db_handler.select(name, cls.__name__.lower()) class Admin(BaseClass): '''管理员类''' def __init__(self, name, password): self.name = name self.password = password self.save() def create_school(self, school_name, addr): School(school_name, addr) def create_course(self, name, price, period): Course(name, price, period) def create_teacher(self, name, password): Teacher(name, password) class Teacher(BaseClass): '''老师类''' def __init__(self, name, password): self.name = name self.password = password self.course_list = [] self.save() def add_course(self, course_name): self.course_list.append(course_name) self.save() def tea_tell_info(self): for cours in self.course_list: cours_obj=Course.get_obj_name(cours) cours_obj.tell_info() def mark_student(self,student,course_name,score): student.score[course_name] = score student.save() class Student(BaseClass): '''学生类''' def __init__(self, name, password): self.name = name self.password = password self.school = None self.course_list = [] self.score = {} self.save() def choice_school(self, schoolname): self.school = schoolname self.save() def add_course(self, course_name): self.course_list.append(course_name) self.score[course_name] = 0 self.save() def stu_tell_info(self): for cours in self.course_list: cours_obj=Course.get_obj_name(cours) cours_obj.tell_info() class School(BaseClass): '''学校类''' def __init__(self, name, addr): self.name = name self.addr = addr self.course_list = [] self.save() def add_course(self, course_name): self.course_list.append(course_name) self.save() class Course(BaseClass): '''课程类''' def __init__(self, name, price, period): self.name = name self.price = price self.period = period self.student_list = [] self.save() def add_student(self, stu_name): self.student_list.append(stu_name) self.save() def tell_info(self): print('课程名:%s 价格:%s 周期:%s' % (self.name, self.price, self.period))
#:coding:utf-8 from db import modules from lib import common import os from conf import settings logger = common.get_logger('操作日志') #公共:登录接口 def login_interface(user_type, name, password): if user_type == 'admin': obj = modules.Admin.get_obj_name(name) elif user_type == 'teacher': obj = modules.Teacher.get_obj_name(name) elif user_type == 'student': obj = modules.Student.get_obj_name(name) else: return False, '模式不存在' if obj: if obj.password == password: return True, '%s : %s登录成功' % (user_type, name) else: return False, '密码错误' else: return False, '用户不存在' #公共:查看所有所有学校 def check_all_school(): path = os.path.join(settings.BASE_DB, 'school') return common.get_all_obj(path) #公共:查看所有课程接口 def check_all_course(): path = os.path.join(settings.BASE_DB, 'course') return common.get_all_obj(path) #公共:查看日志记录接口 def check_log_interface(user_name): common.get_log_info(user_name)
#:coding:utf-8 from db import modules from lib import common logger = common.get_logger("操作日志") # 管理员:注册接口 def admin_registe_interface(name, password): admin_obj = modules.Admin.get_obj_name(name) if admin_obj: return False, '管理员已存在' else: modules.Admin(name, password) logger.info('管理员%s注册成功' % name) return True, '管理员注册成功' # 管理员:创建学校接口 def create_school_interface(admin, school_name, addr): school_obj = modules.School.get_obj_name(school_name) if school_obj: return False, '学校已存在' else: admin_obj = modules.Admin.get_obj_name(admin) admin_obj.create_school(school_name, addr) logger.info('%s:%s学校创建成功' % (admin, school_name)) return True, '%s:%s学校创建成功' % (admin, school_name) # 管理员:创建老师接口 def create_teacher_interface(admin, name, password='123'): teacher_obj = modules.School.get_obj_name(name) if teacher_obj: return False, '该教师已存在' else: admin_obj = modules.Admin.get_obj_name(admin) admin_obj.create_teacher(name, password) logger.info('%s:%s教师创建成功' % (admin, name)) return True, '%s:%s教师创建成功,初始密码为%s' % (admin, name, password) # 管理员:创建课程接口 def create_course_interface(admin, name, price, period, school_name): course_obj = modules.Course.get_obj_name(name) if course_obj: return False, '该课程已存在' else: admin_obj = modules.Admin.get_obj_name(admin) admin_obj.create_course(name, price, period) school_obj = modules.School.get_obj_name(school_name) school_obj.add_course(name) logger.info('%s:%s课程创建成功' % (admin, name)) return True, '%s:%s课程创建成功' % (admin, name)
#:coding:utf-8 from db import modules from lib import common logger = common.get_logger('操作日志') # 老师:选择课程接口 def choose_course_interface(tea_name, course_name): '''选择课程''' obj = modules.Teacher.get_obj_name(tea_name) if course_name in obj.course_list: return False, '课程已存在,无法添加' else: obj.add_course(course_name) logger.info('%s选择课程%s成功' % (tea_name, course_name)) return True, '%s选择课程%s成功' % (tea_name, course_name) # 老师:查看老师课程接口 def check_tea_course_interface(tea_name): '''查看课程''' tea_obj = modules.Teacher.get_obj_name(tea_name) if not tea_obj.course_list: return False, '暂无课程,请先选择课程', None # course_dict_info={} 方式二(未测试) # for course in tea_obj.course_list: # course_obj=modules.Course.get_obj_name(course) # course_dict_info[course_obj.name]={'价格':course_obj.price,'周期':course_obj.period} # return True,course_dict_info else: #方式一 return True, tea_obj.tea_tell_info(), tea_obj.course_list # 老师:查看学生接口 def check_student_interface(course_name): '''查看学生''' course_obj = modules.Course.get_obj_name(course_name) if not course_obj.student_list: return False, '暂无学生' return True, course_obj.student_list # 老师:为学生打分接口 def mark_student_interface(tea_name, stu_name, course_name, score): tea_obj = modules.Teacher.get_obj_name(tea_name) stu_obj = modules.Student.get_obj_name(stu_name) tea_obj.mark_student(stu_obj, course_name, score) logger.info('%s 修改 %s 的 %s 课程成绩成功!' % (tea_name, stu_name, course_name)) return '%s 修改 %s 的 %s 课程成绩成功!' % (tea_name, stu_name, course_name)
#:coding:utf-8 from db import modules from lib import common logger = common.get_logger('操作日志') # 学生:学生注册接口 def student_registe_interface(name, password): obj = modules.Student.get_obj_name(name) if obj: return False, '用户名已存在' else: modules.Student(name, password) logger.info('学生%s注册成功' % name) return True, '学生注册成功' # 学生:选择学校接口 def choice_school_interface(stu_name, school_name): obj = modules.Student.get_obj_name(stu_name) if obj.school: return False, '您已选择过学校,无法再次选择' else: obj.choice_school(school_name) logger.info('%s选择学校%s成功' % (stu_name, school_name)) return True, '%s选择学校%s成功' % (stu_name, school_name) # 学生:根据学校查看所有课程接口 def check_all_course(stu_name): stu_obj = modules.Student.get_obj_name(stu_name) if stu_obj.school: school_obj = modules.School.get_obj_name(stu_obj.school) if school_obj.course_list: return True, school_obj.course_list else: return False, '该学校暂无课程' else: return False, '您还没选择学校' # 学生:选择课程接口 def choice_course_interface(stu_name, course_name): stu_obj = modules.Student.get_obj_name(stu_name) if course_name in stu_obj.course_list: return False, '您已选择本门课程' stu_obj.add_course(course_name) course_obj = modules.Course.get_obj_name(course_name) course_obj.add_student(stu_name) logger.info('%s选择课程%s成功' % (stu_name, course_name)) return True, '%s选择课程%s成功' % (stu_name, course_name) # 学生:查看分数接口 def check_score_interface(stu_name): stu_obj = modules.Student.get_obj_name(stu_name) return stu_obj.score # 学生:查看学生课程信息接口 def check_stu_course_interface(stu_name): stu_obj = modules.Student.get_obj_name(stu_name) if stu_obj.course_list: return True, stu_obj.stu_tell_info() else: return False, '暂无课程,请先选择课程'
#:coding:utf-8 import logging.config,os from conf import settings def auth(user_type): from core import admin, teacher, student def validation(func): def wrapper(*args,**kwargs): if user_type=='admin': if not admin.admin_info['name']: admin.admin_login() else: func(*args,**kwargs) elif user_type=='teacher' : if not teacher.teacher_info['name']: teacher.teacher_login() else: func(*args,**kwargs) elif user_type=='student': if not student.student_info['name']: student.student_login() else: func(*args,**kwargs) return wrapper return validation def get_all_obj(path): '''得到路径下所有对象''' obj_path = os.path.join(settings.BASE_DB, path) if os.path.exists(obj_path): obj_list = os.listdir(obj_path) return obj_list else: return None def get_logger(name): '''日志模块''' logging.config.dictConfig(settings.LOGGING_DIC) logger = logging.getLogger(name) return logger def get_log_info(user_name): with open(settings.LOG_PATH,'rt',encoding='utf-8')as f: for names in f.readlines(): if user_name in names: print(names)
#:coding:utf-8 #===========================start.py========================= import os, sys BASE_DIR = os.path.dirname(__file__) sys.path.append(BASE_DIR) from core import src if __name__ == '__main__': src.run()
花了几天时间重写了下面向对象的作业,核心就在于modules 的基类 和 db_handler 里的数据处理上
然后,还有一些逻辑上的没完善(退出机制等)
之后就是测试和运行
写于 2018-09-13 11:14:00
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash