• python练习---校园管理系统


    角色:

    学校、学员、课程、讲师

    要求:

    1、创建北京、上海2所学校(已完成)

    2、创建linux、python、go3个课程,linux/python在北京开,go在上海开(已完成)

    3、课程包含周期、价格(已完成)

    4、班级关联课程、讲师(已完成)

    5、创建学员时,选择学校,关联班级(已完成)

    6、提供三个角色视图

      6.1:学员视图,登录,查看课程,查看班级(已完成)

      6.2:讲师视图,讲师可以查看自己教学的班级,课程(已完成)

        进阶需求:可管理自己的班级,查看班级学员列表,修改所管理的成绩(未完成

      6.3:管理视图,创建讲师,创建班级,创建课程(已完成)

    7、上面的操作产生的数据通过pickle、序列化保持到文件中(已完成)

    下面是具体的实现代码:

    代码目录主要有:

    实现功能:
        1、根据登录用户进行判断,如果是学生就进入学生视图,是老师就进入老师视图,是管理员就进入管理员视图
            课程中关联学校、讲师等属性;
            班级中关联课程、讲师等属性
            学生对象关联了学校、班级等属性;
    
        2、学生视图:
            a:查看课程
            b:查看学校
            c:查看班级
        3、老师视图:
            a:查看学校
            b:查看讲师
            c:查看班级
            d:查看课程
        4、管理员视图:
            a:创建学校
            b:创建课程
            c:创建班级
            d:创建学员
            e:创建讲师
            f:查看学校
            g:查看讲师
            h:查看班级
            i:查看课程
            j:给课程关联讲师
            k:给学员指定班级
    
    目录结构:
        bin  --->  程序执行的总入口,直接在pycharm或者其他编辑器中执行start.py
        conf --->  配置文件目录,settings.py文件中都是一些文件路径信息
        core --->  程序核心目录,里面分别包含了'学校类''老师类''学生类''班级类''课程类''管理员类';其中Main.py(程序主要逻辑判断文件)文件中主要是实现登录验证、根据不同登录用户反射到对应的类中
        db   --->  信息存储目录,主要记录'学校''老师''学生''班级''课程''用户账号密码'等信息
        log  --->  程序运行中产生的日志信息,主要是记录对程序执行的所有信息
    
    
    管理员账号密码:admin 123
    老师账号密码:goddess 123
    学生账号密码:jack 123
    README.me
    #! /bin/env python
    # coding: utf-8
    import os
    import sys
    
    #将项目需要导入的包路径导入到环境变量中
    # sys.path.append(os.path.dirname(os.getcwd()))
    sys.path.append(os.path.dirname(os.path.dirname(__file__)))
    
    # print(os.path.dirname(__file__))
    from core import Main
    
    
    if __name__ == '__main__':
        print('33[1;36m欢迎进入校园管理系统!33[0m')
        Main.main()
    bin/start.py
    #! /bin/env python
    # coding=utf-8
    import os
    
    path = os.path.dirname(os.path.dirname(__file__))
    
    userinfo = r'%s/db/userinfo' % path
    schoolinfo = r'%s/db/school_info' % path
    teacherinfo = r'%s/db/teacher_info' % path
    classinfo = r'%s/db/class_info' % path
    studentinfo = r'%s/db/student_info' % path
    courseinfo = r'%s/db/course_info' % path
    
    log = r'%s/log/test.log' % path
    conf/settings.py
    #! /bin/env python
    # coding: utf-8
    
    class Class(object):
        def __init__(self,name):
            self.class_name = name
    
    
        def __repr__(self):  #内置的方法,让打印的对象丰富起来
            show_str = ''
            for key in self.__dict__:
                show_str +='%s : %s ' % (key,  self.__dict__[key])
    
            return show_str
    core/Class.py
    #! /bin/env python
    # coding=utf-8
    
    
    class Course(object):
        def __init__(self,name,time,price):
            self.course_name = name
            self.course_time = time
            self.course_price = price
    
        def __repr__(self):  #内置的方法,让打印的对象丰富起来
            show_str = ''
            for key in self.__dict__:
                show_str +='%s : %s ' % (key,  self.__dict__[key])
    
            return show_str
    core/Course.py
    #! /usr/bin/env python
    # coding: utf-8
    
    import logging
    import sys
    from conf import settings
    
    class Log:
        def __init__(self):
            pass
    
        def record(self,s):
            logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s[line:%(lineno)d] %(message)s',
                                filename=settings.log, level=logging.INFO)
            logging.info(s)
    core/Log.py
    #! /bin/env python
    # coding: utf-8
    import sys
    import hashlib
    from collections import OrderedDict
    from core.Manager import Manager
    from core.Student import Student
    from core.Teacher import Teacher
    
    from conf import settings
    
    status_dic = {
        'username': None,
        'status': False,
        'identity': None
    }
    
    # 登录认证
    def login():
        # if status_dic['status'] == False:
            user = input('username>>').strip()
            pwd = input('password>>').strip()
            hash_obj = hashlib.md5(user.encode('utf-8'))   #md5前需要先转成base64
            hash_obj.update(pwd.encode('utf-8'))
            md5_pwd = hash_obj.hexdigest()
            with open(settings.userinfo) as f:
                for line in f:
                    username, password, identity = line.strip().split('|')
                    if username == user and password == md5_pwd and identity == 'Manager':
    
                        print('33[1;32m欢迎%s管理员用户登录33[0m'.center(50,'*') % user)
                        return {'username': user, 'identity': identity}
                    elif username == user and password == md5_pwd and identity == 'Teacher':
                        print('33[1;32m欢迎%s老师用户登录33[0m'.center(50,'*') % user)
                        return {'username': user, 'identity': identity}
                    elif username == user and password == md5_pwd and identity == 'Student':
                        print('33[1;32m欢迎%s学生用户登录33[0m'.center(50,'*') % user)
                        return {'username': user, 'identity': identity}
    
    
    
    def main():
        ret = login()
        if ret:
            if hasattr(sys.modules[__name__],ret['identity']):
                Role_Cls = getattr(sys.modules[__name__],ret['identity'])
                role_obj = Role_Cls(ret['username'])
                while True:
                    d = {}
                    for num,key in enumerate(role_obj.dic,1):
                        print(num,key)
                        d[str(num)]=key
                    try:
                        num = input('33[1;32m请输入您要做的操作序号 :33[0m')
                        func_name = role_obj.dic[d[num]]
                        if hasattr(role_obj, func_name):
                            getattr(role_obj, func_name)()   #根据用户的选择去执行具体的方法
                    except Exception:
                        pass
    
    
    
    if __name__ == '__main__':
        # hash_obj = hashlib.md5('alex'.encode('utf-8'))
        # hash_obj.update('123'.encode('utf-8'))
        # md5_pwd = hash_obj.hexdigest()
        # print(md5_pwd)
        pass
    core/Main.py
    #! /bin/env python
    # coding: utf-8
    import pickle
    import hashlib
    import logging
    from collections import OrderedDict
    from core.Mypickle import Mypickle
    from core.Teacher import Teacher
    from core.Class import Class
    from core.Course import Course
    from core.Student import Student
    from core.School import School
    from core import Log
    from conf import settings
    
    
    
    
    class Manager:
        dic = OrderedDict([ ('创建学校','create_school'),
                            ('创建学员', 'create_student'),
                            ('创建讲师', 'create_teacher'),
                            ('创建课程', 'create_course'),
                            ('创建班级', 'create_class'),
                            ('查看学校', 'show_school'),
                            ('查看讲师', 'show_teacher'),
                            ('查看班级', 'show_class'),
                            ('查看课程', 'show_course'),
                            ('退出', 'quit')])
    
        def __init__(self, name):
            self.name = name
            self.school_pickle = Mypickle(settings.schoolinfo)
            self.teacher_pickle = Mypickle(settings.teacherinfo)
            self.class_pickle = Mypickle(settings.classinfo)
            self.student_pickle = Mypickle(settings.studentinfo)
            self.course_pickle = Mypickle(settings.courseinfo)
            self.obj = Log.Log()
    
        # 创建学校
        def create_school(self):
            school_name = input('school name>').strip()
            school_addr = input('school addr>').strip()
            school_obj = School(school_name, school_addr)
            self.school_pickle.dump(school_obj)
            a = 'Create a school'
            self.obj.record(a)
    
            print('33[1;31m学校创建成功!33[0m')
    
        #创建课程
        def create_course(self):
            course_name = input('course name>').strip()
            course_time = input('course time>').strip()
            course_price = input('course price>').strip()
    
            #选择学校
            self.show_school()
    
            school_num = input('请输入课程所开设的校区序号:').strip()
            school_obj = self.school_pickle.get_item(int(school_num))
    
            #创建一个属于课程的新对象
            course_obj = Course(course_name,course_time,course_price)
            #将课程与学校绑定到一起
            course_obj.school = school_obj
    
            #将课程对象dump写入courseinfo文件中
            course_pickle = Mypickle(settings.courseinfo)
            course_pickle.dump(course_obj)
            print('33[1;31m课程创建成功!33[0m')
    
            a = 'Create a course'
            self.obj.record(a)
    
        #创建班级
        def create_class(self):
            class_name = input('class name>').strip()
    
            #关联课程
            self.show_course()
    
            course_num = input('请输入班级关联的课程序号:').strip()
            course_obj = self.course_pickle.get_item(int(course_num))
    
            #关联讲师
            self.show_teacher()
            teacher_num = input('请输入班级关联的讲师序号:').strip()
            teacher_obj = self.teacher_pickle.get_item(int(teacher_num))
    
            # 创建一个属于班级的对象
            class_obj = Class(class_name)
            class_obj.course = course_obj
            class_obj.teacher = teacher_obj
            class_pickle = Mypickle(settings.classinfo)
            class_pickle.dump(class_obj)
    
            print('33[1;31m班级创建成功!33[0m')
            a = 'Create a class'
            self.obj.record(a)
    
        #创建学员账号
        def create_student(self):
            student_name = input('student name>').strip()
            student_pwd = input('student password>').strip()
            md5_obj = hashlib.md5(student_name.encode('utf-8'))
            md5_obj.update(student_pwd.encode('utf-8'))
            md5_passwd = md5_obj.hexdigest()
            user = '%s|%s|%s
    ' % (student_name,md5_passwd,'Student')
            with open(settings.userinfo,'a',encoding='utf-8') as f:
                f.write(user)
    
            #给学员选择学校
            self.show_school()
            school_num = input('请输入学生所在的校区序号:').strip()
            school_obj = self.school_pickle.get_item(int(school_num))
            #创建一个属于学生的新对象
            student_obj = Student(student_name)
            student_obj.school = school_obj
            student_pickle = Mypickle(settings.studentinfo)
            student_pickle.dump((student_obj))
            print('33[1;31m学生创建成功!33[0m')
            a = 'Create a student'
            self.obj.record(a)
    
        #创建讲师账号
        def create_teacher(self):
            teacher_name = input('teacher name>').strip()
            teacher_pwd = input('teacher password>').strip()
            md5_obj = hashlib.md5(teacher_name.encode('utf-8'))
            md5_obj.update(teacher_pwd.encode('utf-8'))
            md5_passwd = md5_obj.hexdigest()
            user = '%s|%s|%s
    ' % (teacher_name,md5_passwd,'Teacher')
            with open(settings.userinfo,'a',encoding='utf-8') as f:
                f.write(user)
    
            #给老师选择一个校区
            self.show_school()
    
            school_num = input('请输入老师所在的校区序号:').strip()
            school_obj = self.school_pickle.get_item(int(school_num))
            #创建一个属于老师的新对象
            teacher_obj = Teacher(teacher_name)
    
            #将老师选择的校区和老师对象绑定在一起
            teacher_obj.school = school_obj
    
            #将老师对象dump进入teacherinfo文件
            teacher_pickle = Mypickle(settings.teacherinfo)
            teacher_pickle.dump(teacher_obj)
            print('33[1;31m老师创建成功!33[0m')
            a = 'Create a teacher'
            self.obj.record(a)
    
        # 查看学校
        def show_school(self):
            for num,school_obj in enumerate(self.school_pickle.load(),1):
                print(num,school_obj)
            print('')
            a = 'View school information'
            self.obj.record(a)
    
    
    
        #查看讲师
        def show_teacher(self):
            for num,teacher_obj in enumerate(self.teacher_pickle.load(),1):
                print(num,teacher_obj)
            a = 'View instructor information'
            self.obj.record(a)
    
        #查看学生
        def show_student(self):
            for num,student_obj in enumerate(self.student_pickle.load(),1):
                print(num,student_obj)
            a = 'View student information'
            self.obj.record(a)
    
        #查看班级
        def show_class(self):
            for num,class_obj in enumerate(self.class_pickle.load(),1):
                print(num,class_obj)
            a = 'View class information'
            self.obj.record(a)
    
        #查看课程
        def show_course(self):
            for num,course_obj in enumerate(self.course_pickle.load(),1):
                print(num,course_obj)
            a = 'View course information'
            self.obj.record(a)
    
    
    
        def quit(self):
            print('33[1;31m再见啦!!!33[0m')
            a = 'Program exits'
            self.obj.record(a)
            exit()
    core/Manger.py
    #! /bin/env python
    # coding: utf-8
    import pickle
    
    
    class Mypickle:
        def __init__(self,filepath):
            self.filepath = filepath
    
    
        def dump(self,school_obj):
            with open(self.filepath, 'ab') as f:
                pickle.dump(school_obj, f)
                f.flush()
    
    
        def load(self):
            with open(self.filepath,'rb') as f:
                while True:
                    try:
                        schoole_obj = pickle.load(f)
                        yield schoole_obj
                        # print(schoole)
                    except Exception:
                        break
    
        def get_item(self,num):
            with open(self.filepath, 'rb') as f:
                while num > 0:
                    try:
                        schoole_obj = pickle.load(f)
    
                    except Exception:
                        break
                    num -= 1
            return schoole_obj
    core/Mypickle.py
    #! /bin/env python
    # coding=utf-8
    
    class School:
        def __init__(self, name, addr):
            self.school_name = name
            self.school_addr = addr
    
        def __repr__(self):  #内置的方法,让打印的对象丰富起来
            show_str = ''
            for key in self.__dict__:
                show_str +='%s:%s ' % (key,  self.__dict__[key])
    
            return show_str
    core/School.py
    #! /bin/env python
    # coding=utf-8
    from collections import OrderedDict
    from core import Manager
    
    class Student:
        dic = OrderedDict([('查看学校', 'show_school'),
                           ('查看班级', 'show_class'),
                           ('查看课程', 'show_course'),
                           ('退出', 'quit')])
        def __init__(self,name):
            self.student_name = name
            self.obj = Manager.Manager(name)
    
    
        def __repr__(self):  #内置的方法,让打印的对象丰富起来
            show_str = ''
            for key in self.__dict__:
                if key == 'obj':continue
                show_str +='%s:%s ' % (key,  self.__dict__[key])
    
            return show_str
    
        def show_school(self):
            self.obj.show_school()
    
    
        def show_class(self):
            self.obj.show_class()
    
        def show_course(self):
            self.obj.show_course()
    
        def quit(self):
            self.obj.quit()
    core/Student.py
    #! /bin/env python
    # coding=utf-8
    from collections import OrderedDict
    from core import Manager
    
    
    class Teacher:
        dic = OrderedDict([('查看学校', 'show_school'),
                           ('查看讲师', 'show_teacher'),
                           ('查看班级', 'show_class'),
                           ('查看课程', 'show_course'),
                           ('退出', 'quit')])
    
        def __init__(self,name):
            self.teacher_name = name
            self.obj = Manager.Manager(name)
    
    
        def __repr__(self):  #内置的方法,让打印的对象丰富起来
            show_str = ''
            for key in self.__dict__:
                if key == 'obj':continue
                show_str +='%s:%s ' % (key,  self.__dict__[key])
    
            return show_str
    
    
    
        def show_school(self):
            self.obj.show_school()
    
        def show_teacher(self):
            self.obj.show_teacher()
    
        def show_class(self):
            self.obj.show_class()
    
        def show_course(self):
            self.obj.show_course()
    
        def quit(self):
            self.obj.quit()
    core/Teacher.py
  • 相关阅读:
    LOJ2323. 「清华集训 2017」小 Y 和地铁 【搜索】【思维】【好】
    BZOJ2687 交与并/BZOJ2369 区间【决策单调性优化DP】【分治】
    BZOJ1563 NOI2009 诗人小G【决策单调性优化DP】
    LOJ6039. 「雅礼集训 2017 Day5」珠宝【决策单调性优化DP】【分治】【思维好题】
    BZOJ4709 Jsoi2011 柠檬【决策单调性+单调栈】
    BZOJ2216 Poi2011 Lightning Conductor 【决策单调性优化DP】
    BZOJ3675 Apio2014 序列分割 【斜率优化】
    BZOJ4566 Haoi2016 找相同字符【广义后缀自动机】
    51nod 1600 Simple KMP【后缀自动机+LCT】【思维好题】*
    linux usermod
  • 原文地址:https://www.cnblogs.com/watchslowly/p/9129060.html
Copyright © 2020-2023  润新知