根据源程序进行改写:
原程序地址:http://www.cnblogs.com/lianzhilei/p/5985333.html 如有侵权立即删除。
感谢原作者将完整的代码提供参考。
原作者的代码是通过 shelve 模块实现的,非常完美。具体参考上面的链接地址;以下我改写代码是通过 pickle 模块来实现。主要做为练习:
README
class_system-pickle 选课系统根目录
├── bin
│ ├── __init__.py
│ └── start.py 程序入口
├── conf
│ ├── __init__.py
│ └── settings.py 配置文件 - 主要存储数据库文件目录
├── core
│ ├── __init__.py
│ ├── manage_center.py 主菜单入口
│ ├── school_center.py 学校中心功能点实现
│ ├── student_center.py 学生中心功能点实现
│ └── teacher_center.py 讲师中心功能点实现
├── db
│ ├── __init__.py
│ └── school_dict 数据库文件(运行程序自动初始化生成)
└── modules
├── course.py 课程类
├── grade.py 班级类
├── __init__.py
├── school.py 学校类
├── student.py 学生类
├── teacher.py 讲师类
└── tools.py 工具包 - 对数据库文件读写操作 5 directories, 18 files 代码实现逻辑请阅读原作者链接。
基于 pickle 序列化的代码如下:
bin
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
from conf.settings import school_db_file
from core.manage_center import Manage_center if __name__ == '__main__':
obj = Manage_center()
obj.run()
start.py
conf
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) school_db_file = os.path.join(BASE_DIR, 'db', 'school_dict')
teacher_db_file = os.path.join(BASE_DIR, 'db', 'school_dict')
settings.py
db
(不需要手动创建文件,启动程序自动生成数据文件)
core
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
from core.school_center import School_center
from core.student_center import Student_center
from core.teacher_center import Teacher_center class Manage_center(object):
def __init__(self):
pass def run(self):
while True:
print('1. 学校中心\n'
'2. 讲师中心\n'
'3. 学生中心\n'
'4. 退出')
choice = input('>>>').strip()
if choice == '':
School_center()
elif choice == '':
Teacher_center()
elif choice == '':
Student_center()
elif choice == '':
break
else:
print('\033[31;1m输入错误,请重新输入.\033[0m')
manage_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys, pickle
from conf.settings import school_db_file
from modules.tools import file_oper
from modules.school import School class School_center(object):
def __init__(self):
if not os.path.exists(school_db_file):
self.init_school()
self.school_db = file_oper(school_db_file, 'rb')
self.manage_run()
else:
self.school_db = file_oper(school_db_file, 'rb')
self.manage_run() def init_school(self):
sh = School('上海', '上海市')
bj = School('北京', '北京市')
data = {'上海': sh, '北京': bj}
file_oper(school_db_file, 'wb', data) def manage_run(self):
for key in self.school_db:
print('学校名:', key) choice_school = input('>>>').strip()
if choice_school in self.school_db:
self.choice_school = choice_school
self.school_obj = self.school_db[choice_school]
while True:
print('\n欢迎进入【%s】校区\n'
'创建课程 add_course\n'
'查看课程 check_course\n'
'创建班级 add_grade\n'
'查看班级 check_grade\n'
'创建讲师 add_teacher\n'
'查看讲师 check_teacher\n'
'退出系统 exit' % choice_school) user_func = input('>>>').strip()
if hasattr(self, user_func):
getattr(self, user_func)() def add_course(self):
course_name = input('\033[34;1m输入课程名:\033[0m')
course_price = input('\033[34;1m输入课程价格:\033[0m')
course_time = input('\033[34;1m输入课程周期:\033[0m')
if course_name not in self.school_obj.school_course:
self.school_obj.create_course(course_name, course_price, course_time)
self.school_db[self.choice_school] = self.school_obj
file_oper(school_db_file, 'wb', self.school_db)
print('\033[32;1m课程新增成功。\033[0m')
else:
print('\033[31;1m课程信息已存在.\033[0m')
self.school_obj.create_course(course_name, course_price, course_time)
print('\033[32;1m课程更新成功。\033[0m') def check_course(self):
self.school_obj.show_course() def add_grade(self):
grade_name = input('\033[34;1m输入班级名:\033[0m')
course_name = input('\033[34;1m输入班级要上的课程名:\033[0m')
if course_name in self.school_obj.school_course:
course_obj = self.school_obj.school_course[course_name]
if grade_name not in self.school_obj.school_grade:
self.school_obj.create_grade(grade_name, course_obj)
self.school_db[self.choice_school] = self.school_obj
file_oper(school_db_file, 'wb', self.school_db) else:
print('\033[31;1m班级信息已存在.\033[0m')
else:
print('\033[31;1m课程信息不存在.\033[0m') def check_grade(self):
self.school_obj.show_grade() def add_teacher(self):
teacher_name = input('\033[34;1m输入讲师名:\033[0m')
teacher_salary = input('\033[34;1m输入讲师薪资:\033[0m')
grade_name = input('\033[34;1m输入讲师关联的班级:\033[0m')
if grade_name in self.school_obj.school_grade:
grade_obj = self.school_obj.school_grade[grade_name]
if teacher_name not in self.school_obj.school_teacher:
self.school_obj.create_teacher(teacher_name, teacher_salary, grade_name, grade_obj)
print('\033[32;1m讲师新增成功.\033[0m')
else:
print('\033[31;1m讲师信息已存在.\033[0m')
self.school_obj.create_teacher(teacher_name, teacher_salary, grade_name, grade_obj)
print('\033[32;1m讲师更新成功.\033[0m')
self.school_db[self.choice_school] = self.school_obj
file_oper(school_db_file, 'wb', self.school_db)
else:
print('\033[31;1m班级信息不存在,请先创建班级.\033[0m') def check_teacher(self):
self.school_obj.show_teacher() def exit(self, *args):
sys.exit('欢迎下次使用选课系统.')
school_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys, pickle
from conf.settings import school_db_file
from modules.tools import file_oper
from modules.school import School class Student_center(object):
def __init__(self):
if os.path.exists(school_db_file):
self.school_db = file_oper(school_db_file, 'rb')
self.manage_run()
else:
print('\033[31;1m数据库文件不存在,请先初始化.\033[0m') def manage_run(self):
for key in self.school_db:
print('学校名:', key)
choice_school = input('>>>').strip()
if choice_school in self.school_db:
self.choice_school = choice_school
self.school_obj = self.school_db[choice_school]
student_name = input('\033[34;1m输入学生名:\033[0m').strip()
student_age = input('\033[34;1m输入学生年龄:\033[0m').strip()
self.school_obj.show_grade_course()
grade_name = input('\033[34;1m输入要选择的班级名:\033[0m')
if grade_name in self.school_obj.school_grade:
self.school_obj.create_student(student_name, student_age, grade_name)
self.school_db[self.choice_school] = self.school_obj
file_oper(school_db_file, 'wb', self.school_db)
print('\033[32;1m选课成功.\033[0m') else:
print('\033[31;1m班级信息不存在.\033[0m')
student_center.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import os, sys
from modules.tools import file_oper
from conf.settings import school_db_file, teacher_db_file class Teacher_center(object):
def __init__(self):
if os.path.exists(school_db_file):
self.school_db = file_oper(school_db_file, 'rb')
self.manage_run()
else:
print('\033[31;1m数据库不存在,请初始化数据库.')
exit(1)
# if not os.path.exists(teacher_db_file):
# self.init_teacher()
# self.manage_run()
# else:
# self.teacher_db = file_oper(teacher_db_file, 'rb')
# self.manage_run() def init_teacher(self):
data = {}
file_oper(teacher_db_file, 'wb', data) def manage_run(self):
for key in self.school_db:
print('学校名:', key)
choice_school = input('\033[34;1m输入学校名:\033[0m')
if choice_school in self.school_db:
self.choice_school = choice_school
self.school_obj = self.school_db[choice_school]
teacher_name = input('\033[34;1m输入讲师名:\033[0m')
if teacher_name in self.school_obj.school_teacher:
while True:
print('\n欢迎进入讲师中心\n'
'查看班级信息 check_grade\n'
'退出 exit') user_func = input('>>>').strip()
if hasattr(self, user_func):
getattr(self, user_func)(teacher_name) def check_grade(self, teacher_name):
self.school_obj.show_teacher_grade(teacher_name) def exit(self, *args):
sys.exit('欢迎下次使用选课系统.')
teacher_center.py
modules
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey class Course(object):
def __init__(self, course_name, course_price, course_time):
self.course_name = course_name
self.course_price = course_price
self.course_time = course_time
course.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey class Grade(object):
def __init__(self, grade_name, course_obj):
self.grade_name = grade_name
self.course_obj = course_obj
self.grade_student = {}
grade.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import pickle
from modules.course import Course
from modules.grade import Grade
from modules.teacher import Teacher
from modules.student import Student class School(object):
def __init__(self, school_name, school_addr):
self.school_name = school_name
self.school_addr = school_addr
self.school_course = {}
self.school_grade = {}
self.school_teacher = {} def create_course(self, course_name, course_price, course_time):
course_obj = Course(course_name, course_price, course_time)
self.school_course[course_name] = course_obj def show_course(self):
for key in self.school_course:
course_obj = self.school_course[key]
print('\033[32;1m课程【%s】\t价格【%s元】\t周期【%s个月】\033[0m' % (
course_obj.course_name, course_obj.course_price, course_obj.course_time)) def create_grade(self, grade_name, course_obj):
grade_obj = Grade(grade_name, course_obj)
self.school_grade[grade_name] = grade_obj def show_grade(self):
for key in self.school_grade:
grade_obj = self.school_grade[key]
print('\033[32;1m班级名【%s】\t课程【%s】\033[0m' % (grade_obj.grade_name, grade_obj.course_obj.course_name)) def create_teacher(self, teacher_name, teacher_salary, grade_name, grade_obj):
teacher_obj = Teacher(teacher_name, teacher_salary)
teacher_obj.teacher_add_grade(grade_name, grade_obj)
self.school_teacher[teacher_name] = teacher_obj def show_teacher(self):
for key in self.school_teacher:
teacher_obj = self.school_teacher[key]
grade_list = []
for key in teacher_obj.teacher_grade:
grade_list.append(key)
print('\033[32;1m讲师【%s】\t薪资【%s】\t班级列表【%s】\033[0m' % (
teacher_obj.teacher_name, teacher_obj.teacher_salary, grade_list)) def show_grade_course(self):
for key in self.school_grade:
grade_obj = self.school_grade[key]
course_obj = grade_obj.course_obj
print('\033[32;1m班级【%s】\t课程【%s】\t价格【%s元】\t周期【%s个月】\033[0m' % (
grade_obj.grade_name, course_obj.course_name, course_obj.course_price, course_obj.course_time)) def create_student(self, student_name, student_age, grade_name):
student_obj = Student(student_name, student_age)
grade_obj = self.school_grade[grade_name]
grade_obj.grade_student[student_name] = student_obj def show_teacher_grade(self, teacher_name):
teacher_obj = self.school_teacher[teacher_name]
for key in teacher_obj.teacher_grade:
grade_obj = teacher_obj.teacher_grade[key]
student_list = []
for st in grade_obj.grade_student:
student_list.append(st)
print('\033[32;1m班级【%s】课程【%s】学生列表【%s】\033[0m' % (
grade_obj.grade_name, grade_obj.course_obj.course_name, student_list))
school.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey class Student(object):
def __init__(self, student_name, student_age):
self.student_name = student_name
self.student_age = student_age
student.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey class Teacher(object):
def __init__(self, teacher_name, teacher_salary):
self.teacher_name = teacher_name
self.teacher_salary = teacher_salary
self.teacher_grade = {} def teacher_add_grade(self, grade_name, grade_obj):
self.teacher_grade[grade_name] = grade_obj
teacher.py
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: hkey
import pickle def file_oper(file, mode, *args):
if mode == 'wb':
data = args[0]
with open(file, mode) as f:
pickle.dump(data, f)
elif mode == 'rb':
with open(file, mode) as f:
data = pickle.load(f)
return data
tools.py