python编辑选课系统

一.需求分析

  1. 创建北京、上海 2 所学校
  2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
  3. 课程包含,周期,价格,通过学校创建课程 
  4. 通过学校创建班级, 班级关联课程、讲师
  5. 创建学员时,选择学校,关联班级
  6. 创建讲师角色时要关联学校, 
  6. 提供两个角色接口
  6.1 学员视图, 可以注册, 交学费, 选择班级,
  6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩 
  6.3 管理视图,创建讲师, 创建班级,创建课程

  7. 上面的操作产生的数据都通过pickle序列化保存到文件里

二.程序设计原理图

python编辑选课系统

三.代码实现

  工程的创建按照较为简便的格式来描述,大致目录结构如图:

选课系统/
|-- bin
| |-- __init__.py
|
|-- db/
| |--database/
|   |-- 北京电影学院.pickle
|   |-- 上海戏剧学院.pickle
|  |-- __init__.py
|  
|-- core/
| |-- __init__.py
| |-- db_handle.py
|  |-- db_opt.py
|  |-- main.py
| |-- school_class.py
|
|-- conf/
| |-- __init_py.py
| |-- setting.py
|
|-- __init__.py
|-- requirements.txt
|-- README   setting.py程序代码如下:
 import os
import sys BASE_DIR = os.path.abspath(r'..') # print(os.environ) DATA_BASE ={
'engine' :'file_storage',
'suffix':'pickle',
'name': 'database',
'path':'%s\db'%BASE_DIR
} # print(DATA_BASE['path'])

  db_handle.py程序代码如下:

 '''
handle all the database interactions
''' def file_handle_db(conn_param):
'''
parse the db file path
:param conn_param: the db connection params set in settings
:return: file path
'''
db_path = '%s'%conn_param['path']
return db_path def mysql_db_handle(conn_parms):
''' :param conn_parms:
:return:
'''
pass def db_handle(conn_param):
'''
prase all db type
:param conn_param: db config
:return:
'''
if conn_param['engine'] == 'file_storage':
return file_handle_db(conn_param)
elif conn_param['engine'] == 'mysql':
return mysql_db_handle(conn_param)
else:
pass

  db_opt.py程序代码如下:

 import pickle
import json
import os from 课后作业.选课系统.conf import setting
from 课后作业.选课系统.core import db_handle def file_opt_read(account_file,conn_params):
'''
use pickle to load data
:param account_file: file path
:param conn_params: data_base config
:return: file data
'''
with open(account_file, 'rb') as f:
if conn_params['suffix'] == 'pickle':
account_data = pickle.load(f)
elif conn_params['suffix'] == 'json':
account_data = json.load(f)
else:
return
return account_data def file_opt_wtite(school_file,account_data,conn_params):
'''
use pickle to dump data
:param school_file: file path
:param account_data: jump data
:param conn_params: data_base config
:return:
'''
with open(school_file, 'wb') as f:
if conn_params['suffix'] == 'pickle':
acc_data = pickle.dump(account_data, f)
elif conn_params['suffix'] == 'json':
acc_data = json.dump(account_data, f)
return True def database_read(name):
'''
to read school data from database
:param name: file name
:return:
'''
db_path = db_handle.db_handle(setting.DATA_BASE)#获取路径
account_file = "%s\%s\%s.%s" % (db_path,setting.DATA_BASE['name'],name,setting.DATA_BASE['suffix'])
if os.path.isfile(account_file):
return file_opt_read(account_file,setting.DATA_BASE)
else:
return False def database_write(account_data):
'''
after updated transaction or account data , dump it back to file db
:param account_data:
:return:
'''
db_path = db_handle.db_handle(setting.DATA_BASE)
school_file = "%s/%s/%s.%s" %(db_path,setting.DATA_BASE['name'],account_data.name,setting.DATA_BASE['suffix'])
return file_opt_wtite(school_file,account_data,setting.DATA_BASE)

  school_class.py程序代码如下:

 from 课后作业.选课系统.core  import db_opt

 class Course(object):
'''
create a course class
:param name:Course name price: Course price time :Course learning cycle
:return:
'''
def __init__(self,name,price,time):
self.name = name
self.price = price
self.time = time def tell(self):
print('''
--- Info of Course [%s] ---
Name = %s
Price = %s
Time = %s
'''%(self.name,self.name,self.price,self.time)) class Class(object):
'''
create a class class 创建一个班级类
:param name:class name ,course:a course class ,teacher:a teacher class
:return:
'''
def __init__(self,name,course,teacher):
self.name = name
self.course = course
self.teacher = teacher
self.student = []
def tell(self):
print('''
--- Info of %s ---
Class :%s
Course :%s
Teacher :%s
'''%(self.name,self.name,self.course,self.teacher)) class School(object):
'''
create a school class 创建一个班级类
:param name:school name ,addr:school addr ,teachers[]:a list save in memory that info of teachers be hired
:param students[]:a list save in memory that info of students be enrolled
:param courses[]:a list save in memory that info of courses be created
:param classes[]:a list save in memory that info of classes be created
:return:
'''
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.teachers = []
self.students = []
self.courses = []
self.classes = []
def tell(self):
print('''
--- Info of School :%s ---
Name : %s
Addr : %s
'''%(self.name,self.name,self.addr)) def hire(self,teacher,salary):
teacher.school = self.name
teacher.salary =salary
self.teachers.append(teacher)
print("%s has hire %s to be a teacher"%(self.name,teacher.name)) def enroll(self,student,student_class):
self.students.append(student)
student_class.student.append(student)
student.choose_school(self.name)
student.choose_class(student_class)
print("%s has enroll %s to be a student"%(self.name,student.name)) def create_course(self,course_name,price,time):#创建课程类
self.courses.append(Course(course_name,price,time))
print("%s has creat course[%s]"%(self.name,course_name)) def create_class(self,Class_name,course,teacher):
info = Class(Class_name,course.name,teacher.name)
self.classes.append(info)
teacher.Class.append(info) class SchoolMember(object):
'''
it's a base class include of teacher and student
:param
:return
'''
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def tell(self):#个人信息,子类来完善
pass class Teacher(SchoolMember):
'''
it's a subclass class Inheritance by SchoolMember to create a Teacher object
:param
:return
'''
def __init__(self,name,age,sex,course,salary='null',school='null'):
super(Teacher,self).__init__(name,age,sex)
self.salary = salary
self.course = course
self.school = school
self.Class =[] def tell(self):
print('''
--- Info of %s ---
Name = %s
Age = %s
Sex = %s
Salary = %s
Course = %s
Shool = %s
'''%(self.name,self.name,self.age,self.sex,self.salary,self.course,self.school)) class Student(SchoolMember):
'''
it's a subclass class Inheritance by SchoolMember to create a Student object
:param
:return
'''
def __init__(self, name, age, sex,school='null',grade='null',Class='null',tuition = False):
super(Student, self).__init__(name, age, sex)
self.__school = school
self.grade = grade
self.__Class = Class
self.__tuition = tuition def choose_school(self,name):
self.__school = name def choose_grade(self,grade):
self.grade = grade
print("%s grade change success !!!"%self.name) def choose_class(self,Class):
self.__Class = Class.name
print("%s choose class success !!!"%self.name) def tuition(self):
self.__tuition = True
print("%s tuituin success !!!"%self.name) def tell(self):
print('''
--- Info of %s ---
Name = %s
Age = %s
Sex = %s
School = %s
Class = %s
Grade = %s
tuition = %s
'''%(self.name,self.name,self.age,self.sex,self.__school,self.__Class,self.__grade,self.__tuition))

  main.py程序代码如下:

 from 课后作业.选课系统.core  import db_opt
from 课后作业.选课系统.core import school_class def creat_school():
'''
create a school class
:return:
'''
name = input('please input the school name:').strip()
addr = input('Plsase input the school addr:').strip() School = school_class.School(name,addr)
if(db_opt.database_write(School)):
print("\033[31;1mSchool [%s] has be created!\033[0m"%School.name)
return School
else:
return False def read_school_info(school):
'''
load school information from data base
:param school: school name
:return: school data
'''
return db_opt.database_read(school)
def write_school_info(school):
'''
dump school information from data base
:param school: school name
:return: school data
'''
return db_opt.database_write(school) '''
student interface
'''
def student_enroll(school):
'''
to handle the student enroll
:param school: a school class
:return:
'''
name = input("please input the student name : ")
age = input("please input the student age : ")
sex = input("please input the student sex : ")
print("---------info of class----------")
for i,info in enumerate(school.classes):
print('%s. %s %s %s '%(i+1,info.name,info.course,info.teacher))
student_num = int(input((">>:")).strip()) - 1 student= school_class.Student(name,age,sex)#creat a student class
school.enroll(student,school.classes[student_num])#enroll a student
write_school_info(school)
def student_pay(school):
'''
to handle the student tuition
:param school: a school class
:return:
'''
name = input("please input student name : ")
for info in school.students:
if info.name == name:
info.tuition()
write_school_info(school)
def student_choose_class(school):
pass
def studnt_view(school):
'''
student interface
:param school: a school class
:return:
'''
menu = u'''
------- Bank ---------
\033[32;1m
1. 注册
2. 缴费
3. 选择班级
4. 退出
\033[0m'''
menu_dic = {
'': student_enroll,
'': student_pay,
'': student_choose_class,
}
exit_flag = False
while not exit_flag:
print(menu)
user_option = input(">>:").strip()
if user_option in menu_dic:
menu_dic[user_option](school)
else:
print("\033[31;1mOption does not exist!\033[0m") '''
teacher interface
'''
def teacher_teach(school):
'''
handle the interaction of teacher choose class to teach
:param school: a school class
:return:
'''
name = input("please input your name : ").strip()
print("------class info------")
for info in school.teachers:
if info.name == name :
for i,index in enumerate(info.Class):
print("%s. %s "%(i+1,index.name)) user_option = int(input(">>:").strip())-1
print("%s has teach course[%s] in class[%s]"%(school.teachers[user_option].name,school.teachers[user_option].course,school.teachers[user_option].Class[user_option].name))
def teacher_student_view(school):
'''
a interface for teacher to view student
:param school: a school class
:return:
'''
print("-----------info of classes----------")
for i,info in enumerate(school.classes):
print("%s. %s"%(i+1,info.name))
user_option = int(input(">>:").strip())-1
for info in school.classes[user_option].student:
print(info.name)
def teacher_change_grade(school):
'''
a interface for teacher to change student grade
:param school: a school class
:return:
'''
print("-----------info of classes----------")
for i,info in enumerate(school.classes):
print("%s. %s"%(i+1,info.name))
user_option = int(input(">>:").strip())-1
for i,info in enumerate(school.classes[user_option].student):
print("%s. %s %s "%(i+1,info.name,info.grade))
stu_option = int(input(">>:").strip()) - 1
grade= int(input("pelase input the Student %s new grade : "%(school.classes[user_option].student[stu_option].name)).strip())
school.classes[user_option].student[stu_option].choose_grade(grade)
write_school_info(school)
def teacher_view(school):
'''
teacher interface
:param school: a school class
:return:
'''
menu = u'''
------- Bank ---------
\033[32;1m
1. 上课
2. 查看成员
3. 修改成绩
4. 退出
\033[0m'''
menu_dic = {
'': teacher_teach,
'': teacher_student_view,
'': teacher_change_grade,
}
exit_flag = False
while not exit_flag:
print(menu)
user_option = input(">>:").strip()
if user_option in menu_dic:
menu_dic[user_option](school)
else:
print("\033[31;1mOption does not exist!\033[0m") '''
manage interface
'''
def hire_teacher(school):
'''
to hire a teacher by school
:param school: a school class
:return: true
'''
name = input("please input the teacher name : ")
age = input("please input the teacher age : ")
sex = input("plesse input the teacher sex : ")
course = input("please input the teach course : ")
salary = input("please input the teacher salary : ")
teacher = school_class.Teacher(name,age,sex,course)
school.hire(teacher,salary)
write_school_info(school)
return True
def create_class(school):
'''
to create a class(班级) by school
:param school: a school class
:return: true
'''
classname = input("please input the class name : ")
print("----------info of course-----------")
for i,info in enumerate(school.courses):
print("%s. %s"%(i+1,info.name))
course_num = int(input((">>:")).strip())-1
print("----------info of teacher----------")
for i,info in enumerate(school.teachers):
print("%s. teacher name:%s course:%s"%(i+1,info.name,info.course))
teacher_num = int(input((">>:")).strip())-1 school.create_class(classname,school.courses[course_num],school.teachers[teacher_num])
write_school_info(school)
def create_course(school):
'''
to create a course by school
:param school: a school class
:return: true
'''
name = input("please input the course name : ").strip()
price = input("please input the course price : ").strip()
time = input("please input the course time : ").strip()
school.create_course(name,price,time)
write_school_info(school)
def manage_view(school):
'''
manage interface
:param school:
:return:
'''
menu = u'''
------- Bank ---------
\033[32;1m
1. 创建讲师
2. 创建班级
3. 创建课程
4. 退出
\033[0m'''
menu_dic = {
'': hire_teacher,
'': create_class,
'': create_course,
}
exit_flag = False
while not exit_flag:
print(menu)
user_option = input(">>:").strip()
if user_option in menu_dic:
menu_dic[user_option](school)
else:
print("\033[31;1mOption does not exist!\033[0m") def main():
'''
interact with user
:return:
''' choose = input("please input your school: \n1.北京电影学院\n2.上海戏剧学院\n>> :") if choose == '':
school = read_school_info("北京电影学院")
elif choose == '':
school = read_school_info("上海戏剧学院") menu = u'''
------- Bank ---------
\033[32;1m
1. 学员视图
2. 讲师视图
3. 管理视图
4. 退出
\033[0m'''
menu_dic = {
'': studnt_view,
'': teacher_view,
'': manage_view,
}
exit_flag = False
while not exit_flag:
print(menu)
user_option = input(">>:").strip()
if user_option in menu_dic:
menu_dic[user_option](school)
else:
print("\033[31;1mOption does not exist!\033[0m") main()


上一篇:Linux中逻辑卷(LVM)管理基本操作


下一篇:Linux Pin Control 子系统