一个简单的python选课系统

时间:2021-07-24 01:17:11

  下面介绍一下自己写的python程序,主要是的知识点为sys、os、json、pickle的模块应用,python程序包的的使用,以及关于类的使用。

  下面是我的程序目录:

          一个简单的python选课系统

bin是存放一些执行文件course.py下面是这个文件的内容:

import sys,os

#程序主目录
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)

#添加环境变量
sys.path.append(BASE_DIR)

from core import main

if __name__ == '__main__':
    a =main.Run()
    a.interactive()

这个文件是对os,sys模块的应用;

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

  这里的os.path.abspath(__file__)是查看当前文件的所在位置。例如:

  D:\python项目\选课系统\course_system\bin\course.py

  这里的os.path.abspath是上一级目录例如:print(BASE_DIR)

  D:\python项目\选课系统\course_system

sys.path.append(BASE_DIR) 括号里面是模块引用的地址,我们可以理解把这个模块得知设置成环境变量。
from core import main 导入core包中的main文件

这里是core包中udi.py文件
import hashlib
import time

def create_md():
    m = hashlib.md5()
    m.update(bytes(str(time.time()),encoding="utf-8"))
    return m.hexdigest()

在python中hashlib提供了常见的摘要算法,如MD5,SHA1等等。它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)。为了给学生生成一个随机不重复的uid。

这里是core包中main.py文件内容

import sys,os         
import json      #这里说一下json这个模块,java之中也有应用,所以一些数据也可以给java去用。
import pickle     

from bin import course
from core import uid

#数据库文件路径
db_DIR = course.BASE_DIR + r"\db"
db_school = db_DIR + r"\school"
db_teacher = db_DIR + r"\teacher"
db_student = db_DIR + r"\student"
db_classes = db_DIR + r"\classes"
db_course = db_DIR + r"\course"
db_admin = db_DIR + r"\admin"
db_class_record = db_DIR + r"\class_record"
db_class_grade = db_DIR + r"\class_grade"

#基础类,主要包括文件的读写操作
class Baseclass(object):  #在python3中全部是新式类,但是建议还是这样写。在python2中存在经典类和新式类;例如:class Baseclass: 这种在python中是经典类,而class Baseclass(object):这样是新式类。有关它们的区别,我会在下一篇博客中具体说明。
    def __init__(self):
        pass
    def save(self,type,dict):
        filename = uid.create_md()
        dict['uid'] = filename
        file_path = "%s\%s" %(db_DIR,type)
        ab_file = "%s\%s" %(file_path,filename)
        if os.path.isdir(file_path):
            with open(ab_file,"wb") as f:
                f.write(pickle.dumps(dict))
                if True:
                    print(
                    "-------",type,"创建成功","-------")
                    for key in dict:
                        print(key,":\t",dict[key])
    def seek_list(self,type,list):
        filename = uid.create_md()
        file_path = "%s\%s" %(db_DIR,type)
        ab_file = "%s\%s" %(file_path,filename)
        if os.path.isdir(file_path):
            with open(ab_file,"wb") as f:
                f.write(pickle.dumps(list))
                if True:
                    print(
                    "-------",type,"创建成功","-------")
                    for i in list:
                        for key in i:
                            print(key,i[key])
                        print("\n")
        return True

    def open(self,type):
        all_data = []
        db_path = "%s\%s" %(db_DIR,type)
        for i in os.listdir(db_path):
            if os.path.isfile(os.path.join(db_path,i)):
                db_file = os.path.join(db_path,i)
                with open(db_file,"rb") as f:
                    file_dict = pickle.load(f)
                    all_data.append(file_dict)
        return all_data

#admin类
class Admin(Baseclass):
    def __init__(self):
        Baseclass.__init__(self)
    def create_school(self):
        school_dict = {}
        school_name = input("校名:")
        school_address = input("地址:")
        s1 = School(school_name, school_address)
        school_dict["校名"] = s1.school_name
        school_dict["地址"] = s1.school_address
        Baseclass.save(self, "school", school_dict)
    def create_teacher(self):
        teacher_dict = {}
        teacher_name = input("讲师姓名:")
        teacher_salary = input("讲师工资:")
        teacher_school = input("所属学校:")
        t1 = Teacher(teacher_name, teacher_salary, teacher_school)
        teacher_dict["姓名"] = t1.teacher_name
        teacher_dict["工资"] = t1.teacher_salary
        teacher_dict["所属学校"] = t1.teacher_school
        print(teacher_dict)
        Baseclass.save(self, "teacher", teacher_dict)
    def create_student(self):
        student_dict = {}
        student_name = input("学员姓名:")
        student_sex = input("学员性别:")
        student_school = input("所属学校:")
        student_classes = input("学员班级:")
        st1 = Student(student_name, student_sex, student_school, student_classes)
        student_dict["姓名"] = st1.student_name
        student_dict["性别"] = st1.student_sex
        student_dict["学校"] = st1.student_school
        student_dict["班级"] = st1.student_classes
        Baseclass.save(self, "student", student_dict)
    def create_course(self):
        course_dict = {}
        course_name = input("课程名:")
        course_period = input("周期:")
        course_prices = input("价格:")
        c1 = Course(course_name, course_period, course_prices)
        course_dict["课程名"] = c1.course_name
        course_dict["周期"] = c1.course_period
        course_dict["价格"] = course_prices
        Baseclass.save(self, "course", course_dict)
    def create_classes(self):
        classes_dict = {}
        classes_name = input("班级名:")
        classes_teachter = input("负责讲师:")
        classes_course = input("所学课程:")
        cs1 = Classes(classes_name, classes_teachter, classes_course)
        classes_dict["班级名"] = cs1.classes_name
        classes_dict["负责讲师"] = cs1.classes_teacher
        classes_dict["课程"] = cs1.classes_course
        Baseclass.save(self, "classes", classes_dict)

#School类
class School(Baseclass):
    def __init__(self,school_name,school_address):
        Baseclass.__init__(self)
        self.school_name = school_name
        self.school_address = school_address

#teacher类
class Teacher(Baseclass):
    def __init__(self,teacher_name,teacher_salary,teacher_school):
        Baseclass.__init__(self)
        self.teacher_name = teacher_name
        self.teacher_salary = teacher_salary
        self.teacher_school = teacher_school
    def create_class_record(self):
        class_record = []
        student_school = input("选择学校:")
        student_classes = input("选择班级:")
        student_times = input("课次:")
        student_list = Baseclass.open(self,"student")
        for i in student_list:
            if i["学校"] == student_school and i["班级"] == student_classes:
                student_name = i["姓名"]
                student_status = input("%s 上课情况:" % student_name)
                i["上课情况"] = student_status
                i["课次"] = student_times
                class_record.append(i)
        Baseclass.seek_list(self,"class_record",class_record)
    def create_class_grade(self):
        class_grade = []
        student_school = input("选择学校:")
        student_classes = input("选择班级:")
        student_times = input("课次:")
        student_list = Baseclass.open(self,"student")
        for i in student_list:
            if i["学校"] == student_school and i["班级"] == student_classes:
                student_name = i["姓名"]
                student_grade = input("%s 成绩:" % student_name)
                i["成绩"] = student_grade
                i["课次"] = student_times
                class_grade.append(i)
        Baseclass.seek_list(self,"class_grade",class_grade)
    def teacher_view_grade(self):
        grade_list = []
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        class_grade_list = Baseclass.open(self, "class_grade")
        for i in class_grade_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times:
                    grade_list.append(j)
        for i in grade_list:
            for key in i:
                print(key,i[key])
            print("\n")

    def tacher_view_record(self):
        record_list = []
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        class_record_list = Baseclass.open(self, "class_record")
        for i in class_record_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times:
                    record_list.append(j)
        for i in record_list:
            for key in i:
                print(key,i[key])
            print("\n")

#classes类
class Course(Baseclass):
    def __init__(self,course_name,course_period,course_prices):
        Baseclass.__init__(self)
        self.course_name = course_name
        self.course_period = course_period
        self.course_prices = course_prices

#student类
class Student(Baseclass):

    def __init__(self,student_name,student_sex,student_school,student_classes):
        Baseclass.__init__(self)
        self.student_name = student_name
        self.student_sex = student_sex
        self.student_school = student_school
        self.student_classes = student_classes
    def student_registered(self):
        student_dict = {}
        print("欢迎进入学生注册系统")
        student_name = input("注册姓名:")
        student_sex = input("性别:")
        student_school = input("学校:")
        student_class = input("班级:")
        st1 = Student(student_name,student_sex,student_school,student_class)
        student_dict["姓名"] = st1.student_name
        student_dict["性别"] = st1.student_sex
        student_dict["学校"] = st1.student_school
        student_dict["班级"] = st1.student_classes
        Baseclass.save(self, "student", student_dict)
    def student_pay_fees(self):
        pass
    def student_view_grade(self):
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        student_name = input("姓名:")
        class_grade_list = Baseclass.open(self,"class_grade")
        for i in class_grade_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \
                    and j["姓名"] == student_name:
                    for key in j:
                        print(key,j[key])
                    print("\n")
    def student_view_record(self):
        student_school = input("校名:")
        student_class = input("班级:")
        student_times = input("课次:")
        student_name = input("姓名:")
        class_record_list = Baseclass.open(self,"class_record")
        for i in class_record_list:
            for j in i:
                if j["学校"] == student_school and j["班级"] == student_class and j["课次"] == student_times \
                    and j["姓名"] == student_name:
                    for key in j:
                        print(key,j[key])
                    print("\n")

#课程类
class Classes(Baseclass):
    def __init__(self,classes_name,classes_teachter,classes_course):
        Baseclass.__init__(self)
        self.classes_name = classes_name
        self.classes_teacher = classes_teachter
        self.classes_course = classes_course

#管理员视图,继承Admin类
class Admin_view(Admin):
    def __init__(self):
        Admin.__init__(self)
    def auth(self,username,password):
        admin_file = "%s/%s.json" %(db_admin,username)
        if os.path.isfile(admin_file):
            with open(admin_file, 'r') as f:
                admin_data = json.load(f)
            if admin_data["name"] == username and admin_data["password"] == password:
                return True
            else:
                print("用户名或密码错误")
    def login(self):
        menu = u'''
        ------- 欢迎进入管理视图 ---------
            \033[32;1m 1.  校区管理
            2.  讲师管理
            3.  学员管理
            4.  课程管理
            5.  返回
            \033[0m'''
        menu_dic = {
            '1': Admin_view.school_manager,
            '2': Admin_view.teacher_manager,
            '3': Admin_view.student_manager,
            '4': Admin_view.course_manager,
            '5': "logout",
        }
        username = input("输入用户名:").strip()
        password = input("输入密码:").strip()
        auth = Admin_view.auth(self,username,password)
        if auth:
            exit_flag = False
            while not exit_flag:
                print(menu)
                option = input("请选择:").strip()
                if option in menu_dic:
                    if int(option) == 5:
                        exit_flag = True
                    else:
                        print(menu_dic[option])
                        menu_dic[option](self)
                else:
                    print("\033[31;1m输入错误,重新输入\033[0m")
    def school_manager(self):
        exit_flag = False
        while not exit_flag:
            print("""
                ------- 欢迎进入校区管理 ---------
                \033[32;1m1.  创建校区
                2.  创建班级
                3.  返回
                \033[0m
            """)
            option = input("请选择:").strip()
            if int(option) == 1:
                Admin.create_school(self)
            elif int(option) == 2:
                Admin.create_classes(self)
            else:
                exit_flag = True
    def teacher_manager(self):
        exit_flag = False
        while not exit_flag:
            print("""
                ------- 欢迎进入讲师管理 ---------
                \033[32;1m 1.  创建讲师
                2.  ...
                3.  返回
                \033[0m
            """)
            option = input("请选择:").strip()
            if int(option) == 1:
                Admin.create_teacher(self)
            elif int(option) == 2:
                print("扩展中")
            else:
                exit_flag = True

    def student_manager(self):
        exit_flag = False
        while not exit_flag:
            print("""
                ------- 欢迎进入学员管理 ---------
                \033[32;1m 1.  创建学员
                2.  ...
                3.  返回
                \033[0m
            """)
            option = input("请选择:").strip()
            if int(option) == 1:
                Admin.create_student(self)
            elif int(option) == 2:
                print("扩展中")
            else:
                exit_flag = True
    def course_manager(self):
        exit_flag = False
        while not exit_flag:
            print("""
                ------- 欢迎进入课程管理 ---------
                \033[32;1m 1.  创建课程
                2.  ...
                3.  返回
                \033[0m
            """)
            option = input("请选择:").strip()
            if int(option) == 1:
                Admin.create_course(self)
            elif int(option) == 2:
                print("扩展中")
            else:
                exit_flag = True

#讲师视图类,继承Teacher类
class Teacher_view(Teacher,):
    def __init__(self,teacher_name,teacher_salary,teacher_school):
        Teacher.__init__(self,teacher_name,teacher_salary,teacher_school)
    def login(self):
        menu = u'''
        ------- 欢迎进入讲师视图 ---------
            \033[32;1m  1.  创建上课记录
            2.  创建学员成绩
            3.  查看学员上课记录
            4.  查看学员成绩
            5.  返回
            \033[0m'''
        menu_dic = {
            '1': Teacher.create_class_record,
            '2': Teacher.create_class_grade,
            '3': Teacher.tacher_view_record,
            '4': Teacher.teacher_view_grade,
            '5': "logout",
        }
        if True:
            exit_flag = False
            while not exit_flag:
                print(menu)
                option = input("请选择:").strip()
                if option in menu_dic:
                    if int(option) == 5:
                        exit_flag = True
                    else:
                        print(menu_dic[option])
                        menu_dic[option](self)
                else:
                    print("\033[31;1m输入错误,重新输入\033[0m")

#学员视图类,继承Student类
class Student_view(Student):
    def __init__(self,student_name,student_sex,student_school,student_classes):
        Student.__init__(self,student_name,student_sex,student_school,student_classes)

    def login(self):
        menu = u'''
        ------- 欢迎进入学生管理视图 ---------
        \033[32;1m 1.  注册
        2.  交学费
        3.  查看上课记录
        4.  查看作业成绩
        5.  返回
        \033[0m'''
        menu_dic = {
            '1': Student.student_registered,
            '2': Student.student_pay_fees,
            '3': Student.student_view_record,
            '4': Student.student_view_grade,
            '5': "logout",
        }
        if True:
            exit_flag = False
            while not exit_flag:
                print(menu)
                option = input("请选择:").strip()
                if option in menu_dic:
                    if int(option) == 5:
                        exit_flag = True
                    else:
                        menu_dic[option](self)
                else:
                    print("\033[31;1m输入错误,重新输入\033[0m")

#程序交互类
class Run(object):
    def __init__(self):
        pass
    def interactive(self):
        menu = u'''
        ------- 欢迎进入选课系统 ---------
        \033[32;1m 1.  学生视图
        2.  讲师视图
        3.  管理视图
        4.  退出
        \033[0m'''
        menu_dic = {
            '1': Student_view,
            '2': Teacher_view,
            '3': Admin_view,
            # '4': logout,
        }
        exit_flag = False
        while not exit_flag:
            print(menu)
            option_view = input("请选择视图:").strip()
            if option_view in menu_dic:
                if int(option_view) == 4:
                    exit_flag = True
                else:
                    menu_dic[option_view].login(self)
            else:
                print("\033[31;1m输入错误,重新输入\033[0m")

一个简单的python选课系统

写程序的的时候最好先把一个框架先写出来,最后再往里边填写东西。

这是程序执行效果

一个简单的python选课系统

不在过多演示了

因为毕竟是刚刚学习python,这里也借鉴了很多网友的代码,所以也非诚感谢这些人。



一个简单的python选课系统

一个简单的python选课系统的更多相关文章

  1. 一个简单的python爬虫程序

    python|网络爬虫 概述 这是一个简单的python爬虫程序,仅用作技术学习与交流,主要是通过一个简单的实际案例来对网络爬虫有个基础的认识. 什么是网络爬虫 简单的讲,网络爬虫就是模拟人访问web ...

  2. 作业1开发一个简单的python计算器

    开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...

  3. 老男孩python作业5-开发一个简单的python计算器

    开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...

  4. 【译】建立属于你的个人高效系统——效率专家 Mike Vardy 教你如何设置一个简单的个人高效系统

    原文:http://mux.baidu.com/?p=5300 百度MUX 已经有太多的高效系统供人使用,而对于那些刚刚开始,想寻求更好方法完成他们任务,项目,目标的人来说,要做一个高效系统却是相当艰 ...

  5. 一个简单的python爬虫,爬取知乎

    一个简单的python爬虫,爬取知乎 主要实现 爬取一个收藏夹 里 所有问题答案下的 图片 文字信息暂未收录,可自行实现,比图片更简单 具体代码里有详细注释,请自行阅读 项目源码: # -*- cod ...

  6. python选课系统

    程序名称: 选课系统 角色:学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海 ...

  7. 在Openfire上弄一个简单的推送系统

    推送系统 说是推送系统有点大,其实就是一个消息广播功能吧.作用其实也就是由服务端接收到消息然后推送到订阅的客户端. 思路 对于推送最关键的是服务端向客户端发送数据,客户端向服务端订阅自己想要的消息.这 ...

  8. 从零构建一个简单的 Python Web框架

    为什么你想要自己构建一个 web 框架呢?我想,原因有以下几点: 你有一个新奇的想法,觉得将会取代其他的框架 你想要获得一些名气 你遇到的问题很独特,以至于现有的框架不太合适 你对 web 框架是如何 ...

  9. 一个简单的python线程池框架

    初学python,实现了一个简单的线程池框架,线程池中除Wokers(工作线程)外,还单独创建了一个日志线程,用于日志的输出.线程间采用Queue方式进行通信. 代码如下:(不足之处,还请高手指正) ...

随机推荐

  1. XPath函数——字符串函数(转载)

    本文是转载的,原文网址:http://www.cnblogs.com/zhaozhan/archive/2010/01/17/1650242.html 字符串函数主要用来处理字符串.字符串函数主要包括 ...

  2. nodeJS接受post传过来的参数

    1.nodeJs接受Post传递的参数需要通过绑定两个事件来获取, querystring = require("querystring");  1 app.post('/comm ...

  3. Minimum Adjustment Cost

    Given an integer array, adjust each integers so that the difference of every adjacent integers are n ...

  4. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  5. centos6.4 网络适配器设置仅主机模式

    网络适配器设置仅主机模式时: 1.vmnet1网卡必须开启

  6. JavaScript Window Screen

    window.screen 对象包含有关用户屏幕的信息. Window Screen window.screen对象在编写时可以不使用 window 这个前缀. 一些属性: screen.availW ...

  7. 秒懂JS对象、构造器函数和原型对象之间的关系

    学习JS的过程中,想要掌握面向对象的程序设计风格,对象模型(原型和继承)是其中的重点和难点,拜读了各类经典书籍和各位前辈的技术文章,感觉都太过高深,花费了不少时间才搞明白(个人智商是硬伤/(ㄒoㄒ)/ ...

  8. LOJ 2409「THUPC 2017」小 L 的计算题 / Sum

    思路 和玩游戏一题类似 定义\(A_k(x)=\sum_{i=0}^\infty a_k^ix^i=\frac{1}{1-a_kx}\) 用\(\ln 'x\)代替\(\frac{1}{x}\), 所 ...

  9. SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...

  10. IIS7 禁止目录运行脚本

    早晨看博客说有人被黑了:http://www.cnblogs.com/sanshi/p/3150639.html 看回复建议禁用上传目录的脚本运行权限 在IIS6上还是比较容易的,直接右键--属性,把 ...