Python选课系统
Posted 朱志文
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python选课系统相关的知识,希望对你有一定的参考价值。
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) 以上是关于Python选课系统的主要内容,如果未能解决你的问题,请参考以下文章