python 基础10 程序目录结构 学生选课系统面向对象练习
一 程序目录结构
1 bin 二进制文件 2 conf 配置文件 3 帮助文档 4 头文件库文件等
二 学生选课系统部分代码 未完待续
1 包内的__init__.py文件 在包下的脚本内 导入包内 的其他包下的文件也叫模块,在2.7下导入没有__init__.py 会报错。在3.5有没有这个文件以后没有影响了。 讲解: 1 老师类 : 需要有添加时间 加工资 减工资 2 管理员类: 封装管理员用户名密码
bin 目录
#!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'liujianzuo' import pickle, os, time, sys sys.path.append(os.path.dirname(os.path.dirname(__file__))) from lib.models import * from conf import settings def create_course(admin_obj): teacher_list = pickle.load(open(settings.teacher_db_dir,"rb")) for num,item in enumerate(teacher_list,1): print(num,item.name,item.age,item.create_time,item.create_admin.username) course_list = [] while True: name = input("请输入课程名:") if name == "q": break cost = input("请输入课时费:") num = input("选择老师(序号):") obj = Course(name,cost,teacher_list[int(num) - 1],admin_obj) course_list.append(obj) if os.path.exists(settings.course_db_dir): exists_list = pickle.load(open(settings.course_db_dir,"rb")) course_list.extend(exists_list) pickle.dump(course_list,open(settings.course_db_dir,"wb")) def create_teacher(admin_obj): teacher_list = [] while True: teacher_name = input("输入老师姓名(q退出):") if teacher_name == 'q': break teacher_age = input("输入老师年龄:") obj = Teacher(teacher_name,teacher_age,admin_obj) teacher_list.append(obj) if os.path.exists(settings.teacher_db_dir): exists_list = pickle.load(open(settings.teacher_db_dir,"rb")) teacher_list.extend(exists_list) pickle.dump(teacher_list,open(settings.teacher_db_dir,"wb")) def login(user, pwd): if os.path.exists(os.path.join(settings.Base_admin_dir,user)): admin_obj = pickle.load(open(os.path.join(settings.Base_admin_dir,user), "rb")) if admin_obj.login(user, pwd): print("登陆成功") while True: sel = input("1、创建老师 2、创建课程 (随意输入退出)") if sel == "1": create_teacher(admin_obj) elif sel == "2": create_course(admin_obj) else: break else: return 1 else: return 0 def register(user, pwd): admin_obj = Admin() admin_obj.register(user, pwd) def main(): inp = input("1、 登陆 2 、注册\n>>>") user = input("请输入用户名:") pwd = input("请输入密码:") if inp == "1": ret = login(user, pwd) if ret: print("登陆失败") elif ret == 0: print("用户不存在") elif inp == "2": register(user, pwd) if __name__ == '__main__': main()
#!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'liujianzuo' import pickle,os,sys sys.path.append(os.path.dirname(os.path.dirname(__file__))) from lib.models import * from conf import settings def course_info(student_obj): for item in student_obj.course_list: print(item.course_name, item.teacher.name) def select_course(student_obj): course_list = pickle.load(open(settings.course_db_dir,"rb")) for num,item in enumerate(course_list,1): print(num,item.course_name,item.cost,item.teacher.name) while True: num = input("请选择课程序号(q 离开):") if num == "q": break selected_course_obj = course_list[int(num) - 1] if selected_course_obj not in student_obj.course_list: student_obj.course_list.append(selected_course_obj) pickle.dump(student_obj,open(os.path.join(settings.Base_studens_dir,student_obj.username),"wb")) def login(user,pwd): if os.path.exists(os.path.join(settings.Base_studens_dir,user)): student_obj = pickle.load(open(os.path.join(settings.Base_studens_dir,user),"rb")) if student_obj.login(user,pwd): while True: inp = input("1. 选课 2.上课 3.查看课程信息 (其他退出)\n>>>") if inp == "1": select_course(student_obj) continue elif inp == "2": pass elif inp == "3": course_info(student_obj) continue else: break else: return 1 else: return 0 def register(user,pwd): obj = Student() obj.register(user,pwd) def main(): inp = input("1、 登陆 2 、注册\n>>>") user = input("请输入用户名:") pwd = input("请输入密码:") if inp == "1": ret = login(user, pwd) if ret: print("登陆失败") elif ret == 0: print("用户不存在") elif inp == "2": register(user, pwd) if __name__ == '__main__': main()
conf目录
#!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'liujianzuo' import os,sys BaseDir = os.path.dirname(os.path.dirname(__file__)) teacher_db_dir = os.path.join(BaseDir,"db","teacher_list") course_db_dir = os.path.join(BaseDir,"db","course_list") Base_admin_dir = os.path.join(BaseDir,"db","admin") Base_studens_dir = os.path.join(BaseDir,"db","students")
db目录
管理员目录 存放密码文件
学生目录 存放密码文件
lib 目录
#!/usr/bin/env python # _*_ coding:utf-8 _*_ __author__ = 'liujianzuo' import pickle, os, time from conf import settings class Teacher: """ 封装老师的相关信息 """ def __init__(self, name, age, admin): self.name = name self.age = age self.__assets = 0 self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') self.create_admin = admin # 封装的admin对象,里面有用户名 密码 def gain(self, cost): """ 增加资产 :param cost: 增加的数量 :return: """ self.__assets += cost def decrease(self, cost): """ 减少资产 :param cost: 减少的数量 :return: """ self.__assets += cost class Admin: def __init__(self): self.username = None self.password = None def login(self, user, pwd): """ 管理员登陆 :param user: :param pwd: :return: """ if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): """ 管理员注册 :param user: :param pwd: :return: """ self.username = user self.password = pwd path = os.path.join(settings.Base_admin_dir,self.username) pickle.dump(self, open(path, 'xb')) # 将 对象一起dump进去。 亮点。。 笨方法是在主函数里面dump class Course: """ 课程相关信息 """ def __init__(self, course_name, cost, teacher_obj, admin): self.course_name = course_name self.cost = cost self.teacher = teacher_obj self.create_time = time.strftime('%Y-%m-%d %H:%M:%S') self.create_admin = admin class Student: """ 学生相关信息 """ def __init__(self): self.username = None self.password = None self.course_list = [] self.study_dict = {} def select_course(self, course_obj): """ 学生选课 :param course_obj: :return: """ self.course_list.append(course_obj) def study(self, course_obj): """ 学生上课 :param course_obj: :return: """ class_result = course_obj.have_lesson() if course_obj in self.study_dict.keys(): self.study_dict[course_obj].append(class_result) else: self.study_dict[course_obj] = [class_result, ] def login(self, user, pwd): """ 学生登陆 :param user: :param pwd: :return: """ if self.username == user and self.password == pwd: return True else: return False def register(self, user, pwd): """ 学生注册 :param user: :param pwd: :return: """ self.username = user self.password = pwd path = os.path.join(settings.Base_studens_dir,self.username) pickle.dump(self, open(path, 'xb'))