python----选课系统
第一次自己做一个系统,还是很开心哒~~
知识点记录
1、类的静态方法
@staticmethod def get_name(): b = common.get_obj_name(settings.db_type[1]) return b
与类对象无关,可直接通过类名进行调用
2、pickle模块
可存储的数据类型:
- 所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串,字节,None。
- 由任何原生类型组成的列表,元组,字典和集合。
- 函数,类,类的实例
常用的方法
- pickle.dump(obj, file, protocol=None,)
注解:将对象obj保存到文件file中去。
protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示;1:老式的二进制协议;2:2.3版本引入的新二进制协议,较以前的更高效。其中协议0和1兼容老版本的python。protocol默认值为0。
file:对象保存到的类文件对象。file必须有write()接口, file可以是一个以'w'方式打开的文件或者一个StringIO对象或者其他任何实现write()接口的对象。如果protocol>=1,文件对象需要是二进制模式打开的。
- pickle.load(file)
注解:从file中读取一个字符串,并将它重构为原来的python对象。
file:类文件对象,有read()和readline()接口。
-
pickle.dumps(obj[, protocol])
作用:将obj对象序列化为string形式,而不是存入文件中
obj:想要序列化的obj对象
protocal:如果该项省略,则默认为0。如果为负值或HIGHEST_PROTOCOL,则使用最高的协议版本
-
pickle.loads(string)
作用:从string中读出序列化前的obj对象
3、truncate
def db_fileopen_write(db_type,db_id,db_write): path = os.path.join(settings.db_path,db_type,db_id) f = open(path,'wb') f.seek(0)#用于将指针指到文件开头处 f.truncate() pickle.dump(db_write,f) f.close()
此处用于删除文件之前原有的内容,用于更新类对象状态
truncate() 方法语法如下:
fileObject.truncate([size])
size -- 可选,如果存在则文件从开头截断为指定字节。
一、系统目录
二、实现代码
2.1 bin
2.1.1 run.py
程序的入口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from src import main if __name__ == '__main__': main.main()
2.2 confg
2.2.1 settings
配置文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os import sys #path路径 BASIC_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASIC_PATH) db_path = os.path.join(BASIC_PATH,'db') teacher_path = os.path.join(db_path,'teacher_db') #define类型 db_type={ 1:'teacher_db', 2:'les_db', 3:'students_db', 4:'class_db' } auth_type = { 1:'学生', 2:'管理员' } #string condition = False manager_name = 'admin' manager_passwd = '1234' welcome = ''' -------------------------%s------------------------------- ''' welcome1 = welcome%'欢迎登录选课系统' welcome2 = welcome%'欢迎进入选课系统' admin_choice = ''' 作为管理员,您有以下选择: 1、添加新老师 2、添加新课程 3、添加新班级 4、退出登陆 ''' noteach_choice = ''' 1、添加新教师 2、查看现有教师 ''' students_choice ={1:''' 1、学员注册 2、学员登陆 3、返回上一级 ''', 2:''' 1、选择班级 2、查看班级详情 3、查看课程情况 4、退出登录 ''' } main_choice = ''' 1、管理员 2、学生 '''
2.3 db
以一个对象为一个文件的方式存放类对象,名称为该对象的id号
例:
2.3.1 class_db
2.3.2 les_db
2.3.3 studengts_db
2.3.4 teacher_db
2.4 lib
2.4.1 common
存放常用且通用的函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import uuid from confg import settings import os import pickle #获得唯一ID号 def get_id(): m = str(uuid.uuid1()) return m #db_type:对象类别,db_id:对象id号,读取db包下相应的类对象,并返回 def db_fileopen_read(db_type,db_id): path = os.path.join(settings.db_path,db_type,db_id) f = open(path,'rb') m = pickle.load(f) f.close() return m #将db_type类型的对象db_write,以其id号为名称存入相应数据库包中 def db_fileopen_write(db_type,db_id,db_write): path = os.path.join(settings.db_path,db_type,db_id) f = open(path,'wb') f.seek(0) f.truncate() pickle.dump(db_write,f) f.close() #以列表的形式返回某db_type类型下所有文件的id号 def get_id_list(db_type): path = os.path.join(settings.db_path,db_type) db_list = os.listdir(path) n = 0 for i in db_list: if i == '__init__.py': del db_list[n] n += 1 return db_list #以列表形式返回某db_type类型下的所有类对象 def get_object(db_type): db_list = get_id_list(db_type) b = [] for i in db_list: obj =db_fileopen_read(db_type,i) b.append(obj) return b #以列表形式返回某db_type类型下所有对象的名字 def get_obj_name(db_type): m = get_object(db_type) # print(m) obj_name=[] for i in m: obj_name.append(i.name) return obj_name #展示列表信息 def show_list(li): for i in li: print(i) #通过类对象的名字返回类对象本身 def name_obj(db_type,name): obj = get_object(db_type) for i in obj: if name == i.name: return i else: return False #验证密码是否正确 def athu(db_passwd,passwd): if db_passwd == passwd: settings.condition =True print(settings.welcome1) return True else: return False
2.4.2 modle
定义类,类中常有的静态方法:
get_all:得到该类下的所有对象
get_name:得到该类下的所有对象名称
普通方法:
introduction:介绍类对象的基本情况
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from confg import settings from lib import common class Teacher: def __init__(self,name,sex,age): self.name = name self.age = age self.sex = sex self.id = common.get_id() @staticmethod def get_all(): a= common.get_object(settings.db_type[1]) return a @staticmethod def get_name(): b = common.get_obj_name(settings.db_type[1]) return b class Classes: def __init__(self,name,lesson): self.name = name self.lesson = lesson self.id = common.get_id() @staticmethod def get_all(): a= common.get_object(settings.db_type[4]) return a @staticmethod def get_name(): b = common.get_obj_name(settings.db_type[4]) return b def introduction(self): print('班级名称:%s' %self.name) print('班级应上的课程有:') for i in self.lesson: print(i.name) def lesson_list(self): les_name_list = [] for i in self.lesson: les_name_list.append(i.name) return les_name_list class Student: def __init__(self,name,passwd,sex,age): self.name = name self.passwd = passwd self.sex = sex self.age = age self.id = common.get_id() self.cls = None class Lessons: def __init__(self,name,time,cost,teacher): self.name = name self.time = time self.cost = cost self.teacher = teacher self.id = common.get_id() @staticmethod def get_all(): b = common.get_object(settings.db_type[2]) return b @staticmethod def get_name(): a = common.get_obj_name(settings.db_type[2]) return a def introduction(self): print('''课程详情: 课程名称:%s 上课时间:%s 上课老师:%s'''%(self.name,self.time,self.teacher.name))
2.5 src
2.5.1 admin.py
管理员视图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from confg import settings from lib import modle,common def create_tea(): while 1: name = input('请输入新加入老师的姓名:') sex = input('请输入%s老师的性别:'%name) age = input('请输入%s老师的年龄:'%name) new_teacher = modle.Teacher(name,sex,age) common.db_fileopen_write(settings.db_type[1],new_teacher.id,new_teacher) print('成功录入新老师%s'%name) m = int(input('继续录入新老师?(是:1;否:0)')) if m == 0: break return new_teacher def create_les(): while 1: name = input('请输入新课程的名称:') time = input('新课程的开课时间:') cost = input('一学期的费用:') tea = modle.Teacher.get_all() tea_name = input('请输入授课老师:') teanamelist = modle.Teacher.get_name() if tea_name not in teanamelist: print('系统中没有%s老师的记录,您有以下选择:%s'%(tea_name,settings.noteach_choice)) c1 = input('请选择:') if c1 == '1': new_tea = create_tea() new_les = modle.Lessons(name,time,cost,new_tea) common.db_fileopen_write(settings.db_type[2],new_les.id,new_les) print('成功添加新课程') elif c1 == '2': print('有%s位老师可以选择,如下:'%len(tea)) for i in teanamelist: print(i) tea_choice = input('请选择:') teacher = modle.Teacher.get_all() cho_tea = teacher[tea_choice] new_les = modle.Lessons(name,time,cost,cho_tea) common.db_fileopen_write(settings.db_type[2],new_les.id,new_les) print('成功添加新课程') else: for i in tea: if tea_name == i.name: common.db_fileopen_write(settings.db_type[2],i.id,i) print('成功添加新课程') m = int(input('继续添加新课程?(是:1;否:0)')) if m == 0: break def creat_class(): while 1: cls_name = input('请输入新班级的名字') print('有以下课程可供选择:') les_name = modle.Lessons.get_name() common.show_list(les_name) les_list = [] while 1: les_choice = input('请选择(输入exit退出):') if les_choice == 'exit': break else: if les_choice in les_name: les_list.append(les_choice) else: print('%s课程还未存在。'%les_choice) les_obj_list = modle.Lessons.get_all() cls_les_list = [] for i in les_obj_list: if i.name in les_list: cls_les_list.append(i) new_cls = modle.Classes(cls_name,cls_les_list) common.db_fileopen_write(settings.db_type[4],new_cls.id,new_cls) print('成功添加班级!') m = int(input('继续添加班级?(是:1;否:0)')) if m == 0: break def admin_main(): settings.condition = True print(settings.welcome1) while 1 : print(settings.admin_choice) m=int(input('请选择:')) if m == 1: create_tea() elif m == 2: create_les() elif m == 3: creat_class() elif m == 4: settings.condition = False break
学生视图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from confg import settings from lib import modle,common import re def student_zhuce(): while 1 : name= input('请输入您的姓名:') passwd1 = input('请输入您的密码:') n = 3 m = 0 while n>0: passwd2 = input('请再次输入您的密码:') if passwd1 == passwd2: xinxi = input('请输入您的基本信息:性别以及年龄,请以‘,’分隔') sex,age = re.split(r',',xinxi) new_student = modle.Student(name,passwd1,sex,age) common.db_fileopen_write(settings.db_type[3],new_student.id,new_student) m = 1 print('注册成功!') break else: print('输入错误') n -= 1 if m == 1: break def auth_stu(): if settings.condition is not False: print('您已登录') else: stu_name = input('请输入您的名字:') stu_passwd = input('请输入您的密码') obj = common.name_obj(settings.db_type[3],stu_name) if obj is not False: passwd = obj.passwd stu = common.athu(passwd,stu_passwd) if stu: print('登陆成功') settings.condition = True return obj else: print('登陆失败') return False else: print('登录失败') return False def stu_cls(stu): if stu.cls is None: print('现有以下班级:') cls_list = modle.Classes.get_name() for i in cls_list: print(i) choice_cls = '' while 1: choice_cls = input('请输入选择的班级') if choice_cls not in cls_list: print('输入错误,请重新输入') else: break cls = common.name_obj(settings.db_type[4],choice_cls) stu.cls = cls common.db_fileopen_write(settings.db_type[3],stu.id,stu) print('选择成功') else: print('您已经在%s班级中了'%stu.cls.name) def see_lesson(stu): print('您所要上的课程:') for i in stu.cls.lesson: print(i.name) while 1: see_les = input('请输入您想要查看的课程:(输入exit退出)') less_name_list = stu.cls.lesson_list() if see_les == 'exit': break elif see_les in less_name_list: see_les_obj = common.name_obj(settings.db_type[2],see_les) see_les_obj.introduction() break else: print('输入错误') def student_main(): while 1: print('作为学生,您有以下选择:%s'%settings.students_choice[1]) stu_choice = int(input('请输入您的选择:')) if stu_choice == 1: student_zhuce() elif stu_choice == 2: stu = auth_stu() if stu is not False: while 1: print('你有以下选择:%s'%settings.students_choice[2]) stu_choice2 = int(input('请选择:')) if stu_choice2 == 1: stu_cls(stu) elif stu_choice2 == 2: if stu.cls is not None: stu.cls.introduction() else: print('您还未选择班级') elif stu_choice2 == 3: if stu.cls is not None: see_lesson(stu) else: print('您还未选择班级,无法查看课程') elif stu_choice2 == 4: settings.condition = False break else: print('输入错误') elif stu_choice == 3: break else: print('输入错误')
主逻辑视图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from src import admin from src import student from confg import settings def main(): print(settings.welcome2) while 1: print(settings.main_choice) shengfen = int(input('您的身份是:')) if shengfen == 1: admin.admin_main() elif shengfen == 2: student.student_main()
三、运行效果
主界面
3.1 管理员视图
3.1.1 添加新老师
3.1.2 添加新课程
3.1.3 添加新班级
3.2 学生视图
3.2.1 学员注册
3.2.2 学员登录
3.2.3 选择班级
3.2.4 查看班级详情
3.2.5 查看课程情况
3.3 返回上一级