翻了一下之前看的部分,是第16天学习的shelve模块,大致能容如下:
import shelve # shelve模块,功能上类似于json和pickle,但是操作起来比较简单,是将传入数据转化为一个字典 # 但是在写入过程中我们看不到字典的存在,同时不能跨语言进行传输 # f = shelve.open(r'shelve_test1') #此时会生成3个文件,但是你完全不用理会,因为你看不懂 # f['stu1_info1'] = {'name':'alex','age':18} #将传入的字典{'name':'alex','age':18} 变成一个v值, # # 与我们写入的'stu1_info1'行程一个键值对,就是转换成为字典 # f['stu1_info2'] = {'name':'zhoujielun','age':28} # f['shelve_test'] = {'k1':'v1'} # f.close() # print(f.get('stu1_info1')['name']) #用f.get(k值)获取你要找到的字典,然后用字典内的k值得到对应的v #输出结果为:'alex'
上面就是关于shelve模块的部分,之前自己记得好像也不是很详细,关于怎么查询的明天还是得补上,今天勉强把剩下的学生界面和教师界面搞定,很是吃力啊,自己还是差的远,白天用得少,晚上又没咋联系,后期我将着重在每周日更新一次,然后平时就当是随堂笔记了。
上货吧:
student_view:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Colin Yao 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 from core.school_view import School_view class Student_view(object): def __init__(self): if os.path.exists(setting.school_file+ '.dat'): self.school_file = shelve.open(setting.school_file) self.student_manger() self.school_file.close() else: print('没有数据,请联系管理员选择分校') sys.exit() def student_manger(self): while True: print('欢迎注册IT学院我们的分校有:') for school_name in self.school_file: print(' Sean[%s]分校' %school_name) school_option = input('请输入您要学习的分校地址>>>:').strip() if school_option in self.school_file: self.school_option = school_option self.school_obj = self.school_file[school_option] else: print('输入有误程序退出') sys.exit() for classroom in self.school_obj.sch_class_room: class_obj = self.school_obj.sch_class_room[classroom] print('欢迎来到%s分校我们的班级有:%s 课程有:%s' % (school_option, class_obj.class_name, class_obj.course_obj.course_name)) student_option = input('请输入学生姓名进行操作[PS:注册也在这里哟]>>>:').strip() if student_option in self.school_obj.sch_student: self.student_option = student_option self.student_obj = self.school_obj.sch_student[self.student_option] else: print('[%s]这位童鞋'%student_option,'您是否进行注册?') user_choice = input('请注册/退出(y/n)>>>:') if user_choice == 'y': self.student_option = student_option self.enroll() elif user_choice == 'n': sys.exit() else: print('输入有误,请重新开始 = 。= ') self.student_manger() while True: menu = ''' 欢迎来到 Python[%s]校区[%s]童鞋 缴纳学费 pay 选择教室/课程 choice_class 查看讲师 view_teacher 退出 exit ''' %(school_option,student_option) print(menu) user_choice = input('请选择以上操作>>>:').strip() if hasattr(self, user_choice): getattr(self, user_choice)() else: print("没有这个操作,请重新输入!!!!") pass '''注册''' def enroll(self): # 课程、班级 student_name = self.student_option student_gender = input('%s童鞋请输入性别>>>:' % student_name) student_age = input('%s童鞋请输入年纪>>>:' % student_name) class_name = input('%s童鞋请输入教室(PS:教室已经和课程关联)>>>:' % student_name) student_score = None if class_name in self.school_obj.sch_class_room: class_obj = self.school_obj.sch_class_room[class_name] self.school_obj.create_student(student_name, student_gender, student_age, student_score, class_name) self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() print('%s童鞋注册成功,您选择的课程是%s,教室为%s' % (student_name, class_obj.course_obj.course_name, class_obj.class_name)) else: print('创建失败教室不存在') '''缴纳学费''' def pay(self): # 根据教室缴纳学费 student_name = self.student_option class_name = input('%s童鞋请输入您所在的教室(PS:教室已经和课程关联)>>>:' % student_name) if class_name in self.school_obj.sch_class_room: # sch_class_room class_obj = self.school_obj.sch_class_room[class_name] if class_obj.course_obj.course_name in self.school_obj.school_course: course_obj = self.school_obj.school_course[class_obj.course_obj.course_name] print('所在分校:%s 课程名称:%s 课程价格:%s 课程学习计划:%s周' % (self.school_obj.school_addr, course_obj.course_name, course_obj.course_price, course_obj.course_time)) '''选择班级''' def choice_class(self): # 根据学习日期选择班级 student_name = self.student_option student_gender = self.student_obj.student_gender student_age = self.student_obj.student_age class_name = input('%s童鞋选择教室(PS:教室已经和课程关联)>>>:' % student_name) student_score = 0 # class_name ='room' if class_name in self.school_obj.sch_class_room: # sch_class_room class_obj = self.school_obj.sch_class_room[class_name] self.school_obj.create_student(student_name, student_gender, student_age, student_score, class_name) self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() print('%s更改教室成功,您选择的课程是%s,教室为%s' % (student_name, class_obj.course_obj.course_name, class_obj.class_name)) else: print('创建失败教室不存在') '''查看讲师''' def view_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 教师所在班级:%s 教授课程:%s 学生有%s' % (teacher_obj.teacher_name,t, class_obj.course_obj.course_name,stu_list)) '''退出''' def exit(self): sys.exit('感谢使用再见')
teacher_view:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Colin Yao 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 Teacher_view(object): def __init__(self): if os.path.exists(setting.school_file+ '.dat'): self.school_file = shelve.open(setting.school_file) self.teacher_manger() self.school_file.close() else: print('没有教师数据,请先创建学校') sys.exit() def teacher_manger(self): while True: print('欢迎进入IT学院讲师视图:') for school_name in self.school_file: print('讲师分校名:%s' % school_name) school_option = input('请输入您所在分校名>>>:').strip() if school_option in self.school_file: self.school_option = school_option self.school_obj = self.school_file[school_option] else: print('您的输入有误') break teacher_option = input('请输入讲师姓名进行管理>>>:').strip() if teacher_option in self.school_obj.sch_teacher: self.teacher_option = teacher_option self.teacher_obj = self.school_obj.sch_teacher[self.teacher_option] print('欢迎讲师%s进入讲师管理系统以下是您的讲师信息:' ' 讲师姓名:%s 讲师年龄:%s 讲师性别:%s 讲师工资:%s' %(self.teacher_obj.teacher_name,self.teacher_obj.teacher_name ,self.teacher_obj.teacher_age,self.teacher_obj.teacher_gender,self.teacher_obj.teacher_salary)) else: print('没有[%s]这位讲师'%teacher_option) sys.exit() while True: menu = ''' 欢迎来到 Python[%s]校区[%s]讲师 选择教室 management_class 查看班级 view_class 查看学员 view_student_list 修改成绩 revise_student_grades 退出 exit ''' %(school_option,teacher_option) print(menu) user_choice = input('请选择以上操作>>>:').strip() if hasattr(self, user_choice): getattr(self, user_choice)() else: print("没有这个操作,请重新输入!!!!") pass '''管理自己的班级''' def management_class(self): for classroom in self.teacher_obj.teacher_classroom: class_obj = self.teacher_obj.teacher_classroom[classroom] #教室对象 print('%s您所在的班级为:%s 教授的课程是%s:' % (self.teacher_obj.teacher_name, class_obj.class_name,class_obj.course_obj.course_name)) # 讲师对应教室和课程 user_choice = input('是否选择要上课的教室y/n>>>:').strip() if user_choice == 'y': class_name = input('请输入要上课的教室>>>:').strip() class_obj = self.school_obj.sch_class_room[class_name] self.school_obj.create_teacher(self.teacher_obj.teacher_name, self.teacher_obj.teacher_age, self.teacher_obj.teacher_gender,self.teacher_obj.teacher_salary,class_name, class_obj) print('讲师选择教室对应的课程成功') self.school_file.update({self.school_option:self.school_obj}) self.school_file.close() elif user_choice == 'n': print('不对班级进行管理....请重新选择您的操作!!!>>>:') pass else: print('输入不正确') pass '''上课选择班级和管理班级''' def view_class(self): # 上课选择班级, for classroom in self.teacher_obj.teacher_classroom: class_obj = self.teacher_obj.teacher_classroom[classroom] # 教室对象 print('%s您所在的班级为:%s 教授的课程是%s:' % (self.teacher_obj.teacher_name, class_obj.class_name, class_obj.course_obj.course_name)) # 讲师对应教室和课程 '''查看学员列表''' def view_student_list(self): # 查看学员列表 for classroom in self.teacher_obj.teacher_classroom: class_obj = self.teacher_obj.teacher_classroom[classroom] # 教室对象 stu_list = [] for j in class_obj.class_student: stu_list.append(j) print('%s您所在的班级为:%s 教授的课程是%s 您班级里的学生有%s:' % (self.teacher_obj.teacher_name, class_obj.class_name, class_obj.course_obj.course_name, stu_list)) # 讲师对应教室和课程有问题 '''修改成绩''' def revise_student_grades(self): self.view_student_list() for classroom in self.teacher_obj.teacher_classroom: class_obj = self.school_obj.sch_class_room[classroom] # 教室对象 student_name = input('请讲师修改成绩的学生的姓名>>>:').strip() if student_name in class_obj.class_student: student_obj = class_obj.class_student[student_name] print('%s童鞋成绩为%s 课程:%s 教室:%s' %(student_name, student_obj.student_score, class_obj.course_obj.course_name, class_obj.class_name)) student_gender = student_obj.student_gender student_age = student_obj.student_age new_score = input('请讲师修改成绩>>>:').strip() self.school_obj.create_student (student_name, student_gender, student_age, new_score,class_obj.class_name) self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() print('%s童鞋修改成功,他的成绩为%s,他的课程是%s,教室为%s' % (student_name, new_score, class_obj.class_name, class_obj.course_obj.course_name)) else: print('没有这个童鞋>>>>>!!!!') def exit(self): sys.exit('[%s]讲师感谢使用再见'%self.teacher_obj.teacher_name)
就是这些,明天去首都,好好学习一下党纪。。。