• 面象对象编程(选课系统)


    选课系统

    conf包下
    /settings.py/
    
    import os
    
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))
    
    DB_PATH = os.path.join(BASE_PATH, 'db')
    
    
    core包下
    
    /src.py/
    
    from core import admin
    from core import student
    from core import teacher
    
    # 主视图字典
    main_dict = {
        '1': admin.admin_view,
        '2': student.student_view,
        '3': teacher.teacher_view,
    }
    
    
    # 执行函数
    def run():
        while True:
            print('''
            =========主视图===========
            1.管理员视图
            2.学生视图       
            3.老师视图
            q1:退出程序,程序终止
            请选择您要选择的视图前的编号!
            ''')
            choice = input('请输入编号:').strip()
            if choice == 'q1':
                break
            if choice not in main_dict:
                print('请输入正确编号!')
    
            # 调用函数方法
            main_dict[choice]()
            
    /admin.py/
    
    from interface import admin_interface
    from interface import common_interface
    from lib import common
    
    admin_info = {
        'user': None
    }
    
    
    # 注册功能
    def register():
        while True:
            username = input('请输入用户名:').strip()
            if username == 'q':
                break
            password = input('请输入密码:').strip()
            if password == 'q':
                break
            re_password = input('请确认密码:').strip()
            if re_password == 'q':
                break
            if password == re_password:
                flag, msg = admin_interface.register_interface(username, password)
                if flag:
                    print(msg)
                    break
                elif flag == 0:
                    print(msg)
                else:
                    print(msg)
            else:
                print('两次密码输入不一致!')
    
    
    # 登录功能
    def login():
        while True:
            username = input('请输入用户名:').strip()
            if username == 'q':
                break
            password = input('请输入密码:').strip()
            if username == 'q':
                break
            flag, msg = common_interface.common_login_interface(username, password, 'admin')
            if flag:
                print(msg)
                admin_info['user'] = username
                break
            else:
                print(msg)
    
    
    # 创建学校
    @common.login_auth('admin')
    def create_school():
        while True:
            school_name = input('请输入学校名称:').strip()
            if school_name == 'q':
                break
            school_addr = input('请输入学校地址:').strip()
            if school_addr == 'q':
                break
            flag, msg = admin_interface.create_school_interface(admin_info['user'], school_name, school_addr)
            if flag:
                print(msg)
                break
            elif flag == 0:
                print(msg)
            else:
                print(msg)
    
    
    # 创建老师
    @common.login_auth('admin')
    def create_teacher():
        while True:
            teacher_name = input('请输入老师的名字:').strip()
            if teacher_name == 'q':
                break
            flag, msg = admin_interface.create_teacher_interface(admin_info['user'], teacher_name, '123')
            if flag:
                print(msg)
                break
            elif flag == 0:
                print(msg)
            else:
                print(msg)
    
    
    # 创建课程
    @common.login_auth('admin')
    def create_course():
        global school_name
        while True:
            school_list = common_interface.check_all_school_interface()
            if not school_list:
                print('学校为空,无法创建!')
            else:
                for num, school_name in enumerate(school_list):
                    print(f'''
                        编号:{num}
                        学校:{school_name}
                        ''')
                choice = input('请输入编号:').strip()
                if choice == 'q':
                    break
                if choice.isdigit():
                    choice1 = int(choice)
                    if 0 <= choice1 < len(school_list):
                        school_name = school_list[choice1]
                    else:
                        print('请输入有效编号')
                        continue
    
                else:
                    print('请输入数字')
                    continue
                course_name = input('请输入课程名:').strip()
                if course_name == 'q':
                    break
                flag, msg = admin_interface.create_course_interface(admin_info['user'], school_name, course_name)
                if flag:
                    print(msg)
                    break
                elif flag == 0:
                    print(msg)
                else:
                    print(msg)
    
    
    admin_dict = {
        '1': register,
        '2': login,
        '3': create_school,
        '4': create_teacher,
        '5': create_course,
    }
    
    
    # 管理员视图
    def admin_view():
        while True:
            print('''
            ===========管理员视图============
             1、注册
             2、登录
             3、创建学校
             4、创建老师
             5、创建课程
             q:任意输入界面返回至学生视图
             qq:退回到主视图
            ''')
            choice = input('请输入选择:').strip()
            if choice == 'qq':
                break
            if choice not in admin_dict:
                print('输入不合法!')
                continue
            admin_dict[choice]()
    
            
    /student.py/
    
    from interface import common_interface
    from interface import student_interface
    from lib import common
    
    student_info = {
        'user': None
    }
    
    
    # 学生注册功能
    def register():
        while True:
            username = input('请输入用户名:').strip()
            if username == 'q':
                break
            password = input('请输入密码:').strip()
            if password == 'q':
                break
            re_password = input('请确认密码:').strip()
            if re_password == 'q':
                break
            if password == re_password:
                flag, msg = student_interface.register_interface(username, password)
                if flag:
                    print(msg)
                    break
                elif flag == 0:
                    print(msg)
                else:
                    print(msg)
            else:
                print('两次密码输入不一致!')
    
    
    # 学生登录功能
    def login():
        while True:
            username = input('请输入用户名:').strip()
            if username == 'q':
                break
            password = input('请输入密码:').strip()
            if password == 'q':
                break
            flag, msg = common_interface.common_login_interface(username, password, 'student')
            if flag:
                print(msg)
                student_info['user'] = username
                break
            else:
                print(msg)
    
    
    # 学生选择校区
    @common.login_auth('student')
    def choose_school():
        global school_name
        while True:
            school_list = common_interface.check_all_school_interface()
            if not school_list:
                print('学校为空,无法创建!')
            else:
                for num, school_name in enumerate(school_list):
                    print(f'''
                    编号:{num}
                    学校:{school_name}
                    ''')
            choice = input('请输入编号:').strip()
            if choice == 'q':
                break
            if choice.isdigit():
                choice1 = int(choice)
                if 0 <= choice1 < len(school_list):
                    school_name = school_list[choice1]
                else:
                    print('请输入有效编号')
                    continue
    
            else:
                print('请输入数字')
                continue
            flag, msg = student_interface.choose_school_interface(student_info['user'], school_name)
            if flag:
                print(msg)
                break
            else:
                print(msg)
                break
    
    # 学生选择课程
    @common.login_auth('student')
    def choose_course():
        while True:
            # 1.获取当前对象的 学校中 所有的课程
            flag, msg = student_interface.get_course_list_interface(
                student_info.get('user'))
            if not flag:
                print(msg)
                break
    
            # 2.打印课程,选择课程名称
            for index, course_name in enumerate(msg):
                print(index, course_name)
    
            choice = input('请输入选择的课程编号: ').strip()
    
            if choice == 'q':
                break
    
            if not choice.isdigit():
                continue
            choice = int(choice)
    
            if choice not in range(len(msg)):
                continue
            course_name = msg[choice]
    
            # 3.调用学生选择课程接口
            flag, msg = student_interface.choose_course_interface(
                student_info.get('user'), course_name
            )
    
            if flag:
                print(msg)
                break
    
            else:
                print(msg)
    
    
    # 学生查看成绩
    @common.login_auth('student')
    def check_score():
        score_dict = student_interface.check_score_interface(student_info['user'])
        print(score_dict)
    
    
    # 学生功能字典
    student_dict = {
        '1': register,
        '2': login,
        '3': choose_school,
        '4': choose_course,
        '5': check_score,
    }
    
    
    # 学生视图
    def student_view():
        while True:
            print('''
            ===========学生视图============
             1、注册
             2、登录
             3、选择校区
             4、选择课程
             5、查看成绩
             q:任意输入界面返回至学生视图
             qq:退回到主视图
            ''')
            choice = input('请输入选择:').strip()
            if choice == 'qq':
                break
            if choice not in student_dict:
                print('输入不合法!')
                continue
            student_dict[choice]()
    
            
    /teacher.py/
    
    from interface import common_interface
    from lib import common
    from interface import teacher_interface
    
    teacher_info = {
        'user': None
    }
    
    
    # 登录功能
    def login():
        while True:
            username = input('请输入用户名:').strip()
            if username == 'q':
                break
            password = input('请输入密码:').strip()
            if password == 'q':
                break
            flag, msg = common_interface.common_login_interface(username, password, 'teacher')
            if flag:
                print(msg)
                teacher_info['user'] = username
                break
            else:
                print(msg)
    
    
    # 查看教授的课程
    @common.login_auth('teacher')
    def check_teach_course():
        while True:
            flag, course_list_or_msg = teacher_interface.check_teach_course_interface(
                teacher_info['user']
            )
    
            if flag:
                print(course_list_or_msg)
                break
            else:
                print(course_list_or_msg)
                break
    
    
    # 选择教授的课程
    @common.login_auth('teacher')
    def choose_teach_course():
        while True:
            # 1.先获取所有学校,选择学校
            school_list = common_interface.check_all_school_interface()
            if not school_list:
                print('没有学校可以选择!')
                break
    
            for index, school_name in enumerate(school_list):
                print(index, school_name)
    
            choice = input('请输入学校编号: ').strip()
            if choice == 'q':
                break
            if not choice.isdigit():
                continue
    
            choice = int(choice)
    
            if choice not in range(len(school_list)):
                continue
    
            school_name = school_list[choice]
    
            # 2.通过学校获取所有课程,再选择教授课程
            flag, course_list_or_msg = common_interface.check_course_from_school_interface(school_name)
    
            # 3.打印所有的课程,让老师选择课程编号
            if not flag:
                print(course_list_or_msg)
                break
    
            for index, course_name in enumerate(course_list_or_msg):
                print(index, course_name)
    
            choice = input('请输入课程编号: ').strip()
            if choice == 'q':
                break
            if not choice.isdigit():
                continue
    
            choice = int(choice)
    
            if choice not in range(len(course_list_or_msg)):
                continue
    
            course_name = course_list_or_msg[choice]
    
            # 4.调用选择教授课程接口
            flag, msg = teacher_interface.choose_teach_course_interface(
                 teacher_info.get('user'), course_name)
    
            if flag:
                print(msg)
                break
            else:
                print(msg)
    
    
    # 查看课程下的学生
    @common.login_auth('teacher')
    def check_course_student():
        while True:
            # 1.获取老师中所有的课程,并选择
            flag, course_list_or_msg = teacher_interface.check_teach_course_interface(
                teacher_info.get('user'))
    
            if not flag:
                print(course_list_or_msg)
                break
    
            # 2.通过课程查看课程中所有的学生
            for index, course_name in enumerate(course_list_or_msg):
                print(index, course_name)
    
            choice = input('请输入课程编号: ').strip()
            if choice == 'q':
                break
            if not choice.isdigit():
                continue
    
            choice = int(choice)
    
            if choice not in range(len(course_list_or_msg)):
                continue
    
            course_name = course_list_or_msg[choice]
    
            # 3.调用查看课程中所有学生接口
            flag, student_list_or_msg = teacher_interface.check_course_student_interface(
                teacher_info['user'], course_name
            )
    
            if flag:
                print(student_list_or_msg)
                break
    
            else:
                print(student_list_or_msg)
    
    
    # 修改学生成绩
    @common.login_auth('teacher')
    def change_student_score():
        while True:
            # 1.获取老师中所有的课程,并选择
            flag, course_list_or_msg = teacher_interface.check_teach_course_interface(
                teacher_info.get('user'))
    
            if not flag:
                print(course_list_or_msg)
                break
    
            # 2.通过课程查看课程中所有的学生
            for index, course_name in enumerate(course_list_or_msg):
                print(index, course_name)
    
            choice = input('请输入课程编号: ').strip()
            if choice == 'q':
                break
            if not choice.isdigit():
                continue
    
            choice = int(choice)
    
            if choice not in range(len(course_list_or_msg)):
                continue
    
            course_name = course_list_or_msg[choice]
    
            # 3.调用查看课程中所有学生接口
            flag, student_list_or_msg = teacher_interface.check_course_student_interface(
                 teacher_info.get('user'), course_name,
            )
    
            if not flag:
                print(student_list_or_msg)
                break
    
            # 4.先循环打印所有的学生,并选择学生编号,获取学生名字
            for index, student_name in enumerate(student_list_or_msg):
                print(index, student_name)
    
            choice2 = input('请输入学生编号: ').strip()
            if choice == 'q':
                break
            if not choice2.isdigit():
                continue
    
            choice2 = int(choice2)
    
            if choice2 not in range(len(student_list_or_msg)):
                continue
    
            # 获取学生名字
            student_name = student_list_or_msg[choice2]
    
            # 让老师输入修改课程的分数
            score = input('请输入修改的分数: ').strip()
            if choice == 'q':
                break
            if score.isdigit():
    
                score = int(score)
    
                if 0 <= score <= 100:
    
                    # 5.调用修改学生分数接口
                    flag, msg = teacher_interface.change_student_score_interface(
                        teacher_info['user'], student_name, course_name, score
                    )
    
                    if flag:
                        print(msg)
                        break
                else:
                    print('请输入有效的分数!')
                    continue
            else:
                print('请输入数字!')
    
    
    teacher_dict = {
        '1': login,
        '2': check_teach_course,
        '3': choose_teach_course,
        '4': check_course_student,
        '5': change_student_score,
    
    }
    
    
    def teacher_view():
        while True:
            print('''
                =============老师视图===============
                1、登录
                2、查看教授课程
                3、选择教授课程
                4、查看课程下学生
                5、修改学生成绩
                q:任意输入界面返回至学生视图
                qq:退回到主视图
                ''')
            choice = input('请输入选择:').strip()
            if choice == 'qq':
                break
            if choice not in teacher_dict:
                print('输入不合法!')
                continue
            teacher_dict[choice]()
    
    
    db包下
    
    /db_handler.py/
    
    from conf import settings
    import os
    import pickle
    import re
    
    
    # 保存数据
    def db_save(obj):
        # 拼接类绝对路径
        cls_path = os.path.join(settings.DB_PATH, obj.__class__.__name__.lower())
        # 判断类的文件夹是否存在,若不存在,则新建
        if not os.path.exists(cls_path):
            os.mkdir(cls_path)
    
        # 判断传入的名称是否合法
        if not re.search(u'^[_a-zA-Z0-9u4e00-u9fa5]+$', obj.name):
            return 0
        user_path = os.path.join(cls_path, obj.name)
        # 存储数据
        with open(user_path, 'wb') as f:
            pickle.dump(obj, f)
            f.flush()
    
    
    # 查看数据
    def db_select(cls, name):
        # 拼接绝对路径
        cls_path = os.path.join(settings.DB_PATH, cls.__name__.lower())
        # 判断类的文件是否存在,若不存在,则新建
        if not os.path.exists(cls_path):
            os.mkdir(cls_path)
    
        # 判断传入的名称是否合法
        if not re.search(u'^[_a-zA-Z0-9u4e00-u9fa5]+$', name):
            return 0
        user_path = os.path.join(cls_path, name)
        if os.path.exists(user_path):
            # 读取数据
            with open(user_path, 'rb') as f:
                obj = pickle.load(f)
                return obj
    
    
    /models.py/
    from db import db_handler
    
    
    # 父类
    class Base:
        # 保存数据
        def save(self):
            db_handler.db_save(self)
    
        # 读取数据
        @classmethod
        def select_ob_from_name(cls, name):
            obj = db_handler.db_select(cls, name)
            return obj
    
    
    # 管理员类
    class Admin(Base):
        def __init__(self, name, pwd):
            self.name = name
            self.pwd = pwd
            self.save()
    
        # 创建学校
        def create_school(self, school_name, school_addr):
            School(school_name, school_addr)
    
        # 创建老师
        def create_teacher(self, teacher_name, teacher_pwd):
            Teacher(teacher_name, teacher_pwd)
    
        # 创建课程
        def create_course(self, course_name, school_name):
            Course(course_name)
            school_obj = School.select_ob_from_name(school_name)
            school_obj.course_list.append(course_name)
            school_obj.save()
    
    
    # 学生类
    class Student(Base):
        def __init__(self, student_name, student_pwd, ):
            self.name = student_name
            self.pwd = student_pwd
            self.score = {}
            self.course_list = []
            self.school = None
            self.save()
    
        # 学生选择课程
        def choose_course(self, course_name):
            self.course_list.append(course_name)
            self.save()
            course_obj = Course.select_ob_from_name(course_name)
            course_obj.course_student_list.append(self.name)
            course_obj.save()
    
        # 学生选择校区功能
        def choose_school(self, school_name):
            self.school = school_name
            self.save()
    
        # 学生查看成绩功能
        def check_score(self):
            return self.score
    
    
    # 老师类
    class Teacher(Base):
        def __init__(self, teacher_name, teacher_pwd):
            self.name = teacher_name
            self.pwd = teacher_pwd
            self.teacher_course_list = []
            self.save()
    
        # 查看教授的课程
        def check_teach_course(self):
            return self.teacher_course_list
    
        # 选择教授的课程
        def choose_teach_course(self, course_name):
            self.teacher_course_list.append(course_name)
            self.save()
    
        # 查看课程下的学生
        def check_course_all_student(self, course_name):
            course_obj = Course.select_ob_from_name(course_name)
            return course_obj.course_student_list
    
        # 修改学生成绩
        def change_score(self, student_name, course_name, score):
            student_obj = Student.select_ob_from_name(student_name)
            student_obj.score[course_name] = score
            student_obj.save()
    
    
    # 学校类
    class School(Base):
        def __init__(self, school_name, school_addr):
            self.name = school_name
            self.school_addr = school_addr
            self.course_list = []
            self.save()
    
        # 查看所有课程
        def check_all_course(self):
            return self.course_list
    
    
    # 课程类
    class Course(Base):
        def __init__(self, course_name):
            self.name = course_name
            self.course_student_list = []
            self.save()
    
        # 添加学生
        def add_student(self, student_name):
            self.course_student_list.append(student_name)
            self.save()
    
        # 查看所有学生
        def check_all_student(self):
            return self.course_student_list
    
    
    
    
    interface包下
    
    /admin_interface.py/
    
    from db import models
    
    
    # 注册接口
    def register_interface(user, pwd):
        admin_obj = models.Admin.select_ob_from_name(user)
        if admin_obj:
            return False, '该用户已经存在!'
        elif admin_obj == 0:
            return 0, '输入不合法,只能包含汉字,字母,数字,下划线'
        else:
            models.Admin(user, pwd)
            return True, f'{user}用户注册成功!'
    
    
    # 创建学校接口
    def create_school_interface(user, school_name, school_addr):
        school_obj = models.School.select_ob_from_name(school_name)
        if school_obj:
            return False, '该学校已经存在!'
        elif school_obj == 0:
            return 0, '输入不合法,只能包含汉字,字母,数字,下划线'
        else:
            admin_obj = models.Admin.select_ob_from_name(user)
            admin_obj.create_school(school_name, school_addr)
            return True, f'学校{school_name}创建成功!'
    
    
    # 创建老师接口
    def create_teacher_interface(user, teacher_name, teacher_pwd):
        teacher_obj = models.Teacher.select_ob_from_name(teacher_name)
        if teacher_obj:
            return False, '该老师已经存在!'
        elif teacher_obj == 0:
            return 0, '输入不合法,只能包含汉字,字母,数字,下划线'
        else:
            admin_obj = models.Admin.select_ob_from_name(user)
            admin_obj.create_teacher(teacher_name, teacher_pwd)
            return True, f'老师{teacher_name}创建成功!'
    
    
    # 创建课程接口
    def create_course_interface(user, school_name, course_name):
        course_obj = models.Course.select_ob_from_name(course_name)
        if course_obj:
            return False, '该课程已经存在!'
        elif course_obj == 0:
            return 0, '输入不合法,只能包含汉字,字母,数字,下划线'
        else:
            admin_obj = models.Admin.select_ob_from_name(user)
            admin_obj.create_course(course_name, school_name)
            return True, f'课程{course_name}创建成功!'
    
        
    /common_interface.py/
    
    from db import models
    import os
    from conf import settings
    
    
    # 公共登录接口
    def common_login_interface(user, pwd, role):
        if role == 'admin':
            obj = models.Admin.select_ob_from_name(user)
        elif role == 'student':
            obj = models.Student.select_ob_from_name(user)
        elif role == 'teacher':
            obj = models.Teacher.select_ob_from_name(user)
        else:
            return False, '权限不足!'
        if obj:
            if obj.pwd == pwd:
                return True, f'{user}登录成功!'
            else:
                return False, '密码错误!'
        else:
            return False, '该用户不存在!'
    
    
    # 查询所有的学校
    def check_all_school_interface():
        school_path = os.path.join(settings.DB_PATH, 'school')
        school_list = os.listdir(school_path)
        return school_list
    
    
    # 查看学校中所有的课程接口
    def check_course_from_school_interface(school_name):
    
        school_obj = models.School.select_ob_from_name(school_name)
    
        # 有学校对象,调用查看学校中所有课程的方法
        course_list = school_obj.check_all_course()
        if course_list:
            return True, course_list
    
        return False, '学校中没有课程!'
    
    
    /student_interface/
    
    from db import models
    
    
    # 学生注册功能
    def register_interface(user, pwd):
        student_obj = models.Student.select_ob_from_name(user)
        if student_obj:
            return False, '该用户已经存在!'
        elif student_obj == 0:
            return 0, '输入不合法,只能包含汉字,字母,数字,下划线'
        else:
            models.Student(user, pwd)
            return True, f'{user}用户注册成功!'
    
    
    # 学生选课功能
    def choose_course_interface(student_name, course_name):
        student_obj = models.Student.select_ob_from_name(student_name)
        if course_name in student_obj.course_list:
            return False, '该课程已存在!'
        student_obj.choose_course(course_name)
        return True, '添加课程成功!'
    
    
    # 学生选择校区功能
    def choose_school_interface(student_name, school_name):
        student_obj = models.Student.select_ob_from_name(student_name)
        if student_obj.school:
            return False, '已选过学校'
        else:
            student_obj.choose_school(school_name)
            return True, '选择成功!'
    
    
    # 学生查看成绩功能
    def check_score_interface(student_name):
        student_obj = models.Student.select_ob_from_name(student_name)
        score_dict = student_obj.check_score()
        return score_dict
    
    
    # 获取学生对象中的所有课程
    def get_course_list_interface(student_name):
        # 1.获取当前学生对象
        student_obj = models.Student.select_ob_from_name(student_name)
        school_name = student_obj.school
    
        # 2.判断学生是否 选择了学校
        if not school_name:
            return False, '请先选择学校'
    
        # 3.获取学校对象,获取学校对象中的课程列表
        school_obj = models.School.select_ob_from_name(school_name)
        course_list = school_obj.course_list
        if course_list:
            return True, course_list
        else:
            return False, '学校中没有课程!'
    
        
        
    /teacher_interface/
    
    from db import models
    
    
    # 查看教授的课程
    def check_teach_course_interface(teacher_name):
        teacher_obj = models.Teacher.select_ob_from_name(teacher_name)
        course_list = teacher_obj.check_teach_course()
        if course_list:
            return True, course_list
        else:
            return False, '老师还没选择课程'
    
    
    # 选择教授的课程
    def choose_teach_course_interface(teacher_name, course_name):
        # 1.获取老师对象
        teacher_obj = models.Teacher.select_ob_from_name(teacher_name)
        teacher_course_list = teacher_obj.teacher_course_list
    
        # 2.判断该课程是否存在
        if course_name in teacher_course_list:
            return False, '该课程已存在'
    
        # 3.若不存在,则让老师对象 调用添加课程方法,老师添加课程。
        teacher_obj.choose_teach_course(course_name)
    
        return True, '添加课程成功!'
    
    
    # 查看课程下的学生
    def check_course_student_interface(teacher_name, course_name):
        teacher_obj = models.Teacher.select_ob_from_name(teacher_name)
        student_list = teacher_obj.check_course_all_student(course_name)
        if student_list:
            return True, student_list
        else:
            return False, '课程下还没有学生!'
    
    
    # 修改学生成绩
    def change_student_score_interface(teacher_name, student_name, course_name, score):
    
        # 1.获取老师对象
        teacher_obj = models.Teacher.select_ob_from_name(teacher_name)
    
        # 2.通过老师对象,调用修改分数方法
        teacher_obj.change_score(
            # 学生名字传入,为了获取学生对象,并修改学生中的课程成绩
            student_name, course_name, score,
        )
    
        return True, f'修改分数成功, 课程[{course_name}]分数为: [{score}]'
    
    
    
    lib包下
    
    /common.py/
    
    # 登录认证+权限认证功能
    def login_auth(user_type):
        def outer(func):
            def inner(*args, **kwargs):
                if user_type == 'admin':
                    from core import admin
                    if admin.admin_info['user']:
                        res = func(*args, **kwargs)
                        return res
                    else:
                        print('请先进行登录操作!')
                        admin.login()
                elif user_type == 'student':
                    from core import student
                    if student.student_info['user']:
                        res = func(*args, **kwargs)
                        return res
                    else:
                        print('请先进行登录操作!')
                        student.login()
                elif user_type == 'teacher':
                    from core import teacher
                    if teacher.teacher_info['user']:
                        res = func(*args, **kwargs)
                        return res
                    else:
                        print('请先进行登录操作!')
                        teacher.login()
                else:
                    print('权限不足!')
    
            return inner
    
        return outer
    
    
    start.py
    
    from core import src
    import sys
    import os
    
    
    # 添加环境变量
    sys.path.append(
        os.path.dirname(__file__)
    )
    
    
    # 测试运行
    if __name__ == '__main__':
        src.run()
    
    readme.txt
    
    1.需求分析
    
    # 项目需求如下
    '''
    
    对象角色:
        管理员对象、学生对象、老师对象、课程对象、学校对象
    
        - 至少需要创建5个类.
    
    要求:
        # 管理员模块 ---》 注册、登录、创建学校、创建课程、创建老师
      1. 创建北京、上海 2 所学校  ---》 创建学校 (学校名称与学校地址)
    
        学校课程---》 [创建课程1, 创建课程2, ...]
      2. 创建linux , python13 , go 3个课程 , linuxpy在北京开,go在上海开 ---> 创建课程(课程名称,绑定学校)
    
      3. 管理员创建学校 ,课程  ,老师  ---》 创建老师, (老师名字,老师密码)
    
      4. 学校包含学校名称,地址等属性
    
        # 学生模块 ---》 注册、登录、选择学校、选择课程、查看成绩
      5. 学生注册登录后,可以选择学校,选择课程,查看成绩
    
      6. 学生只能选择一所学校,可以从校区中选择多门课程,课程绑定给学生。
            [创建课程1, 创建课程2, ...]  <----- 学生选择学校中的一门课程, 学生课程列表[课程1, 课程2]
    
        # 老师模块 ---》 登录、查看教授课程、选择教授课程、查看课程下的学生、修改学生成绩
      7. 老师登录后,可以查看教授课程,选择想要教授的课程,查看课程下的学生,修改学生成绩等
    
    
    
    视图和功能:
    
        - 总视图: src.py
    
            让用户先进入总视图,然后再通过选择的编号 进入相应的视图。
    
            - 学生视图
                    1、注册
                    2、登录
                    3、选择校区
                    4、选择课程
                    5、查看成绩
    
            - 老师视图
                    1、登录
                    2、查看教授课程
                    3、选择教授课程
                    4、查看课程下学生
                    5、修改学生成绩
    
            - 管理视图,创建讲师, 创建班级,创建课程
                    1、注册
                    2、登录
                    3、创建学校
                    4、创建老师
                    5、创建课程
    '''
    
    
    2.项目的架构设计
        - 三层架构
            - 用户视图层
                - 接收用户输入的内容
                - 接收接口层返回的数据,并展示给用户
    
            - 接口层
                - 业务逻辑的处理
                - 将处理完的结果返回的用户视图层 ↑
                - 产生的数据: ↓
    
            - 数据处理层
                - 数据的存取
                - 存: 拿到接口层产生的数据进行保存
                - 查: 将数据返回给接口层  ↑
    
    
    
    3.分任务开发
    4.测试
    5.上线
    
    
    
    注意: 写完所有功能,务必删除所有文件后,重新测试,以免报错!
    
    分析:
    '''
    存放一个个的类:
    
    管理员对象、学生对象、老师对象、课程对象、学校对象
    
    所有类的父类:
        - Class Base:
    
        - 技能:
            - 保存数据
            - 查看数据
    
    课程对象(课程类):
        - 特征(属性):
            - 课程名字
            - 学校
            - 学生列表
    
        - 技能(方法)
            - 保存数据(课程)
            - 查看数据(课程)
    
            - 添加学校
                self.学校 = 上海校区
    
            - 添加学生
                self.学生列表.append(学生)
    
    
    学生对象(学生类):
        - class Student(Base):
            - 特征(属性):
                - 名字
                - 密码
                - 学校
                - 课程列表
                - 分数字典: {'课程名字': 课程分数, '课程名字2': 课程分数,}
    
            - 技能(方法)
                - 保存数据(注册):
                    - 调用学生类,实例化得到一个学生对象,并将学生对象保存成pickle文件。
    
                - 查看数据(登录):
                    - 从pickle文件中读出相应的对象。
    
                - 选择校区
                    学校 = oldboy
    
                - 选择课程
                    self.课程列表.append(课程)
    
                - 查看成绩
                    for line in self.分数字典:
                        print(line)
    
    
    老师对象(老师类):
        class Teacher(Base):
            - 特征(属性):
                - 名字
                - 密码
                - 课程列表
    
            - 技能(方法)
    
                - 保存数据(老师)
    
                - 查看数据 (登录)
    
                - 查看教授课程
                    self.课程列表
    
                - 选择教授课程
                    课程列表.append(课程名字)
    
                - 查看课程下学生
                    课程列表.课程---》 获取课程对象.学生列表
    
                - 修改学生成绩
                    课程列表.课程----> 获取课程对象.学生列表.学生 ----> 获取学生对象.分数字典[课程]  = 100
    
    
    管理员对象(管理员类):
        - class Admin(Base):
            - 特征(属性):
                - 名字
                - 密码
    
            - 技能(方法)
                - 保存数据 (注册)
                    - 调用管理员类,实例化得到一个管理员对象,并将管理员对象保存成pickle文件。
    
                - 查看数据 (登录)
    
                - 创建学校
                    - 调用学校类,实例化得到一个学校对象,并将学校对象保存成pickle文件。
    
                - 创建老师
                    - 调用老师类,实例化得到一个老师对象,并将老师对象保存成pickle文件。
    
                - 创建课程
                    - 调用课程类,实例化得到一个课程对象,并将课程对象保存成pickle文件。
    
    
    学校对象(学校类):
        - class School(Base):
            - 特征(属性):
                - 学校名称
                - 学校地址
                - 课程列表
                -
    
            - 技能(方法)
                - 查看数据(学校)
                - 保存数据(学校)
    
                - 添加课程
                    self.课程列表.append(课程)
    
    '''
    from db import db_handler
    
    
    # 父类
    class Base:
        # 保存 数据(对象) 方法
        def save_obj(self):
    
            # 调用db_handler中save将对象保存到pickle文件中
            db_handler.save(self)
    
        # 查看 数据(对象) 方法
        @classmethod
        def select_obj_by_name(cls, name):
    
            obj = db_handler.select(cls, name)
    
            return obj
    
    
    # 管理员类
    class Admin(Base):  # admin_obj.save_obj(admin_obj)
    
        def __init__(self, name, pwd):
            self.name = name
            self.pwd = pwd
    
        # 管理员创建学校方法
        def create_school(self, school_name, school_addr):
            # 创建对象方式一:
            # school_obj = School(school_name, school_addr)
            # school_obj.save_obj()
    
            # 创建对象方式二:
            School(school_name, school_addr)
    
        # 管理员创建课程方法
        def create_course(self, school_name, course_name):
            # 1.调用课程类 创建课程对象
            Course(school_name, course_name)
    
            # 2.获取学校对象,为学校对象中的课程列表添加课程
            school_obj = School.select_obj_by_name(school_name)
            school_obj.course_list.append(course_name)
            school_obj.save_obj()
    
        # 管理员创建老师方法
        def create_teacher(self, teacher_name, teacher_pwd):
            Teacher(teacher_name, teacher_pwd)
    
    
    # 学校类
    class School(Base):
        def __init__(self, school_name, school_addr):
            self.name = school_name
            self.addr = school_addr
            self.course_list = []
            self.save_obj()
    
        # 学校对象查看所有课程方法
        def check_all_course(self):
            return self.course_list
    
    
    # 课程类
    class Course(Base):
        '''
        - 课程名字
        - 学校
        - 学生列表
        '''
        def __init__(self, school_name, course_name):
            self.name = course_name
            self.school = school_name
            self.student_list = []
            self.save_obj()
    
        # 课程添加学生方法
        def add_student(self, student_name):
            self.student_list.append(student_name)
            self.save_obj()
    
        # 课程对象获取所有学生方法
        def check_all_student(self):
            return self.student_list
    
    
    # 学生类
    class Student(Base):
        def __init__(self, student_name, student_pwd):
            self.name = student_name
            self.pwd = student_pwd
            # 一个学生只能选择一所学校
            self.school = None
            self.course_list = []
            self.score = {}
            self.save_obj()
    
        # 学生添加学校方法
        def add_school(self, school_name):
            self.school = school_name
            self.save_obj()
    
        # 学生添加课程方法
        def add_course(self, course_name):
            # 1.添加课程
            self.course_list.append(course_name)
            # 2.为新添加的课程设置初始分数
            self.score[course_name] = 0
            self.save_obj()
    
            # 3.获取课程对象,让课程对象调用添加学生方法
            course_obj = Course.select_obj_by_name(course_name)
    
            # 调用添加学生方法
            course_obj.add_student(self.name)
    
    
    # 老师类
    class Teacher(Base):
    
        def __init__(self, teacher_name, teacher_pwd):
            self.name = teacher_name
            self.pwd = teacher_pwd
            self.course_list = []
            self.save_obj()
    
        # 老师查看所有课程方法
        def check_all_course(self):
            return self.course_list
    
        # 老师添加课程方法
        def add_course(self, course_name):
            self.course_list.append(course_name)
            self.save_obj()
    
        # 老师查看教授课程方法
        def check_student_from_course(self, course_name):
            # 1.获取课程对象
            course_obj = Course.select_obj_by_name(course_name)
    
            # 2.通过课程对象, 获取学生列表
            student_list = course_obj.check_all_student()
            return student_list
    
        # 老师修改分数方法
        def change_score(self, student_name, course_name, score):
            # 1.获取学生对象
            student_obj = Student.select_obj_by_name(student_name)
    
            # 2.直接修改学生score属性
            student_obj.score[course_name] = score
            student_obj.save_obj()
    
    
  • 相关阅读:
    LeetCode344
    LeetCode18四数之和扩展N数之和
    LeetCode383赎金信
    2018-2020创业总结
    LeetCode454四数相加
    普通dll项目添加WPF的Window对象
    WPF中RadioButton的数据绑定
    02 C# 文件压缩与解压
    WPF 使用附加属性声明 ICommand
    自定义WPF分页控件
  • 原文地址:https://www.cnblogs.com/godlover/p/11977991.html
Copyright © 2020-2023  润新知