python 选课系统

couser.py:

import sys,os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from core import main

if __name__ == '__main__': #__main__就是导入的模块,main,__name__是系统变量
a = main.Run() #实例化类
a.interactive() #调用类的方法

uid.py:
import hashlib
import time

def create_md():
m = hashlib.md5()
m.update(bytes(str(time.time()),encoding='utf-8')) #hash的数值
return m.hexdigest() #16进制的hash值


main.py:

import sys,os
import json
import pickle

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core import uid
#数据库文件路径
db_DIR = BASE_DIR + r"\db"
db_school = db_DIR + r"\school"
db_teacher = db_DIR + r"\student"
db_classes = db_DIR + r"\classes"
db_course = db_DIR + "r\course"
db_admin = db_DIR + r"\admin"
db_class_record = db_DIR + r"\class_record"
db_class_grade = db_DIR + r"\class_grade"

class Baseclass(object):
def __init__(self):
pass
def save(self,type,dict):
"""
school是一个字典格式,这个函数是对创建的学校进行存档
:param type:type输入的其实是db中的文件夹
:param dict:
:return:
"""
filename = uid.create_md()
dict['uid'] = filename #
file_path = "%s\%s"%(db_DIR,type) #type就是文件夹的名称
ab_file = "%s\%s"%(file_path,filename) #文件夹下的文件名称,filename是hash的id值
with open(ab_file,"wb") as f:
# dumps 将数据通过特殊的形式转换为只有python语言认识的字符串
f.write(pickle.dumps(dict))
print("-------",type,"创建成功","-------")
for key in dict:
print(key,":\t",dict[key])


def seek_list(self,type,list):
"""
此方法是将班级中的学生,学校的老师等列表进行存储
:param type: 学生,学校,班级中学生登记,班级等类别
:param list: 每种课程中的学生信息
:return:
"""
filename = uid.create_md()
file_path = "%s\%s" %(db_DIR,type)
ab_file = "%s\%s" %(file_path,filename)
with open(ab_file,"wb") as f: #二进制读入,二进制打开,最好的方式
f.write(pickle.dumps(list))
print("-------",type,"创建成功","--------")
for i in list: #i就是相关学生,老师的选课信息的字典
for key in i:
print(key,i[key])
print('\n')
return True

def open(self,type):
"""
此方法是将相关数据中的进行还原
:param type:数据类型
:return: 返回一个列表
"""
all_data = []
db_path = "%s\%s" %(db_DIR,type) #相对应的文件夹
for i in os.listdir(db_path): #直接就将文件显示出来了
if os.path.isfile(os.path.join(db_path,i)):
db_file = os.path.join(db_path,i) #变成绝对路径,os.path.join直接就把路径\合并了
with open(db_file,"rb") as f: #二进制打开
file_dict = pickle.load(f) #还原pickle的数据
all_data.append(file_dict) #可以把字典直接还原,返回的是列表

return all_data

class Admin(Baseclass):
def __init__(self):
super(Admin,self).__init__()
def create_school(self):
school_dict = {}
school_name = input("校名:")
school_address = input("地址:")
s1 = School(school_name,school_address)
school_dict["校名"] = s1.school_name
school_dict["地址"] = s1.school_address
Baseclass.save(self,"school",school_dict)
def create_teacher(self):
teacher_dict = {}
teacher_name = input("讲师姓名:")
teacher_salary = input("讲师工资:")
teacher_school = input("所属学校:")
t1 = Teacher(teacher_name,teacher_salary,teacher_school) #这些有点多余,其它也没有关联起来
teacher_dict["姓名"] = t1.teacher_name
teacher_dict["工资"] = t1.teacher_salary
teacher_dict["所属学校"] = t1.teacher_school
print(teacher_dict) #进行测试
Baseclass.save(self,"teacher",teacher_dict)
def create_student(self):
student_dict = {}
student_name = input("学员姓名:")
student_sex = input("学员性别:")
student_school = input("所属学校:")
student_classes = input("学员班级:")
# st1 = Student(student_name,student_sex,student_school,student_classes)
student_dict["姓名"] = student_name
student_dict["性别"] = student_sex
student_dict["学校"] = student_school
student_dict["班级"] = student_classes
Baseclass.save(self,"student",student_dict)
def create_course(self):
course_dict = {}
course_name = input("课程名:")
course_period = input("周期:")
course_prices = input("价格:")
c1 = Course(course_name,course_period,course_prices) #实例化Course类
course_dict["课程名"] = c1.course_name #这里用course_name也可以 course_dict["周期"] = c1.course_period course_dict["价格"] = c1.course_prices Baseclass.save(self,"course",course_dict) def create_classes(self): classes_dict = {} classes_name = input("班级名:") classes_teacher = input("负责讲师:") classes_course = input("所学课程:") cs1 = Classes(classes_name,classes_teacher,classes_course) classes_dict["班级名"] = cs1.classes_name classes_dict["负责讲师"] = cs1.classes_teacher classes_dict["课程"] = cs1.classes_course Baseclass.save(self,"classes",classes_dict)class School(Baseclass): def __init__(self,school_name,school_address): super(School,self).__init__() self.school_name = school_name self.school_address = school_addressclass Teacher(Baseclass): def __init__(self,teacher_name,teacher_salary,teacher_school): super(Teacher,self).__init__() self.teacher_name = teacher_name self.teacher_salary = teacher_salary self.teacher_school = teacher_school def create_class_record(self): """ 这个班级记录的方法方法应该是然学生选择哪个学校,班级,学生是否旷课 :return: """ class_record = [] student_school = input("选择学校:") student_classes = input("选择班级:") student_times = input("课次:") student_list = Baseclass.open(self,"student") #学生的列表,但是哪个课程的学生呢 for i in student_list: if i["学校"] == student_school and i["班级"] == student_classes: student_name = i["姓名"] try: student_status = input("%s 上课情况:" %student_name) except Exception as e: print("yes or no") else: i["上课情况"] = student_status #如果输入错误怎么办,需要输入什么 i["课次"] = student_times class_record.append(i) Baseclass.seek_list(self, "class_record", class_record) # 将课程记录进行存档 else: print("此学生不存在") def create_class_grade(self): """ 这是学生的成绩 :return: """ class_grade = [] student_school = input("选择学校:") student_classes = input("选择班级:") student_times = input("课次:") student_list = Baseclass.open(self,"student") #打开的是一个类表 for i in student_list: if i["学校"] == student_school and i["班级"] == student_classes: student_name = i["姓名"] student_grade = input("%s 成绩:"% student_name) i["成绩"] = student_grade i["课次"] = student_times class_grade.append(i) Baseclass.seek_list(self,"class_grade",class_grade) def teacher_view_grade(self): grade_list = [] student_school = input("校名:") student_class = input("班级:") student_times = input("课次:") class_grade_list = Baseclass.open(self,"class_grade") for i in class_grade_list: for j in i: if j["学校"] == student_school and j["班级"]==student_class \ and j["课次"] == student_times: grade_list.append(j) for i in grade_list: for key in i: print(key,i[key]) print("\n") def teacher_view_record(self): record_list = [] student_school = input("校名:") student_class = input("班级:") student_times = input("课次:") class_record_list = Baseclass.open(self,"class_record") for i in class_record_list: for j in i: if j["学校"] == student_school and j["班级"] == student_class and j["课次"] ==student_times: record_list.append(j) for i in record_list: for key in i: print(key,i[key]) print("\n")class Student(Baseclass): def __init__(self,student_name,student_sex,student_school,student_classes): super(Student, self).__init__() self.student_name = student_name self.student_sex = student_sex self.student_school = student_school self.student_classes = student_classes def student_registered(self): student_dict = {} print("欢迎进入学生注册系统") student_name = input("注册姓名:") student_sex = input("性别:") student_school = input("学校:") student_class = input("班级:") st1 = Student(student_name,student_sex,student_school,student_class) student_dict["姓名"] = st1.student_name student_dict["性别"] = st1.student_sex student_dict["学校"] = st1.student_school student_dict["班级"] = st1.student_classes Baseclass.save(self, "student", student_dict) def student_pay_fees(self): pass def student_view_grade(self): student_school = input("校名:") student_class = input("班级:") student_times = input("课次:") student_name = input("姓名:") class_grade_list = Baseclass.open(self, "class_grade") for i in class_grade_list: for j in i: if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \ and j["姓名"] == student_name: for key in j: print(key, j[key]) print("\n") def student_view_record(self): student_school = input("校名:") student_class = input("班级:") student_times = input("课次:") student_name = input("姓名:") class_record_list = Baseclass.open(self, "class_record") for i in class_record_list: for j in i: if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \ and j["姓名"] == student_name: for key in j: print(key, j[key]) print("\n")class Course(Baseclass): def __init__(self,course_name,course_period,course_prices): super(Course,self).__init__() self.course_name = course_name self.course_period = course_period self.course_prices = course_pricesclass Classes(Baseclass): def __init__(self,classes_name,classes_teacher,classes_course): super(Classes,self).__init__() self.classes_name = classes_name self.classes_teacher = classes_teacher self.classes_course = classes_courseclass Admin_view(Admin): def __init__(self): Admin.__init__(self) def auth(self, username, password): admin_file = "%s/%s.json" % (db_admin, username) if os.path.isfile(admin_file): with open(admin_file, 'r') as f: admin_data = json.load(f) if admin_data["password"] == password: return True else: print("密码错误") else: print("用户名错误") def login(self): menu = u""" ------- 欢迎进入管理视图 --------- \033[32;1m 1. 校区管理 2. 讲师管理 3. 学员管理 4. 课程管理 5. 返回 \033[0m""" menu_dic ={ '1': Admin_view.school_manager, '2': Admin_view.teacher_manager, '3': Admin_view.student_manager, '4': Admin_view.course_manager, '5': "logout" } username = input("输入用户名:").strip() password = input("输入密码:").strip() auth = Admin_view.auth(self, username, password) if auth: exit_flag = False while not exit_flag: print(menu) option = input("请选择:") if option in menu_dic: if int(option) == 5: exit_flag = True else: menu_dic[option](self) else: print("\033[31;1m输入错误,重新输入\033[0m") def school_manager(self): exit_flag = False while not exit_flag: print(""" ------- 欢迎进入校区管理 --------- \033[32;1m1. 创建校区 2. 创建班级 3. 返回 \033[0m """) option = input("请选择:").strip() if int(option) == 1: Admin.create_school(self) elif int(option) == 2: Admin.create_classes(self) else: exit_flag = True def teacher_manager(self): exit_flag = False while not exit_flag: print(""" ------- 欢迎进入讲师管理 --------- \033[32;1m 1. 创建讲师 2. 返回 \033[0m """) option = input("请选择:").strip() if int(option) == 1: Admin.create_teacher(self) else: exit_flag = True def student_manager(self): exit_flag = False while not exit_flag: print(""" ------- 欢迎进入学员管理 --------- \033[32;1m 1. 创建学员 2.返回 \033[0m """) option = input("请选择:").strip() if int(option) == 1: Admin.create_student(self) else: exit_flag = True def course_manager(self): exit_flag = False while not exit_flag: print(""" ------- 欢迎进入课程管理 --------- \033[32;1m 1. 创建课程 2.返回 \033[0m """) option = input("请选择:").strip() if int(option) == 1: Admin.create_course(self) else: exit_flag = Trueclass Teacher_view(Teacher): def __init__(self, teacher_name, teacher_salary, teacher_school): super(Teacher_view,self).__init__(teacher_name,teacher_salary,teacher_school) def login(self): menu = u""" ------- 欢迎进入讲师视图 --------- \033[32;1m 1. 创建上课记录 2. 创建学员成绩 3. 查看学员上课记录 4. 查看学员成绩 5. 返回 \033[0m """ menu_dic = { '1': Teacher.create_class_record, '2': Teacher.create_class_grade, '3': Teacher.teacher_view_grade, '4': Teacher.teacher_view_record, '5': "logout", } if True: exit_flag = False while not exit_flag: print(menu) option = input("请选择:").strip() if option in menu_dic: if int(option) == 5: exit_flag = True else: menu_dic[option](self) else: print("\033[31;1m输入错误,重新输入\033[0m")class Student_view(Student): def __init__(self,student_name,student_sex,student_school,student_classes): super(Student_view,self).__init__(student_name,student_sex,student_school,student_classes) def login(self): menu = u''' ------- 欢迎进入学生管理视图 --------- \033[32;1m 1. 注册 2. 交学费 3. 查看上课记录 4. 查看作业成绩 5. 返回 \033[0m''' menu_dic = { '1': Student.student_registered, '2': Student.student_pay_fees, '3': Student.student_view_record, '4': Student.student_view_grade, '5': "logout", } if True: exit_flag = False while not exit_flag: print(menu) option = input("请输入").strip() if option in menu_dic: if int(option) == 5: exit_flag = True else: menu_dic[option](self) else: print("\033[31;1m输入错误,重新输入\033[0m")class Run(object): def __init__(self): pass def interactive(self): menu = u''' ------- 欢迎进入选课系统 --------- \033[32;1m 1. 学生视图 2. 讲师视图 3. 管理视图 4. 退出 \033[0m''' menu_dic = { '1': Student_view, '2': Teacher_view, '3': Admin_view, '4':"log_out" } exit_flag = False while not exit_flag: print(menu) option_view = input("请选择视图:").strip() if option_view in menu_dic: if int(option_view) == 4: exit_flag = True else: menu_dic[option_view].login(self) else: print("\033[31;1m输入错误,重新输入\033[0m")

阐述:
以上的就是pickle的基本使用,对文件目录文件读取写入等,在继承方面,指继承了基类的方法,注册什么的都是没有什么效果,最主要是类的继承比较繁琐,还是可以学习到一点东西。

猜你喜欢

转载自www.cnblogs.com/qianyuyu/p/10081751.html