下麵介紹一下自己寫的python程式,主要是的知識點為sys、os、json、pickle的模塊應用,python程式包的的使用,以及關於類的使用。 下麵是我的程式目錄: bin是存放一些執行文件course.py下麵是這個文件的內容: 這個文件是對os,sys模塊的應用; BASE_DIR = o ...
下麵介紹一下自己寫的python程式,主要是的知識點為sys、os、json、pickle的模塊應用,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,這裡也借鑒了很多網友的代碼,所以也非誠感謝這些人。