码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习第72天(复习:选课实现习题)

时间:2020-05-10 01:43:13      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:ice   休息   shel   rom   成功   管理   append   讲师   上海   

  之前有一个关于选课的练习,一直没有给实现,趁着明天休息,琢磨一下给弄出来,本来以为今天能搞定,看来是我想多了。

  先看一下文件分布吧

  技术图片

 

   没啥可多说的,今天完成了一部分,剩下的明天搞定。

  bin文件下:  

import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from conf import setting
from core.main import Admin

if __name__ == __main__:
    Admin.run()

  conf文件下的setting  

import os,time,shelve,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from src.class_school import School
data_path = os.path.join(BASE_DIR+\db)
school_file =os.path.join(data_path,school)

  core文件下的main

import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from core.school_view import School_view
from core.teacher_view import Teacher_view
from coer.student_view import Student_view

class Admin:
    def run(self):
        exit_flag = False
        menu = u‘‘‘
        \033[32;1m
        1.学生视图
        2.老师视图
        3.学校视图
        4.退出\033[0m
        ‘‘‘
        choice = {
            1: Student_view ,
            2: School_view ,
            3: School_view ,
            4: exit()
        }
        while not exit_flag:
            print(menu)
            user_option = input(请输入你想进入的界面>>>)
            if user_option in choice:
                choice[user_option]()
            else:
                print(输入指令不正确!!!请重新输入!!!)

if __name__ == __main__:
    a = Admin()
    a.run()

  core文件下的school_view

import os,sys,logging,shelve
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from conf import setting
from src.class_school import School

class School_view:
    choice = {
        1: add_course,
        2: add_classroom,
        3: add_teacher,
        4: show_classroom,
        5: show_teacher,
        6: add_teacher,
        7:exit()
    }
    def __init__(self):
        if os.path.exists(setting.school_file+.dat):
            self.school_file = shelve.open(setting.school_file)
            self.school_manger()
            self.school_file.close()

        else:
            print(没有学校和班级数据,请先进行创建)
            self.init_school()
            self.school_manger()
            self.school_file.close()

    def init_school(self):
        self.school_file = shelve.open(setting.school_file)
        self.school_file[北京] = School(总校,北京)
        self.school_file[上海] = School(总校,上海)

    def school_manger(self):
        while True:
            for school_name in self.school_file:
                print(学校名称是:%s%school_name)
            school_option = input(请输入需要管理的学校名称)
            if school_option in self.school_file:
                self.school_option = school_option
                self.school_obj = self.school_file[school_option]
            else:
                print(请重新输入!!!)
            # ‘‘‘ 如果是北京就等于self.school_obj[北京]
            #     file[北京] =School(‘总校‘,‘北京‘)
            #     所以当下文出现了self.school_obj.school_course =School(‘总校‘,‘北京‘).school_course
            #     等同于调用了School().school_course的属性是
            #     ‘‘‘
            while True:
                menu = 1.添加课程                        2.添加班级                        3.添加讲师                        4.查看班级、课程                        5.查看讲师                         6.修改讲师                        7.退出
                print(欢迎来到Python学习%s校区%self.school_option)
                print(menu)
                user_choice = input(请选择你所需的功能>>>)
                if hasattr(self,choice[user_choice]):
                    func = getattr(self,choice[user_choice])
                else:
                    print(指令输入错误。)
                    pass

    def add_course(self):
        course_name = input(请输入课程名称>>>:).strip()
        course_price = input(请输入课程价格>>>:).strip()
        course_time = input(请输入课程周期[默认为周]>>>:).strip()
        if course_name in self.school_obj.school_course:
            print(课程已经存在)
        else:
            self.school_obj.create_course(course_name, course_price,
                                          course_time)  # 相当于School.create_course(course_name, course_price, course_time)
            print(%s课程添加成功 % course_name)
        self.school_file.update({self.school_option: self.school_obj})
        self.school_file.close()


    def add_classroom(self):
        class_name = input(请输入班级名称>>>:).strip()
        class_course = input(请输入课程>>>:).strip()
        if class_name not in self.school_obj.sch_class_room:  # sch_class_room
            if class_name not in self.school_obj.school_course:
                course_obj = self.school_obj.school_course[class_course]
                self.school_obj.create_classroom(class_name, course_obj)
                self.school_file.update({self.school_option: self.school_obj})
                self.school_file.close()
                print(班级创建成功)
            else:
                print(课程不存在)
        else:
            print(班级已经存在)


    def show_classroom(self):
        for classroom in self.school_obj.sch_class_room:
            class_obj = self.school_obj.sch_class_room[classroom]
            print(班级名称:%s\t课程:%s
                  % (class_obj.class_name, class_obj.course_obj.course_name))


    def add_teacher(self):
        teacher_name = input(请输入讲师姓名>>>:)
        teacher_age = input(请输入讲师年龄>>>:)
        teacher_gender = input(请输入讲师性别>>>:)
        teacher_salary = input(请输入讲师薪水>>>:)
        class_name = input(请输入授课班级>>>:)

        if class_name in self.school_obj.sch_class_room:  # sch_class_room
            class_obj = self.school_obj.sch_class_room[class_name]
            if teacher_name not in self.school_obj.sch_teacher:
                self.school_obj.create_teacher(teacher_name, teacher_age, teacher_gender, teacher_salary, class_name,
                                               class_obj)
                print(讲师创建成功,请确认是否已经签完合同)
            else:
                self.school_obj.modify_teacher({})
                print(修改讲师成功)
            self.school_file.update({self.school_option: self.school_obj})
            self.school_file.close()
        else:
            print(请先创建班级)


    def show_teacher(self):
        for teacher_name in self.school_obj.sch_teacher:
            teacher_obj = self.school_obj.sch_teacher[teacher_name]
            for t in teacher_obj.teacher_classroom:
                class_obj = self.school_obj.sch_class_room[t]
                stu_list = []
                for j in class_obj.class_student:
                    stu_list.append(j)
                print(教师姓名:%s\t教师所在班级:%s\t教授课程:%s\t课程学员:%s % (teacher_obj.teacher_name
                                                                , t, class_obj.course_obj.course_name,
                                                                class_obj.class_student))


    def exit(self):
        sys.exit(程序退出)

  关于logging模块、shelve模块,基本忘的差不多了,现在内容逐渐多了,复习貌似更重要,不能过于激进,争取端午节前实现自己的承诺。

Python学习第72天(复习:选课实现习题)

标签:ice   休息   shel   rom   成功   管理   append   讲师   上海   

原文地址:https://www.cnblogs.com/xiaoyaotx/p/12861410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!