功能代碼實現源地址:https://www.cnblogs.com/lianzhilei/p/5832691.html 若有侵權,當即刪除
本文主要是分析 選課系統 實現思路及上面代碼的實現過程,感謝python前輩分享出源碼。html
首先,寫出本身的實現思路:python
python初學者,自學到面向對象,有如下做業要求須要實現:linux
角色:學校、學員、課程、講師 要求: 1. 建立北京、上海 2 所學校 2. 建立linux , python , go 3個課程 , linux\py 在北京開, go 在上海開 3. 課程包含,週期,價格,經過學校建立課程 4. 經過學校建立班級, 班級關聯課程、講師 5. 建立學員時,選擇學校,關聯班級 6. 建立講師角色時要關聯學校, 7. 提供兩個角色接口 8. 學員視圖, 能夠註冊, 交學費, 選擇班級, 9. 講師視圖, 講師可管理本身的班級, 上課時選擇班級, 查看班級學員列表 , 修改所管理的學員的成績 10. 管理視圖,建立講師, 建立班級,建立課程 11. 上面的操做產生的數據都經過pickle序列化保存到文件裏
最初拿到這個做業真的是無從下手,只能經過一句一句的返回斟酌,由於是學過了python的面向對象,首先分析上面那些能夠做爲類:數據結構
學校類、課程類、講師類、學生類 class School: pass class Course: pass class Teacher: pass class Student: pass
1. 建立北京、上海 2 所學校ide
經過實例化能夠建立這兩所學校 sh = School(xxx) bj = School(xxx)
2. 建立linux , python , go 3個課程 , linux\py 在北京開, go 在上海開函數
三個課程,咱們能夠經過實例化課程類實現 linux = Course(xxx) python = Course(xxx) go = Course(xxx)
linux\py 在北京開, go 在上海開 這就說明 School 類下有course這個屬性,所以 School 修改成:學習
class School: def __init__(name, addr, course): self.name = name # 學校名 self.addr = addr # 學校地址 self.course = course # 學校裏的課程 class Course: def __init__(name, price, time): self.name = name self.price = price self.time = time course = Course(name, price, time) # 實例化課程類 school = School('上海', '上海市', course) # 類的組合
3. 課程包含,週期,價格,經過學校建立課程spa
到第三點要求的時候,上面的代碼得改寫了,以下: class School: def __init__(name, addr, course): self.name = name # 學校名 self.addr = addr # 學校地址 def create_course(self): # 經過學校建立課程 pass class Course: def __init__(name, price, time): self.name = name self.price = price self.time = time
4. 經過學校建立班級, 班級關聯課程、講師code
到第四點又多了一個班級的類,這個類裏包含哪些屬性呢? 班級名、班級上課的講師、班級要上的課程、班級裏的學生 class School: def __init__(name, addr, course): self.name = name # 學校名 self.addr = addr # 學校地址 def create_course(self): # 經過學校建立課程 pass class Course: def __init__(name, price, time): self.name = name self.price = price self.time = time class Grade: def __init__(name, teacher, course, student): self.name = name self.teacher = teacher self.course = course self.student = student
做爲一個初學者,後面的需求就不知道怎麼去實現了。因而去參考了下python前輩的代碼:orm
源碼地址:功能代碼實現源地址:https://www.cnblogs.com/lianzhilei/p/5832691.html 若有侵權,當即刪除
擼了N邊代碼,整理下思路,數據關係結構以下:
{school:{course:{'teacher': teacher, 'grade': grade}}}
{teacher:{'grade':grade}}
有了這個數據關係,基本就能明白做者的實現思路。
舉個栗子:
main_db = {school:{course:{'teacher': teacher, 'grade': grade}}} school = School('上海') 上海學院下面的課程信息: course = main_db[school] 上海學院python課程下面的老師和班級信息: python = Course('python') info = main_db[school][python]
不知道經過上面的栗子能不能稍微領悟一點,通讀做者代碼和上面的做業要求,能夠分爲三大部分。
學校中心
接下來,不用看代碼,試試本身來實現下:
做業要求是經過 pickle 序列化到本地文件
首先有三個視圖:
main_list = ['學校中心', '講師中心', '學生中心', '退出']
先來實現第一個視圖:學校中心
第一步,實現三大視圖的循環選擇
import pickle def options(li): for i, k in enumerate(li): print(i+1, k) choice = input('>>>').strip() return choice def start(): while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break if __name__ == '__main__': main_list = ['學校中心', '講師中心', '學生中心', '退出'] start()
根據流程圖, 第二步咱們須要選擇學校名,而咱們並無任何數據信息,這裏就須要先初始化學校信息:
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] start()
經過初始化,已經將數據結構肯定下來了。
__db_main = {sh: {}, bj: {}} __db_teacher = {}
能夠查看當前程序的目錄下,已經生成兩個pickle序列化的文件,下一步,咱們就能夠經過讀取本地的文件,將初始化的數據讀取出來,進行學校的選擇:
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() if not isinstance(key, str): dict_info[key.name] = key return dict_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main') school_name = input('\033[34;1m選擇學校名:\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] start()
上面的代碼中,School 類中增長了一個cat_school 查看學校屬性的方法,最主要的是增長了一個 information 函數,這個函數必定要理解:
咱們在初始化時:
sh = School('上海', '上海市') bj = School('北京', '北京市')
經過pickle讀取出來的類型以下:
main_db = {sh: {}, bj: {}}
sh、bj 都是School實例化後的對象,這裏就須要經過 information 來作轉換
def information(db, mode): dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() if not isinstance(key, str): dict_info[key.name] = key return dict_info
當咱們執行:
information(main_db, 'main')
1. 首先 key 就是school對象,經過對象.方法查看school類的方法cat_school()
2. if not isinstance(key, str): 當 key 不是字符串類型,這裏 key 是實例化後的類型,並非字符串類型,所以返回的是 {'北京':bj, '上海':sh}
經過上面的步驟,咱們已經能夠進行選擇學校,來到了學校內部,能夠 ['建立課程', '招聘講師', '建立班級']
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() if not isinstance(key, str): dict_info[key.name] = key return dict_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main') school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] # 經過學校名獲取到了該學校名的類 while Flag: choice = options(school_list) # 循環打印學校中心下的功能點 if choice == '1': # 建立課程 pass elif choice == '2': # 招聘講師 pass elif choice == '3': # 建立班級 pass elif choice == '4': # 返回 Flag = False # 退出到主界面 else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] start()
上面的代碼:
1. 經過用戶選擇的學校名獲取到了帶學校名屬性的類;
2. 使用Flag = True 循環,當要退出時候,直接能夠退出到主界面,這裏能夠揣摩下,這種用法之後確定會遇到。
接下來,就須要實現建立課程的功能點。
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() if not isinstance(key, str): dict_info[key.name] = key return dict_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main') school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course') print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break elif choice == '2': # 招聘講師 pass elif choice == '3': # 建立班級 pass elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] start()
1. 首先建立課程類和查看課程的方法;
2. 經過information函數執行查看課程的方法並返回{'課程名': 課程類},當沒有課程的時候,必定要返回字典類型,由於後面須要作課程名是否存在的判斷;
3. 在school類中添加新增課程的方法,由於經過要求得知,課程是經過學校建立的
4. 首先展現已經存在的課程信息,並獲得{'課程名': 課程類},再次經過用戶輸入獲得課程信息,經過用戶輸入信息實例化課程類,最後將實例化對象寫入學校類的方法中
到此,新增課程已經完成。接下來是招聘講師,其實思路和上面大同小異。
首先,經過上面分析過做者的數據類型:
main_db = {school:{course:{'講師名': 講師類, '班級名': 班級類}}}
當用戶輸入講師名的時候,須要判斷該講師是否已經存在,所以咱們須要獲得 {'講師名': 講師類}
s1 = set() # 這裏須要定義一個set類型,由於不管數據存不存在,後續都須要進行判斷 for key in main_db[school][course]: main_db[school][course][key].cat_teacher() # 這樣就能執行查看講師的方法 s1.add(key) return s1
經過上面的僞代碼,咱們執行了查看講師的方法,並返回了已經存在講師的名字。
1. 建立講師類和查看講師的方法;
2. 經過information函數執行查看講師的方法並返回,當沒有講師的時候,必定要返回set()類型,由於後面須要作講師名是否存在的判斷;
3. 在school類中添加招聘講師的方法;
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) def hire_teacher(self, main_db, course, teacher, main_file): main_db[self][course] = {'teacher': teacher} file_oper(main_file, 'wb', main_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) class Teacher(object): def __init__(self, name, age, school, course): self.name = name self.age = age self.school = school self.course = course def cat_teacher(self): print('\033[32;1m課程【%s】\t講師【%s】\033[0m' % (self.course, self.name)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): set_info = set() dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() elif mode == 'teacher': db[key].cat_teacher() set_info.add(key) if not isinstance(key, str): dict_info[key.name] = key return dict_info, set_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course')[0] print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:\033[0m').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '2': # 招聘講師 while True: print('\033[33;1m目前學校【%s】已經存在的講師信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] res_teacher = information(main_db[school][k], 'teacher')[1] if not res_teacher: print('\033[31;1m課程【%s】\t講師【None】\033[0m' % k.name) if_cont = input('\033[34;1m是否招聘講師[y/b]:\033[0m').strip() if if_cont == 'y': teacher_name = input('\033[34;1m輸入講師名:').strip() teacher_age = input('\033[34;1m輸入講師年齡:').strip() course_name = input('\033[34;1m輸入課程名:').strip() if course_name in res_course: course = res_course[course_name] if teacher_name not in res_teacher: teacher = Teacher(teacher_name, teacher_age, school_name, course_name) school.hire_teacher(main_db, course, teacher, __db_main) else: print('\033[31;1m該講師名已存在.\033[0m') else: print('\033[31;1m該課程名不存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '3': # 建立班級 pass elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] start()
建立班級功能,和招聘講師實現思路徹底一致。
1. 建立班級類和查看班級的方法;
2. 經過information函數執行查看班級的方法並返回,當沒有班級的時候,必定要返回set()類型,由於後面須要作班級名是否存在的判斷;
3. 在school類中添加班級的方法;
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) def hire_teacher(self, main_db, course, teacher, main_file): main_db[self][course] = {'teacher': teacher} file_oper(main_file, 'wb', main_db) def create_grade(self, main_db, teacher_db, course, grade, teacher, main_file, teacher_file): main_db[self][course]['grade'] = grade file_oper(main_file, 'wb', main_db) teacher_db[teacher] = {'grade': grade} file_oper(teacher_file, 'wb', teacher_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) class Teacher(object): def __init__(self, name, age, school, course): self.name = name self.age = age self.school = school self.course = course def cat_teacher(self): print('\033[32;1m課程【%s】\t講師【%s】\033[0m' % (self.course, self.name)) class Grade(object): def __init__(self, name, course, teacher): self.name = name self.course = course self.teacher = teacher def cat_grade(self): print('\033[32;1m課程【%s】\t班級【%s】\033[0m' % (self.course, self.name)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): set_info = set() dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() elif mode == 'teacher' and key == 'teacher': db[key].cat_teacher() set_info.add(key) elif mode == 'grade' and key == 'grade': db[key].cat_grade() set_info.add(key) if not isinstance(key, str): dict_info[key.name] = key return dict_info, set_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course')[0] print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:\033[0m').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '2': # 招聘講師 while True: print('\033[33;1m目前學校【%s】已經存在的講師信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] res_teacher = information(main_db[school][k], 'teacher')[1] print(main_db) if not res_teacher: print('\033[31;1m課程【%s】\t講師【None】\033[0m' % k.name) if_cont = input('\033[34;1m是否招聘講師[y/b]:\033[0m').strip() if if_cont == 'y': teacher_name = input('\033[34;1m輸入講師名:').strip() teacher_age = input('\033[34;1m輸入講師年齡:').strip() course_name = input('\033[34;1m輸入課程名:').strip() if course_name in res_course: course = res_course[course_name] if teacher_name not in res_teacher: teacher = Teacher(teacher_name, teacher_age, school_name, course_name) school.hire_teacher(main_db, course, teacher, __db_main) else: print('\033[31;1m該講師名已存在.\033[0m') else: print('\033[31;1m該課程名不存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '3': # 建立班級 while True: teacher_db = file_oper(__db_teacher, 'rb') print('\033[33;1m目前學校【%s】已經存在的班級信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] print('k:', k) print(main_db[school][k]) print('--------------------------') res_grade = information(main_db[school][k], 'grade')[1] if not res_grade: print('\033[31;1m目前沒有任何班級信息.\033[0m') else: print('\033[31;1m目前沒有任何課程信息, 請先建立課程.\033[0m') if_cont = input('\033[34;1m是否建立班級[y/b]:\033[0m').strip() if if_cont == 'y': grade_name = input('\033[34;1m輸入班級名:\033[0m').strip() course_name = input('\033[34;1m輸入班級要上的課程:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course]: teacher = main_db[school][course]['teacher'] if grade_name not in res_grade: grade = Grade(grade_name, course_name, teacher.name) print('grade:', grade) school.create_grade(main_db, teacher_db, course, grade, teacher, __db_main, __db_teacher) else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程名不存在,請從新輸入.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] start()
招聘講師和建立班級思路是同樣的,只是多了下面的步驟:
1. 班級類包含:班級、課程、講師 信息
2. 這裏就引入了第二個存儲文件 __db_teacher 必定要知道數據類型的存儲格式:
{講師類:{'班級名':班級類}}
3. 必須取得講師類才能進行第2步的操做
獲取講師信息
res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] # 這裏就拿到了課程類 res_grade = information(main_db[school][k], 'grade')[1] # 這裏就能夠拿到已經存在或者爲空的班級信息
當該班級的課程名被接收後
course_name = input('\033[34;1m輸入班級要上的課程:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course]: teacher = main_db[school][course]['teacher']
上面的代碼就能拿到講師類,數據結構定義下來,代碼就很好寫了。
到目前爲止,根據要求,學校中心的全部功能都已經實現了。
學校中心功能全部代碼:
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) def hire_teacher(self, main_db, course, teacher, main_file): main_db[self][course] = {'teacher': teacher} file_oper(main_file, 'wb', main_db) def create_grade(self, main_db, teacher_db, course, grade, teacher, main_file, teacher_file): main_db[self][course]['grade'] = grade file_oper(main_file, 'wb', main_db) teacher_db[teacher] = {'grade': grade} file_oper(teacher_file, 'wb', teacher_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) class Teacher(object): def __init__(self, name, age, school, course): self.name = name self.age = age self.school = school self.course = course def cat_teacher(self): print('\033[32;1m課程【%s】\t講師【%s】\033[0m' % (self.course, self.name)) class Grade(object): def __init__(self, name, course, teacher): self.name = name self.course = course self.teacher = teacher def cat_grade(self): print('\033[32;1m課程【%s】\t班級【%s】\033[0m' % (self.course, self.name)) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): set_info = set() dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() elif mode == 'teacher' and key == 'teacher': db[key].cat_teacher() set_info.add(key) elif mode == 'grade' and key == 'grade': db[key].cat_grade() set_info.add(key) if not isinstance(key, str): dict_info[key.name] = key return dict_info, set_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course')[0] print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:\033[0m').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '2': # 招聘講師 while True: print('\033[33;1m目前學校【%s】已經存在的講師信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] res_teacher = information(main_db[school][k], 'teacher')[1] print(main_db) if not res_teacher: print('\033[31;1m課程【%s】\t講師【None】\033[0m' % k.name) if_cont = input('\033[34;1m是否招聘講師[y/b]:\033[0m').strip() if if_cont == 'y': teacher_name = input('\033[34;1m輸入講師名:').strip() teacher_age = input('\033[34;1m輸入講師年齡:').strip() course_name = input('\033[34;1m輸入課程名:').strip() if course_name in res_course: course = res_course[course_name] if teacher_name not in res_teacher: teacher = Teacher(teacher_name, teacher_age, school_name, course_name) school.hire_teacher(main_db, course, teacher, __db_main) else: print('\033[31;1m該講師名已存在.\033[0m') else: print('\033[31;1m該課程名不存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '3': # 建立班級 while True: teacher_db = file_oper(__db_teacher, 'rb') print('\033[33;1m目前學校【%s】已經存在的班級信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] print('k:', k) print(main_db[school][k]) print('--------------------------') res_grade = information(main_db[school][k], 'grade')[1] if not res_grade: print('\033[31;1m目前沒有任何班級信息.\033[0m') else: print('\033[31;1m目前沒有任何課程信息, 請先建立課程.\033[0m') if_cont = input('\033[34;1m是否建立班級[y/b]:\033[0m').strip() if if_cont == 'y': grade_name = input('\033[34;1m輸入班級名:\033[0m').strip() course_name = input('\033[34;1m輸入班級要上的課程:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course]: teacher = main_db[school][course]['teacher'] if grade_name not in res_grade: grade = Grade(grade_name, course_name, teacher.name) print('grade:', grade) school.create_grade(main_db, teacher_db, course, grade, teacher, __db_main, __db_teacher) else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程名不存在,請從新輸入.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] start()
學生中心
功能點:學員註冊,返回
學員註冊這裏,是使用了一個Grade類中的student屬性來保存的。
self.student = set([]) # 該屬性是一個set集合
在Grade類中實現一個新增學員的方法:
def add_student(self, teacher_db, teacher, student_name, teacher_file): self.student.add(student_name) # 將學員名添加到 self.student屬性中 teacher_db[teacher] = {'grade': self} # 從新給講師賦值,因此這裏必需要拿到講師類 file_oper(teacher_file, 'wb', teacher_db) # 寫入文件
以上的操做,就能添加一個新的學員到班級。
新增學員的操做流程:
輸入用戶名 --> 選擇已有學校 --> 選擇已有課程 --> 寫入文件
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) def hire_teacher(self, main_db, course, teacher, main_file): main_db[self][course] = {'teacher': teacher} file_oper(main_file, 'wb', main_db) def create_grade(self, main_db, teacher_db, course, grade, teacher, main_file, teacher_file): main_db[self][course]['grade'] = grade file_oper(main_file, 'wb', main_db) teacher_db[teacher] = {'grade': grade} file_oper(teacher_file, 'wb', teacher_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) class Teacher(object): def __init__(self, name, age, school, course): self.name = name self.age = age self.school = school self.course = course def cat_teacher(self): print('\033[32;1m課程【%s】\t講師【%s】\033[0m' % (self.course, self.name)) class Grade(object): def __init__(self, name, course, teacher): self.name = name self.course = course self.teacher = teacher self.student = set([]) def cat_grade(self): print('\033[32;1m課程【%s】\t班級【%s】\033[0m' % (self.course, self.name)) def add_student(self, teacher_db, teacher, student_name, teacher_file): self.student.add(student_name) teacher_db[teacher] = {'grade': self} file_oper(teacher_file, 'wb', teacher_db) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): set_info = set() dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() elif mode == 'teacher' and key == 'teacher': db[key].cat_teacher() set_info.add(key) elif mode == 'grade' and key == 'grade': db[key].cat_grade() set_info.add(key) if not isinstance(key, str): dict_info[key.name] = key return dict_info, set_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course')[0] print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:\033[0m').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '2': # 招聘講師 while True: print('\033[33;1m目前學校【%s】已經存在的講師信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] res_teacher = information(main_db[school][k], 'teacher')[1] print(main_db) if not res_teacher: print('\033[31;1m課程【%s】\t講師【None】\033[0m' % k.name) if_cont = input('\033[34;1m是否招聘講師[y/b]:\033[0m').strip() if if_cont == 'y': teacher_name = input('\033[34;1m輸入講師名:').strip() teacher_age = input('\033[34;1m輸入講師年齡:').strip() course_name = input('\033[34;1m輸入課程名:').strip() if course_name in res_course: course = res_course[course_name] if teacher_name not in res_teacher: teacher = Teacher(teacher_name, teacher_age, school_name, course_name) school.hire_teacher(main_db, course, teacher, __db_main) else: print('\033[31;1m該講師名已存在.\033[0m') else: print('\033[31;1m該課程名不存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '3': # 建立班級 while True: teacher_db = file_oper(__db_teacher, 'rb') print('\033[33;1m目前學校【%s】已經存在的班級信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] print('k:', k) print(main_db[school][k]) print('--------------------------') res_grade = information(main_db[school][k], 'grade')[1] if not res_grade: print('\033[31;1m目前沒有任何班級信息.\033[0m') else: print('\033[31;1m目前沒有任何課程信息, 請先建立課程.\033[0m') if_cont = input('\033[34;1m是否建立班級[y/b]:\033[0m').strip() if if_cont == 'y': grade_name = input('\033[34;1m輸入班級名:\033[0m').strip() course_name = input('\033[34;1m輸入班級要上的課程:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course]: teacher = main_db[school][course]['teacher'] if grade_name not in res_grade: grade = Grade(grade_name, course_name, teacher.name) print('grade:', grade) school.create_grade(main_db, teacher_db, course, grade, teacher, __db_main, __db_teacher) else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程名不存在,請從新輸入.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def student_center(): while True: choice = options(student_list) main_db = file_oper(__db_main, 'rb') teacher_db = file_oper(__db_teacher, 'rb') if choice == '1': student_name = input('\033[34;1m輸入學生名:\033[0m').strip() res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m輸入學校名:\033[0m').strip() if school_name in res_school: school = res_school[school_name] res_course = information(main_db[school], 'course')[0] course_name = input('\033[34;1m輸入課程名:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course].get('grade'): for i in teacher_db: i.name = course.name teacher = i grade = teacher_db[teacher].get('grade') print('\033[33;1m課程【%s】的費用爲【%s元】\033[0m' % (course_name, course.price)) if_pay = input('\033[34;1m是否支付當前費用(y支付):\033[0m').strip() if if_pay == 'y': grade.student.add(student_name) grade.add_student(teacher_db, teacher, student_name, __db_teacher) print('\033[32;1m選課成功!\033[0m') any = input('輸入任意鍵退出當前:') else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程不存在,請從新選擇.\033[0m') else: print('\033[31;1m學校不存在,請從新選擇.\033[0m') elif choice == '2': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') elif choice == '3': print('\033[33;1m【學生中心】\033[0m') student_center() elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] student_list = ['學員註冊', '返回'] start()
上面代碼的難點在經過講師把兩個字典聯繫起來取值:
main_db = {學校類:{課程類:{'講師名': 講師類, '班級名': 班級類}}} teacher_db = {講師類:{'班級名': 班級類}} if main_db[school][course].get('grade'): # 使用get判斷班級是否存在,不存在返回None不會報錯 for i in teacher_db: # 這裏循環取講師類 i.course = course.name # 當teacher_db中講師類課程的名字和main_db中課程的名字一致時,講師和課程就關聯起來了 teacher = i # 直接獲取講師類
上面這裏是整篇代碼最難理解地方,須要從全面代碼數據結構去理解。
簡單來講,經過學員選擇的課程肯定課程類,在從課程類肯定講師類,就是這樣的關聯關係。
目前,學校中心和學員中心都已經實現了,學校中心和學員中心都有查詢和寫入的操做,因此提早完成,接下就是講師中心,只有查詢的功能
講師中心
功能點:查看班級信息,查看學員列表信息
實現思路:
每一個講師都有本身的班級信息和學員列表,所以在實現功能點以前須要進行確認講師身份
查看班級信息中須要:學校和班級
學員列表是保存在Grade類student屬性中
import os, pickle BASE_DIR = os.path.dirname(os.path.abspath(__file__)) __db_main = os.path.join(BASE_DIR, 'main_dict') # 學校內容文件 __db_teacher = os.path.join(BASE_DIR, 'teacher_dict') # 講師課程內容文件 class School(object): '''學校類''' def __init__(self, name, addr): self.name = name self.addr = addr def cat_school(self): print('\033[32;1m學校【%s】\t地址【%s】\033[0m' % (self.name, self.addr)) def create_course(self, main_db, course, main_file): main_db[self][course] = {} file_oper(main_file, 'wb', main_db) def hire_teacher(self, main_db, course, teacher, main_file): main_db[self][course] = {'teacher': teacher} file_oper(main_file, 'wb', main_db) def create_grade(self, main_db, teacher_db, course, grade, teacher, main_file, teacher_file): main_db[self][course]['grade'] = grade file_oper(main_file, 'wb', main_db) teacher_db[teacher] = {'grade': grade} file_oper(teacher_file, 'wb', teacher_db) class Course(object): def __init__(self, name, price, time): self.name = name self.price = price self.time = time def cat_course(self): print('\033[32;1m課程【%s】\t價格【%s元】\t週期【%s個月】\033[0m' % (self.name, self.price, self.time)) class Teacher(object): def __init__(self, name, age, school, course): self.name = name self.age = age self.school = school self.course = course def cat_teacher(self): print('\033[32;1m課程【%s】\t講師【%s】\033[0m' % (self.course, self.name)) class Grade(object): def __init__(self, name, course, teacher): self.name = name self.course = course self.teacher = teacher self.student = set([]) def cat_grade(self): print('\033[32;1m課程【%s】\t班級【%s】\033[0m' % (self.course, self.name)) def add_student(self, teacher_db, teacher, student_name, teacher_file): self.student.add(student_name) teacher_db[teacher] = {'grade': self} file_oper(teacher_file, 'wb', teacher_db) def options(li): '''序號和標題循環打印,返回選擇序號''' for i, k in enumerate(li): print(i + 1, k) choice = input('>>>').strip() return choice def information(db, mode): set_info = set() dict_info = {} if db: for key in db: if mode == 'main': key.cat_school() elif mode == 'course': key.cat_course() elif mode == 'teacher' and key == 'teacher': db[key].cat_teacher() set_info.add(key) elif mode == 'grade' and key == 'grade': db[key].cat_grade() set_info.add(key) if not isinstance(key, str): dict_info[key.name] = key return dict_info, set_info def school_center(): Flag = True while Flag: main_db = file_oper(__db_main, 'rb') res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m選擇學校名:\033[0m') if school_name in res_school: school = res_school[school_name] while Flag: choice = options(school_list) if choice == '1': # 建立課程 while True: print('\033[33;1m目前學校【%s】已經存在的課程信息:\033[0m' % school_name) res_course = information(main_db[school], 'course')[0] print(type(res_course)) if not res_course: print('\033[31;1m目前沒有任何課程信息.\033[0m') if_cont = input('\033[34;1m是否建立課程[y/b]:\033[0m').strip() if if_cont == 'y': course_name = input('\033[34;1m輸入課程名:').strip() if course_name not in res_course: course_price = input('\033[34;1m輸入課程價格:').strip() course_time = input('\033[34;1m輸入課程週期:').strip() course = Course(course_name, course_price, course_time) school.create_course(main_db, course, __db_main) else: print('\033[31;1m該課程名已存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '2': # 招聘講師 while True: print('\033[33;1m目前學校【%s】已經存在的講師信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] res_teacher = information(main_db[school][k], 'teacher')[1] print(main_db) if not res_teacher: print('\033[31;1m課程【%s】\t講師【None】\033[0m' % k.name) if_cont = input('\033[34;1m是否招聘講師[y/b]:\033[0m').strip() if if_cont == 'y': teacher_name = input('\033[34;1m輸入講師名:').strip() teacher_age = input('\033[34;1m輸入講師年齡:').strip() course_name = input('\033[34;1m輸入課程名:').strip() if course_name in res_course: course = res_course[course_name] if teacher_name not in res_teacher: teacher = Teacher(teacher_name, teacher_age, school_name, course_name) school.hire_teacher(main_db, course, teacher, __db_main) else: print('\033[31;1m該講師名已存在.\033[0m') else: print('\033[31;1m該課程名不存在.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '3': # 建立班級 while True: teacher_db = file_oper(__db_teacher, 'rb') print('\033[33;1m目前學校【%s】已經存在的班級信息:\033[0m' % school_name) res_course = information(main_db[school], 'None')[0] if res_course: for i in res_course: k = res_course[i] print('k:', k) print(main_db[school][k]) print('--------------------------') res_grade = information(main_db[school][k], 'grade')[1] if not res_grade: print('\033[31;1m目前沒有任何班級信息.\033[0m') else: print('\033[31;1m目前沒有任何課程信息, 請先建立課程.\033[0m') if_cont = input('\033[34;1m是否建立班級[y/b]:\033[0m').strip() if if_cont == 'y': grade_name = input('\033[34;1m輸入班級名:\033[0m').strip() course_name = input('\033[34;1m輸入班級要上的課程:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course]: teacher = main_db[school][course]['teacher'] if grade_name not in res_grade: grade = Grade(grade_name, course_name, teacher.name) print('grade:', grade) school.create_grade(main_db, teacher_db, course, grade, teacher, __db_main, __db_teacher) else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程名不存在,請從新輸入.\033[0m') elif if_cont == 'b': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') elif choice == '4': # 返回 Flag = False else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def student_center(): while True: choice = options(student_list) main_db = file_oper(__db_main, 'rb') teacher_db = file_oper(__db_teacher, 'rb') if choice == '1': student_name = input('\033[34;1m輸入學生名:\033[0m').strip() res_school = information(main_db, 'main')[0] school_name = input('\033[34;1m輸入學校名:\033[0m').strip() if school_name in res_school: school = res_school[school_name] res_course = information(main_db[school], 'course')[0] course_name = input('\033[34;1m輸入課程名:\033[0m').strip() if course_name in res_course: course = res_course[course_name] if main_db[school][course].get('grade'): for i in teacher_db: i.course = course.name teacher = i grade = teacher_db[teacher].get('grade') print('\033[33;1m課程【%s】的費用爲【%s元】\033[0m' % (course_name, course.price)) if_pay = input('\033[34;1m是否支付當前費用(y支付):\033[0m').strip() if if_pay == 'y': grade.student.add(student_name) grade.add_student(teacher_db, teacher, student_name, __db_teacher) print('\033[32;1m選課成功!\033[0m') any = input('輸入任意鍵退出當前:') else: print('\033[31;1m講師不存在,請先招聘講師.\033[0m') else: print('\033[31;1m課程不存在,請從新選擇.\033[0m') else: print('\033[31;1m學校不存在,請從新選擇.\033[0m') elif choice == '2': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') def teacher_center(): teacher_db = file_oper(__db_teacher, 'rb') teacher_name = input('\033[34;1m輸入講師名:\033[0m').strip() res_teacher = information(teacher_db, 'None')[0] if res_teacher: if teacher_name in res_teacher: teacher = res_teacher[teacher_name] grade = teacher_db[teacher].get('grade') print('\033[33;1m歡迎進入講師【%s】的管理中心\033[0m' % teacher_name) while True: choice = options(teacher_list) if choice == '1': print('\033[32;1m學校【%s】\t班級【%s】\033[0m' % (teacher.school, grade.name)) any = input('輸入任意鍵退出當前:') elif choice == '2': print('\033[32;1m班級【%s】\t學員【%s】\033[0m' % (grade.name, grade.student)) any = input('輸入任意鍵退出當前:') elif choice == '3': break else: print('\033[31;1m輸入錯誤,請從新輸入.\033[0m') else: print('\033[31;1m講師信息不存在,請從新輸入.\033[0m') def start(): '''開始程序,根據選擇的序號進入不一樣的視圖''' while True: choice = options(main_list) if not choice: continue if choice == '1': print('\033[33;1m【學校中心】\033[0m') school_center() elif choice == '2': print('\033[33;1m【講師中心】\033[0m') teacher_center() elif choice == '3': print('\033[33;1m【學生中心】\033[0m') student_center() elif choice == '4': print('退出') break def file_oper(file, mode, *args): '''根據文件的讀或者寫,作不一樣的操做,讀返回文件內容信息''' if mode == 'wb': data = args[0] with open(file, mode) as f: pickle.dump(data, f) elif mode == 'rb': with open(file, mode) as f: data = pickle.load(f) return data def init_database(): '''初始化學校信息''' sh = School('上海', '上海市') bj = School('北京', '北京市') if not os.path.exists(__db_main): data = {sh: {}, bj: {}} file_oper(__db_main, 'wb', data) if not os.path.exists(__db_teacher): data = {} file_oper(__db_teacher, 'wb', data) if __name__ == '__main__': init_database() main_list = ['學校中心', '講師中心', '學生中心', '退出'] school_list = ['建立課程', '招聘講師', '建立班級', '返回'] student_list = ['學員註冊', '返回'] teacher_list = ['查看班級信息', '查看學員列表信息', '返回'] start()
以上代碼實現了講師中心的兩個查詢功能,流程:
1. 認證講師名信息
2. 經過講師名獲取講師類,經過講師類獲得班級類
到此,程序代碼完成,須要注意的是,在學員註冊的時候,爲 Grade類新增了student屬性,須要刪除本地 main_dict, teacher_dict 再次執行,不然報錯。
總結: 成長就是一個不斷模仿和學習的過程,對於剛學習python,拿到需求,首先要通過本身分析,確實搞不定了,能夠查看一些優秀的代碼過來學習總結, 從而轉化爲本身的能力。對於寫程序,實現邏輯思路是很重要的。