• 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 + "rcourse"
    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,": ",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(' ')
    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


    阐述:
    以上的就是pickle的基本使用,对文件目录文件读取写入等,在继承方面,指继承了基类的方法,注册什么的都是没有什么效果,最主要是类的继承比较繁琐,还是可以学习到一点东西。
  • 相关阅读:
    Python操作Mongo数据库
    Python正则模块
    Python时间模块
    Python协程(下)
    Python协程(中)
    Python协程(上)
    aiohttp
    常用排序算法的Python实现
    江苏省各地级市58同城租房数据
    百合网
  • 原文地址:https://www.cnblogs.com/qianyuyu/p/10081751.html
Copyright © 2020-2023  润新知