Python选课系统
一、程序介绍
需求:
开发一个简单的python计算器
角色:学校、学员、课程、讲师
要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
6. 创建讲师角色时要关联学校,
7. 提供两个角色接口
7.1 学员视图, 可以注册, 交学费, 选择班级,
7.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
7.3 管理视图,创建讲师, 创建班级,创建课程
8. 上面的操作产生的数据都通过pickle序列化保存到文件里
程序结构:
ElectiveSys #选课系统主目录
├── bin #选课系统 执行文件 目录
│ └── start.py #执行程序
├── conf #配置文件 目录
│ ├── __pycache__
│ │ └── settings.cpython-35.pyc
│ └── settings.py #配置文件
├── db #数据存储 目录
│ ├── admin #管理员用户数据目录
│ │ └── 09fc65b4-bfdc-11e7-811c-8c8590496b2f
│ ├── classes #班级数据目录
│ │ └── ff63f0ee-c045-11e7-9b0d-8c8590496b2f
│ ├── course #课程数据目录
│ │ ├── 4ff1873e-b38c-11e6-82f5-0088653ea1ec
│ │ └── 66214534-c03c-11e7-9a2c-8c8590496b2f
│ ├── course_to_teacher
│ ├── school #学校数据目录
│ │ ├── 93a3a674-bfdc-11e7-8e2e-8c8590496b2f
│ │ ├── 9b6a07f4-bfdc-11e7-b768-8c8590496b2f
│ │ ├── a67fd088-bfdc-11e7-91eb-8c8590496b2f
│ │ ├── af08c21e-bfdc-11e7-bee2-8c8590496b2f
│ │ └── b62fc1e6-bfdc-11e7-93c8-8c8590496b2f
│ ├── student #学生数据目录
│ │ └── 6bacae38-c048-11e7-8123-8c8590496b2f
│ └── teacher #老师数据目录
│ ├── 1f7c0f74-bfdd-11e7-aa97-8c8590496b2f
│ ├── 33f09c2c-bfdd-11e7-bcb8-8c8590496b2f
│ └── 42198dd8-bfdd-11e7-a1ef-8c8590496b2f
├── lib #存放基本共享库和模块的目录
│ ├── __pycache__
│ │ └── commons.cpython-36.pyc
│ └── commons.py #功能模块文件
├── log #日志目录
└── src #程序主逻辑源码目录
├── __pycache__
│ ├── identifier.cpython-36.pyc
│ └── models.cpython-36.pyc
├── identifier.py #识别码文件
├── models.py #模板类文件
└── services #服务类目录
├── __pycache__
│ ├── admin_service.cpython-36.pyc
│ ├── initialize_service.cpython-36.pyc
│ ├── student_service.cpython-36.pyc
│ └── teacher_service.cpython-36.pyc
├── admin_service.py #管理员功能文件
├── initialize_service.py #初始化功能文件
├── student_service.py #学生功能文件
└── teacher_service.py #老师功能文件
二、流程图
三、代码
bin/start.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 import os,sys 4 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 sys.path.append(BASE_DIR) 6 7 from src.services import admin_service 8 from src.services import teacher_service 9 from src.services import student_service 10 from src.services import initialize_service 11 12 def show_role(): 13 msg=‘‘‘ 14 0:初始化 15 1:管理员 16 2:老师 17 3:学生 18 ‘‘‘ 19 print(msg) 20 21 if __name__ == ‘__main__‘: 22 role_main={ 23 ‘0‘:initialize_service.main, 24 ‘1‘:admin_service.login, 25 ‘2‘:teacher_service.login, 26 ‘3‘:student_service.login, 27 } 28 while True: 29 show_role() 30 choice=input(‘输入角色: ‘).strip() 31 if choice not in role_main:continue 32 role_main[choice]()
conf/settings.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 import os 4 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 6 ADMIN_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘admin‘) 7 SCHOOL_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘school‘) 8 TEACHER_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘teacher‘) 9 COURSE_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘course‘) 10 COURSE_TO_TEACHER_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘course_to_teacher‘) 11 CLASSES_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘classes‘) 12 STUDENT_DB_DIR=os.path.join(BASE_DIR,‘db‘,‘student‘)
lib/commons.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 import uuid,hashlib 4 import time 5 6 def create_uuid(): 7 return str(uuid.uuid1()) 8 9 def create_md5(): 10 m=hashlib.md5() 11 m.update(bytes(str(time.time()),encoding=‘utf-8‘)) 12 return m.hexdigest() 13 14 if __name__ == ‘__main__‘: 15 x=create_md5() 16 17 print(x) 18 19 y=create_uuid() 20 print(y)
src/models.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 import time 4 import pickle 5 import os 6 from conf import settings 7 from src import identifier 8 9 10 class BaseModel: 11 def save(self): 12 file_path=os.path.join(self.db_path,str(self.nid)) 13 pickle.dump(self,open(file_path,‘wb‘)) 14 15 @classmethod 16 def get_all_obj_list(cls): 17 ret=[] 18 for filename in os.listdir(cls.db_path): 19 file_path=os.path.join(cls.db_path,filename) 20 ret.append(pickle.load(open(file_path,‘rb‘))) 21 return ret 22 23 class Admin(BaseModel): 24 db_path=settings.ADMIN_DB_DIR 25 def __init__(self,username,password): 26 self.nid=identifier.AdminNid(self.db_path) 27 self.username=username 28 self.password=password 29 self.create_time=time.strftime(‘%Y-%m-%d‘) 30 31 @staticmethod 32 def login(): 33 try: 34 name=input(‘请输入用户名: ‘).strip() 35 pas=input(‘请输入密码: ‘).strip() 36 for obj in Admin.get_all_obj_list(): 37 if obj.username == name and obj.password == pas: 38 status = True 39 error=‘‘ 40 data=‘\033[45;1m登录成功\033[0m‘ 41 break 42 else: 43 raise Exception(‘\033[43;1m用户名或密码错误\033[0m‘ %name) 44 except Exception as e: 45 status=False 46 error=str(e) 47 data=‘‘ 48 return {‘status‘:status,‘error‘:error,‘data‘:data} 49 50 51 class School(BaseModel): 52 db_path=settings.SCHOOL_DB_DIR 53 def __init__(self,name,addr): 54 self.nid=identifier.SchoolNid(self.db_path) 55 self.name=name 56 self.addr=addr 57 self.create_time=time.strftime(‘%Y-%m-%d %X‘) 58 self.__income=0 59 60 def __str__(self): 61 return self.name 62 63 class Teacher(BaseModel): 64 db_path=settings.TEACHER_DB_DIR 65 def __init__(self,name,level): 66 self.nid=identifier.TeacherNid(self.db_path) 67 self.name=name 68 self.level=level 69 self.__account=0 70 self.create_time=time.strftime(‘%Y-%m-%d %X‘) 71 72 class Course(BaseModel): 73 db_path=settings.COURSE_DB_DIR 74 def __init__(self,name,price,period,school_nid): 75 self.nid=identifier.CourseNid(self.db_path) 76 self.name=name 77 self.price=price 78 self.period=period 79 self.school_nid=school_nid 80 81 class Course_to_teacher(BaseModel): 82 db_path=settings.COURSE_TO_TEACHER_DB_DIR 83 def __init__(self,course_nid,school_nid): 84 self.nid=identifier.Course_to_teacherNid(self.db_path) 85 self.course_nid=course_nid 86 self.school_nid=school_nid 87 88 def get_course_to_teacher_list(self): 89 ret=self.get_all_obj_list() 90 if ret: 91 return [ret.course_nid.get_obj_by_uuid(),ret.classes_nid.get_obj_by_uuid()] 92 return [None,None] 93 94 class Classes(BaseModel): 95 db_path=settings.CLASSES_DB_DIR 96 def __init__(self,name): 97 # def __init__(self,name,tuition,school_nid,course_to_teacher_list): 98 self.nid=identifier.ClassesNid(self.db_path) 99 self.name=name 100 # self.tuition=tuition 101 # self.school_nid=school_nid 102 # self.course_to_teacher_list=course_to_teacher_list 103 self.create_time = time.strftime(‘%Y-%m-%d %X‘) 104 105 class Score: 106 def __init__(self,nid): 107 self.nid=nid 108 self.score_dict={} 109 110 def set(self,course_to_teacher_nid,number): 111 self.score_dict[course_to_teacher_nid]=number 112 113 def get(self,course_to_teacher_nid): 114 return self.score_dict.get(course_to_teacher_nid) 115 116 class Student(BaseModel): 117 db_path=settings.STUDENT_DB_DIR 118 def __init__(self,name,age,qq): 119 # def __init__(self,name,age,qq,classes_nid): 120 self.nid=identifier.StudentNid(self.db_path) 121 self.name=name 122 self.age=age 123 self.qq=qq 124 # self.classes_nid=classes_nid 125 # self.score=Score(self.nid) 126 self.create_time = time.strftime(‘%Y-%m-%d %X‘)
src/identifier.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 from lib import commons 4 import os,pickle 5 6 class Nid: 7 def __init__(self,role,db_path): 8 role_list=[ 9 ‘admin‘,‘school‘,‘teacher‘,‘course‘,‘course_to_teacher‘,‘classes‘,‘student‘ 10 ] 11 if role not in role_list: 12 raise Exception(‘用户角色错误,选项: %s‘ % ‘,‘.join(role_list)) 13 self.role=role 14 self.uuid=commons.create_uuid() 15 self.db_path=db_path 16 17 def __str__(self): 18 return self.uuid 19 20 def get_obj_by_uuid(self): 21 for filename in os.listdir(self.db_path): 22 if filename == self.uuid: 23 return pickle.load(open(os.path.join(self.db_path,filename),‘rb‘)) 24 return None 25 26 class AdminNid(Nid): 27 def __init__(self,db_path): 28 super(AdminNid,self).__init__(‘admin‘,db_path) 29 30 class SchoolNid(Nid): 31 def __init__(self,db_path): 32 super(SchoolNid,self).__init__(‘school‘,db_path) 33 34 class TeacherNid(Nid): 35 def __init__(self,db_path): 36 super(TeacherNid,self).__init__(‘teacher‘,db_path) 37 38 class CourseNid(Nid): 39 def __init__(self,db_path): 40 super(CourseNid,self).__init__(‘course‘,db_path) 41 42 class Course_to_teacherNid(Nid): 43 def __init__(self,db_path): 44 super(Course_to_teacherNid,self).__init__(‘course_to_teacher‘,db_path) 45 46 class ClassesNid(Nid): 47 def __init__(self,db_path): 48 super(ClassesNid,self).__init__(‘classes‘,db_path) 49 50 class StudentNid(Nid): 51 def __init__(self,db_path): 52 super(StudentNid,self).__init__(‘student‘,db_path)
src/services/admin_service.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 from src.models import Admin 4 from src.models import School 5 from src.models import Teacher 6 from src.models import Course 7 from src.models import Course_to_teacher 8 from src.models import Classes 9 from src.models import Score 10 from src.models import Student 11 def create_school(): 12 try: 13 name=input(‘请输入学校名字: ‘).strip() 14 addr=input(‘请输入学校地址: ‘).strip() 15 school_name_list=[(obj.name,obj.addr) for obj in School.get_all_obj_list()] 16 if (name,addr) in school_name_list: 17 raise Exception(‘\033[43;1m[%s] [%s]校区 已经存在,不可重复创建\033[0m‘ %(name,addr)) 18 obj=School(name,addr) 19 obj.save() 20 status=True 21 error=‘‘ 22 data=‘\033[33;1m[%s] [%s]校区 创建成功\033[0m‘ %(obj.name,obj.addr) 23 except Exception as e: 24 status=False 25 error=str(e) 26 data=‘‘ 27 return {‘status‘:status,‘error‘:error,‘data‘:data} 28 29 30 def show_school(): 31 for obj in School.get_all_obj_list(): 32 print(‘\033[45;1m学校[%s] 地址[%s] 创建日期[%s]\033[0m‘.center(60,‘-‘) 33 %(obj.name,obj.addr,obj.create_time)) 34 35 def create_teacher(): 36 try: 37 name=input(‘请输入老师姓名: ‘).strip() 38 level=input(‘请输入老师级别: ‘).strip() 39 teacher_name_list=[obj.name for obj in Teacher.get_all_obj_list()] 40 if name in teacher_name_list: 41 raise Exception(‘\033[43;1m老师[%s] 已经存在,不可重复创建\033[0m‘ %(name)) 42 obj=Teacher(name,level) 43 obj.save() 44 status=True 45 error=‘‘ 46 data=‘\033[33;1m老师[%s] 级别[%s] 时间[%s]创建成功\033[0m‘ %(obj.name,obj.level,obj.create_time) 47 except Exception as e: 48 status=False 49 error=str(e) 50 data=‘‘ 51 return {‘status‘:status,‘error‘:error,‘data‘:data} 52 53 54 def show_teacher(): 55 for obj in Teacher.get_all_obj_list(): 56 print(‘\033[33;1m老师[%s] 级别[%s] 创建时间[%s]\033[0m‘.center(60,‘-‘) 57 %(obj.name,obj.level,obj.create_time)) 58 59 60 def create_course(): 61 try: 62 print(‘创建课程‘.center(60,‘=‘)) 63 school_list=School.get_all_obj_list() 64 for k,obj in enumerate(school_list): 65 print(k,obj,obj.addr) 66 sid=int(input(‘请选择学校: ‘)) 67 school_obj=school_list[sid] 68 69 name=input(‘请输入课程名: ‘).strip() 70 price=input(‘请输入课程价格: ‘).strip() 71 period=input(‘请输入课程周期: ‘).strip() 72 73 course_name_list=[(obj.name,obj.school_nid.uuid) for obj in Course.get_all_obj_list()] 74 if (name,school_obj.nid.uuid) in course_name_list: 75 raise Exception(‘\033[43;1m课程[%s] 已经存在,不可重复创建\033[0m‘ %(name)) 76 obj=Course(name,price,period,school_obj.nid) 77 obj.save() 78 status=True 79 error=‘‘ 80 data=‘\033[33;1m课程[%s] 价格[%s] 周期[%s]创建成功\033[0m‘ %(obj.name,obj.price,obj.period) 81 except Exception as e: 82 status=False 83 error=str(e) 84 data=‘‘ 85 return {‘status‘:status,‘error‘:error,‘data‘:data} 86 87 def show_course(): 88 for obj in Course.get_all_obj_list(): 89 # print(‘\033[33;1m[%s] [%s]校区 [%s]课程 价格[%s] 周期[%s]\033[0m‘.center(60,‘-‘) \ 90 # %(obj.school_nid.get_obj_by_uuid().name,\ 91 # obj.school_nid.get_obj_by_uuid().addr,\ 92 # obj.name,obj.price,obj.period)) 93 print(‘\033[33;1m[%s]课程 价格[%s] 周期[%s]\033[0m‘.center(60,‘-‘) 94 %(obj.name,obj.price,obj.period)) 95 96 def create_course_to_teacher(): 97 pass 98 99 def create_classes(): 100 try: 101 name=input(‘请输入班级名:‘).strip() 102 classes_name_list=[obj.name for obj in Classes.get_all_obj_list()] 103 if name in classes_name_list: 104 raise Exception(‘\033[43;1m班级[%s] 已经存在,不可重复创建\033[0m‘ %(name)) 105 obj=Classes(name) 106 obj.save() 107 status=True 108 error=‘‘ 109 data=‘\033[33;1m 班级[%s] 时间[%s]创建成功\003[0m‘ %(obj.name,obj.create_time) 110 except Exception as e: 111 status=False 112 error=str(e) 113 data=‘‘ 114 return {‘status‘:status,‘error‘:error,‘data‘:data} 115 116 117 def show_classes(): 118 for obj in Classes.get_all_obj_list(): 119 print(‘\033[33;1m 班级[%s] 创建时间[%s]\033[0m‘.center(60, ‘-‘) 120 %(obj.name,obj.create_time)) 121 122 def create_student(): 123 try: 124 name=input(‘请输入姓名:‘).strip() 125 age=input(‘请输入年龄:‘).strip() 126 qq=input(‘请输入QQ:‘).strip() 127 student_name_list=[(obj.name,obj.age,obj.qq) for obj in Student.get_all_obj_list()] 128 if name in student_name_list: 129 raise Exception(‘\033[43;1m姓名[%s] 已经存在,不可重复创建\033[0m‘ %(name)) 130 obj=Student(name,age,qq) 131 obj.save() 132 status=True 133 error=‘‘ 134 data=‘\033[33;1m 姓名[%s] 年龄[%s] QQ[%s] 时间[%s]创建成功\003[0m‘ 135 %(obj.name,obj.age,obj.qq,obj.create_time) 136 except Exception as e: 137 status=False 138 error=str(e) 139 data=‘‘ 140 return {‘status‘:status,‘error‘:error,‘data‘:data} 141 142 def show_student(): 143 for obj in Student.get_all_obj_list(): 144 print(‘\033[33;1m 姓名[%s] 年龄[%s] QQ[%s] 创建时间[%s]\033[0m‘.center(60, ‘-‘) 145 %(obj.name,obj.age,obj.qq,obj.create_time)) 146 147 148 def show(): 149 msg=‘‘‘ 150 0:选项 151 1:创建学校 152 2:查看学校 153 3:创建老师 154 4:查看老师 155 5:创建课程 156 6:查看课程 157 7:关联老师与课程 158 8:创建班级 159 9:查看班级 160 10:创建学生 161 11:查看学生 162 12:退出 163 ‘‘‘ 164 print(msg) 165 166 167 def main(): 168 choice_dic={ 169 ‘0‘:show, 170 ‘1‘:create_school, 171 ‘2‘:show_school, 172 ‘3‘:create_teacher, 173 ‘4‘:show_teacher, 174 ‘5‘:create_course, 175 ‘6‘:show_course, 176 ‘7‘:create_course_to_teacher, 177 ‘8‘:create_classes, 178 ‘9‘:show_classes, 179 ‘10‘:create_student, 180 ‘11‘:show_student, 181 ‘12‘:exit 182 } 183 show() 184 while True: 185 choice=input(‘请输入选项: ‘).strip() 186 if choice not in choice_dic:continue 187 ret=choice_dic[choice]() 188 if ret: 189 if ret[‘status‘]: 190 print(ret[‘data‘].center(60,‘-‘)) 191 else: 192 print(ret[‘error‘].center(60,‘-‘)) 193 194 195 def login(): 196 ret=Admin.login() 197 if ret: 198 if ret[‘status‘]: 199 print(ret[‘data‘].center(60,‘-‘)) 200 main() 201 else: 202 print(ret[‘error‘].center(60,‘-‘)) 203 204 if __name__ == ‘__main__‘: 205 main()
src/services/initialize.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 import getpass 4 from src.models import Admin 5 6 def initialize(): 7 try: 8 user = input(‘请输入初始化用户名:‘) 9 # pwd = getpass.getpass(‘请输入初始化密码:‘) 10 pwd = input(‘请输入初始化密码:‘) 11 obj = Admin(user, pwd) 12 obj.save() 13 return True 14 except Exception as e: 15 print(e) 16 17 18 def main(): 19 show = """ 20 1. 初始化管理员账户 21 """ 22 choice_dict = { 23 ‘1‘: initialize 24 } 25 while True: 26 print(show) 27 choice = input(‘请输入操作选项:‘) 28 if choice not in choice_dict: 29 print(‘选项错误,请重新输入!!!‘) 30 func = choice_dict[choice] 31 ret = func() 32 if ret: 33 print(‘操作成功‘) 34 return 35 else: 36 print(‘操作异常,请重新操作‘)
src/services/student
_service.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 from src.models import Student 4 5 class register: 6 """ 7 学生注册 8 """ 9 10 class score: 11 """ 12 学生查看个人成绩 13 """ 14 15 def main(): 16 pass 17 18 def login(): 19 pass
src/services/teacher_service.py
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 4 def class_info(): 5 """ 6 查看班级信息 7 """ 8 9 def student_info(): 10 """ 11 查看学生信息 12 """ 13 14 def set_student_score(): 15 """ 16 设置学生分数 17 """ 18 19 def main(): 20 pass 21 22 def login(): 23 pass
时间: 2024-11-09 03:56:38