运行程序时总是提示
AttributeError: 'Class' object has no attribute 'class_student'
""" 注:bin目录 判断了系统环境,添加了环境变量,调用了主业务区main.py """ import os import sys import platform
if platform.system() == "Windows": BASE_DIR = "\".join(os.path.abspath(os.path.dirname(file)).split("\")[:-1])
else: BASE_DIR = "/".join(os.path.abspath(os.path.dirname(file)).split("/")[:-1])
sys.path.append(BASE_DIR) #print(sys.path)
from core import main from conf import settings
if name == 'main': obj = main.Manage_center() obj.run() ——————————————————————————————————————
""" 注:conf目录 定义了数据库路径 """ import os import sys import platform
if platform.system() == "Windows": BASE_DIR = "\".join(os.path.abspath(os.path.dirname(file)).split("\")[:-1]) database_path = os.path.join(BASE_DIR,"database")
else: BASE_DIR = "/".join(os.path.abspath(os.path.dirname(file)).split("/")[:-1]) database_path = os.path.join(BASE_DIR, "database")
school_db_file = os.path.join(database_path,"school")
——————————————————————————————————————
""" 注:core目录定义了4个类,管理中心,学员视图,讲师视图,学校视图 """
import os import sys import shelve from modules.school import School from conf import settings
class Manage_center(object): def init(self): pass
def run(self):
while True:
print("\n------欢迎进入校园管理页面-------\n"
"\t\t1.学校视图\n"
"\t\t2.教师视图\n"
"\t\t3.学生视图\n"
"\t\t4.按q健退出\n" )
user_choice=input("\033[34;0m请输入您要登录的视图:\033[0m")
if user_choice == '1':
Manage_student()
elif user_choice == '2':
Manage_teacher()
elif user_choice == '3':
Manage_school()
elif user_choice == 'q':
print("\033[32;1m你即将退出管理系统,感谢你的使用\033[0m")
break
else:
print("你输入的选择不正确,请重新确认")
class Manage_school(object): <>学校管理视图>
def __init__(self):
if os.path.exists(settings.school_db_file + ".dat"): # shelve会生成三个文件,其中有.dat结尾
self.school_db = shelve.open(settings.school_db_file) # 打开学校数据库文件
self.run_manger() # 运行管理视图
self.school_db.close() # 关闭数据库文件
else:
print("\33[31;1m系统信息:初始化数据库\33[0m")
self.initialize_school() # 初始化数据库
self.run_manger()
self.school_db.close()
def initialize_school(self):
'''实例化两个学校北京/上海'''
self.school_db = shelve.open(settings.school_db_file)
self.school_db['北京'] = School('北京', '中国.北京')
self.school_db['上海'] = School('上海', '中国.上海')
def run_manger(self):
'''运行学校管理视图 '''
while True:
for key in self.school_db:
print("学校名称:", key)
choice_school = input("\33[34;0m输入选择管理的学校名:\33[0m").strip()
if choice_school in self.school_db:
self.choice_school = choice_school
self.school_obj = self.school_db[choice_school]
while True:
print("\n欢迎来到老男孩%s校区\n"
"添加课程 add_course\n"
"增加班级 add_class\n"
"招聘讲师 add_teacher\n"
"查看课程 check_course\n"
"查看班级 check_class\n"
"查看讲师 check_teacher\n"
"退出程序 exit" % self.school_obj.school_name)
user_func = input('''\033[34;0m输入要操作的命令:\033[0m''').strip()
if hasattr(self, user_func):
getattr(self, user_func)()
else:
print("\33[31;1m输入错误:请输入正确的学校名\33[0m")
def add_course(self):
course_name = input('''\033[34;0m输入要添加课程的名称:\033[0m''').strip()
course_price = input('''\033[34;0m输入要添加课程的价格:\033[0m''').strip()
course_time = input('''\033[34;0m输入要添加课程的时长:\033[0m''').strip()
if course_name in self.school_obj.school_course: # 判断课程是否已经添加过
print("\33[32;1m课程存在\33[0m")
self.school_obj.create_course(course_name, course_price, course_time)
print("\33[32;1m课程更新完成\33[0m")
else:
self.school_obj.create_course(course_name, course_price, course_time)
print("\33[32;1m课程添加成功\33[0m")
self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据
def add_class(self):
class_name = input('''\033[34;0m输入要添加班级的名称:\033[0m''').strip()
course_name = input('''\033[34;0m输入要关联的课程:\033[0m''').strip()
if class_name not in self.school_obj.school_class:
if course_name in self.school_obj.school_course:
course_obj = self.school_obj.school_course[course_name]
self.school_obj.create_class(class_name, course_obj)
self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据
print("\33[32;1m班级创建成功\33[0m")
else:
print("\33[31;1m系统错误:关联的课程不存在\33[0m")
else:
print("\33[31;1m系统错误:班级已经存在\33[0m")
def add_teacher(self):
teacher_name = input('''\033[34;0m输入要招聘教师的名称:\033[0m''').strip()
teacher_salary = input('''\033[34;0m输入教师的薪资:\033[0m''').strip()
teacher_class = input('''\033[34;0m输入要关联的班级:\033[0m''').strip()
if teacher_class in self.school_obj.school_class: # 判断班级是否存在
class_obj = self.school_obj.school_class[teacher_class] # 获取班级名对应的实例
if teacher_name not in self.school_obj.school_teacher: # 判断招聘教师是否存在,不存在创建,存在更新
self.school_obj.create_teacher(teacher_name, teacher_salary, teacher_class, class_obj)
print("\33[32;1m新讲师招聘成功\33[0m")
else:
self.school_obj.update_teacher(teacher_name, teacher_class, class_obj)
print("\33[32;1m讲师已经存在,信息更新完成\33[0m")
self.school_db.update({self.choice_school: self.school_obj}) # 更新数据库数据
else:
print("\33[31;1m系统错误:关联的班级不存在\33[0m")
def check_course(self):
self.school_obj.show_course()
def check_class(self):
self.school_obj.show_class()
def check_teacher(self):
self.school_obj.show_teacher()
def exit(self):
self.school_db.close()
sys.exit("\033[32;1m欢迎下次使用学员管理系统\033[0m")
class Manage_student(object): '''学生视图''' def init(self): if os.path.exists(settings.school_db_file+'.dat'): self.school_db=shelve.open(settings.school_db_file) self.run_manger() self.school_db.close() else: print("\033[31;1m数据库文件不存在,请先创建学校\033[0m") exit() def run_manger(self): print("\033[31;1m欢迎进入学生管理视图\033[0m") for key in self.school_db: print("学校名称:", key) chonse_school = input("\33[34;0m输入选择注册的学校名:\33[0m").strip() if chonse_school in self.school_db: self.chonse_school = chonse_school self.school_obj = self.school_db[chonse_school] student_name = input('''\033[34;1m输入学生的姓名:\033[0m''') student_age = input('''\033[34;1m输入学生的年龄:\033[0m''') self.school_obj.show_class_course() class_choice = input('''\033[34;0m输入上课的班级:\033[0m''') if class_choice in self.school_obj.school_class: self.school_obj.create_student[student_name, student_age, class_choice] self.school_db.update() print("\33[32;1m学生注册成功\33[0m") else: print("\33[31;1m系统错误:输入的班级不存在\33[0m") else: print("\33[31;1m系统错误:输入的学校不存在\33[0m")
class Manage_teacher(object): '''教师视图''' def init(self): if os.path.exists(settings.school_db_file+".dat"): self.school_db=shelve.open(settings.school_db_file) self.run_manger() self.school_db.close() else: print("\033[31;1m数据库文件不存在,请先创建学校\033[0m") exit() def run_manger(self): print("\033[31;1m欢迎进入讲师管理视图\033[0m") for key in self.school_db: print("学校名称",key) chonse_school = input("\33[34;0m输入选择学校名:\33[0m").strip() if chonse_school in self.school_db: self.chonse_school=chonse_school self.school_obj=self.school_db[chonse_school] teacher_name= input('''\033[34;0m输入登录讲师的姓名:\033[0m''').strip() while True: if teacher_name in self.school_obj.school_teacher: print("\n欢迎来到教师中心\n" "查看班级 check_class\n" "退出程序 exit" ) user_funk=input('''\033[34;0m输入要操作的命令:\033[0m''').strip() if hasattr(self,user_funk): getattr(self,user_funk)(teacher_name) else: print("\033[31;1m讲师不存在\033[0m") break def check_class(self,teacher_name): self.school_obj.show_teacher_classinfo(teacher_name)
def exit(self):
self.school_db.close()
sys.exit("\033[32;1m欢迎下次使用学员管理系统\033[0m")
'''注:modules目录 定义类属性'''
class Class(object): '''班级类,包含名称,课程,学生''' def init(self,class_name,course_obj): self.class_name = class_name self.class_courese = course_obj self.class_student = #学生字典
class Course(): '''定义课程类,包含名称,价格,周期''' def init(self,course_name,course_price,course_time): self.course_name = course_name self.course_price = course_price self.course_time = course_time
from modules.course import Course from modules.classs import Class from modules.teacher import Teacher from modules.student import Student
from modules.course import Course from modules.classs import Class from modules.teacher import Teacher from modules.student import Student
class School(object): '''学校类,包含名称,地址,课程,班级,教师''' def init(self,school_name,school_addr): self.school_name = school_name self.school_addr = school_addr self.school_course = #学校所有的课程实例 self.school_class = self.school_teacher = #self.school_student =
def create_course(self,course_name,course_price,course_time):
'''创建课程'''
course_obj = Course(course_name,course_price,course_time)
self.school_course[course_name] = course_obj
def show_course(self):
'''查看课程信息'''
for key in self.school_course:
course_obj = self.school_course[key]
print("\33[32;1m课程:%s\t价格:%s\t周期:%s月\33[0m"%(course_obj.course_name,course_obj.course_price,
course_obj.course_time,))
def create_class(self,class_name,courese_obj):
'''创建班级'''
class_obj = Class(class_name,courese_obj)
self.school_class[class_name] = class_obj
def show_class(self):
for key in self.school_class:
class_obj = self.school_class[key]
print("\33[32;1m班级:%s\t关联课程:%s\33[0m" % (class_obj.class_name, class_obj.class_courese.course_name))
def show_class_course(self):
for key in self.school_class:
class_obj = self.school_class[key]
course_obj = class_obj.class_courese
print("\33[32;1m班级:%s\t关联课程:%s\t价格:%s\t周期:%s月\33[0m" % (class_obj.class_name, course_obj.course_name,
course_obj.course_price,course_obj.course_time))
def create_teacher(self,teacher_name, teacher_salary,class_name,class_obj):
'''创建讲师'''
teacher_obj = Teacher(teacher_name, teacher_salary)
teacher_obj.teacher_add_class(class_name,class_obj)
self.school_teacher[teacher_name] = teacher_obj
def update_teacher(self,teacher_name,class_name,class_obj):
'''更新教师信息'''
teacher_obj = self.school_teacher[teacher_name]
teacher_obj.teacher_add_class(class_name,class_obj)
def show_teacher(self):
'''查看讲师信息'''
for key in self.school_teacher:
teacher_obj = self.school_teacher[key]
class_list = []
for i in teacher_obj.teacher_calss:
class_list.append(i)
print("\33[32;1m讲师:%s\t薪资:%s\t关联班级:%s\33[0m" % (teacher_obj.teacher_name, teacher_obj.teacher_salary,
class_list ))
def create_student(self,student_name,student_age,class_choice):
'''注册学生'''
student_obj = Student(student_name,student_age) #生成学生实例
class_obj = self.school_class[class_choice] #获取学生所注册班级的实例对象
class_obj.class_student[student_name]=student_obj #班级实例里添加学生信息
self.school_class[class_choice] = class_obj #学校班级字典更新
def show_teacher_classinfo(self,teacher_name):
teacher_obj = self.school_teacher[teacher_name]
for i in teacher_obj.teacher_class:
class_obj = self.school_class[i]
student_list = []
for k in class_obj.class_student:
student_list.append(k)
print("\33[32;1m班级:%s\t关联课程:%s\t学员:%s\33[0m" % (class_obj.class_name, class_obj.class_courese.course_name,
student_list))
class Student(object): '''学生类,包含姓名,年龄''' def init(self,student_name,student_age): self.student_name = student_name self.student_age = student_age
class Teacher(object): '''讲师类,定义teacher_name,teacher_salary,包含teacher_class''' def init(self, teacher_name, teacher_salary): self.teacher_name = teacher_name self.teacher_salary = teacher_salary self.teacher_class = #班级列表 [s14,15]
def teacher_add_class(self,class_name,class_obj):
self.teacher_class[class_name] = class_obj