python编辑选课系统
一.需求分析
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
6. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
二.程序设计原理图
三.代码实现
工程的创建按照较为简便的格式来描述,大致目录结构如图:
选课系统/
|-- 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()
python编辑选课系统的更多相关文章
- Python作业-选课系统
目录 Python作业-选课系统 days6作业-选课系统: 1. 程序说明 2. 思路和程序限制 3. 选课系统程序目录结构 4. 测试帐户说明 5. 程序测试过程 title: Python作业- ...
- python之选课系统详解[功能未完善]
作业需求 思路:1.先写出大体的类,比如学校类,学生类,课程类-- 2.写出类里面大概的方法,比如学校类里面有创建讲师.创建班级-- 3.根据下面写出大致的代码,并实现其功能 遇到的困 ...
- Python作业选课系统(第六周)
作业需求: 角色:学校.学员.课程.讲师.完成下面的要求 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 ...
- [ python ] 面向对象 - 选课系统
根据源程序进行改写: 原程序地址:http://www.cnblogs.com/lianzhilei/p/5985333.html 如有侵权立即删除. 感谢原作者将完整的代码提供参考. ...
- Python 28 选课系统的讲解
1.首先我们要对每一个新的项目有一个明确的思路,脑子是好东西,但是好记性不如烂笔头,所以,要把能想到的都写下来 2.然后就是创建项目的整体结构框架,比如说:conf ( 配置文件 ) .core ( ...
- 一个简单的python选课系统
下面介绍一下自己写的python程序,主要是的知识点为sys.os.json.pickle的模块应用,python程序包的的使用,以及关于类的使用. 下面是我的程序目录: bin是存放一些执行文件co ...
- Python开发程序:选课系统-改良版
程序名称: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...
- python实现学生选课系统 面向对象的应用:
一.要求: 选课系统 管理员: 创建老师:姓名.性别.年龄.资产 创建课程:课程名称.上课时间.课时费.关联老师 使用pickle保存在文件 学生: 学生:用户名.密码.性别.年龄.选课列表[].上课 ...
- Python开发程序:选课系统
本节作业: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...
随机推荐
- virtualbox+vagrant学习-2(command cli)-16-vagrant snapshot命令
Snapshot快照 这是用于管理客户机器快照的命令.快照记录客户计算机的时间点状态.然后可以快速恢复到此环境.这可以让你进行试验和尝试,并迅速恢复到以前的状态. 快照并不是每个provider都支持 ...
- git问题整理
//1.git常用命令,git的branch 2.git的原理 //4.怎么同步到本地仓库,怎么传到远程仓库 //3.git中 rebase 和 merge的区别 5.git的使用,讲一下? //4. ...
- python自动化之models 进阶操作二
################################################################## # PUBLIC METHODS THAT ALTER ATTRI ...
- 通过应用程序域AppDomain加载和卸载程序集
微软装配车的大门似乎只为货物装载敞开大门,却将卸载工人拒之门外.车门的钥匙只有一把,若要获得还需要你费一些心思.我在学习Remoting的时候,就遇到一个扰人的问题,就是Remoting为远程对象仅提 ...
- 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数。定义 Gymnastics 类和 School 类,它们都是 ComputerAverage 的子类。Gymnastics 类中计算选手的平均成绩的方法是去掉一个最低分,去掉一个最高分,然后求平均分;School 中计算平均分的方法是所有科目的分数之和除以总科目数。 要求:定义ComputerAv
题目: 编写一个ComputerAverage抽象类,类中有一个抽象方法求平均分average,可以有参数. 定义 Gymnastics 类和 School 类,它们都是 ComputerAverag ...
- 980. Unique Paths III
题目来源: https://leetcode.com/problems/unique-paths-iii/ 自我感觉难度/真实难度: 题意: 分析: 回溯法,直接DFS就可以了 自己的代码: clas ...
- 构建 CDN 分发网络架构
cdn基本架构: CDN的基本目的:1.通过本地缓存实现网站的访问速度的提升 CDN的关键点:CNAME在域名解析:split智能分发,引流到最近缓存节点
- CMD centos7 安装 最新版本的docker -- dockerfire 原语 ENTRYPOINT - 导入镜像 tar mariadb Dockerfile 构建镜像
yum update # vim /etc/yum.repos.d/docker.repo //添加以下内容 [dockerrepo] name=Docker Repository baseurl=h ...
- 五种典型开发周期模型(瀑布、V、原型化、螺旋、迭代)
五种典型开发周期模型(瀑布.V.原型化.螺旋.迭代) 总结一下经常可以见到的系统开发周期模型. 在过去的几年里,可以很奇葩的碰到类似于“创业项目库”这种需求非常明确,工作量十分可控,对质量要求比 ...
- 《驱蚊神器v1.0》android应用 赶走那些烦人的臭蚊子
<驱蚊神器v1.0>能够非常好地赶走那些个烦人又恼人伤人的臭蚊子,它总是搞得自己没有好的睡眠或歇息,得努力地拍巴巴掌,这下可好了,也少些烦恼了,先深情地眯缝一会儿...此声波怡人不会对人产 ...