您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Python面向函數(shù)轉(zhuǎn)面向?qū)ο髮嵗治觥?,?nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Python面向函數(shù)轉(zhuǎn)面向?qū)ο髮嵗治觥蔽恼履軒椭蠹医鉀Q疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
我們今天有兩個目的,第一就是將之前面向函數(shù)開發(fā)的學生信息庫功能轉(zhuǎn)變?yōu)槊嫦驅(qū)ο箝_發(fā)的學生信息庫功能;第二就是要實現(xiàn)批量的增刪改查及模糊查找的功能。
代碼示例如下:
# coding:utf-8 """ @Author:Neo @Date:2020/1/15 @Filename:students_info.py @Software:Pycharm """ class StudentInfo(object): def __init__(self, students): self.students = students def get_by_id(self, student_id): return self.students.get(student_id) def get_all_students(self): for id_, value in self.students.items(): print('學號:{}, 姓名:{}, 年齡:{}, 性別:{}, 班級:{}'.format( id_, value['name'], value['age'], value['sex'], value['class_number'] )) return self.students def add(self, **student): check = self.check_user_info(**student) if check != True: print(check) return self.__add(**student) def adds(self, new_students): for student in new_students: check = self.check_user_info(**student) if check != True: print(check, student.get('name')) continue self.__add(**student) def __add(self, **student): new_id = max(self.students) + 1 self.students[new_id] = student def delete(self, student_id): if student_id not in self.students: print('{} 并不存在'.format(student_id)) else: user_info = self.students.pop(student_id) print('學號是{}, {}同學的信息已經(jīng)被刪除了'.format(student_id, user_info['name'])) def deletes(self, ids): for id_ in ids: if id_ not in self.students: print(f'{id_} 不存在學生庫中') continue student_info = self.students.pop(id_) print(f'學號{id_} 學生{student_info["name"]} 已被移除') def update(self, student_id, **kwargs): if student_id not in self.students: print('并不存在這個學號:{}'.format(student_id)) check = self.check_user_info(**kwargs) if check != True: print(check) return self.students[student_id] = kwargs print('同學信息更新完畢') def updates(self, update_students): for student in update_students: id_ = list(student.keys())[0] if id_ not in self.students: print(f'學號{id_} 不存在') continue user_info = student[id_] check = self.check_user_info(**user_info) if check != True: print(check) continue self.students[id_] = user_info print('所有用戶信息更新完成') def search_users(self, **kwargs): values = list(self.students.values()) key = None value = None result = [] if 'name' in kwargs: key = 'name' value = kwargs[key] elif 'sex' in kwargs: key = 'sex' value = kwargs['sex'] elif 'class_number' in kwargs: key = 'class_number' value = kwargs[key] elif 'age' in kwargs: key = 'age' value = kwargs[key] else: print('沒有發(fā)現(xiàn)搜索的關(guān)鍵字') return for user in values: # [{name, sex, age, class_number}, {}] if value in user[key]: result.append(user) return result def check_user_info(self, **kwargs): if 'name' not in kwargs: return '沒有發(fā)現(xiàn)學生姓名' if 'age' not in kwargs: return '缺少學生年齡' if 'sex' not in kwargs: return '缺少學生性別' if 'class_number' not in kwargs: return '缺少學生班級' return True students = { 1: { 'name': 'Neo', 'age': 18, 'class_number': 'A', 'sex': 'boy' }, 2: { 'name': 'Jack', 'age': 16, 'class_number': 'B', 'sex': 'boy' }, 3: { 'name': 'Lily', 'age': 18, 'class_number': 'A', 'sex': 'girl' }, 4: { 'name': 'Adem', 'age': 18, 'class_number': 'C', 'sex': 'boy' }, 5: { 'name': 'HanMeiMei', 'age': 18, 'class_number': 'B', 'sex': 'girl' } } if __name__ == '__main__': student_info = StudentInfo(students) user = student_info.get_by_id(1) student_info.add(name='Marry', age=16, class_number='A', sex='girl') users = [ {'name': 'Atom', 'age': 17, 'class_number': 'B', 'sex': 'boy'}, {'name': 'Lucy', 'age': 18, 'class_number': 'C', 'sex': 'girl'} ] student_info.adds(users) student_info.get_all_students() print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> 學號:1, 姓名:Neo, 年齡:18, 性別:boy, 班級:A # >>> 學號:2, 姓名:Jack, 年齡:16, 性別:boy, 班級:B # >>> 學號:3, 姓名:Lily, 年齡:18, 性別:girl, 班級:A # >>> 學號:4, 姓名:Adem, 年齡:18, 性別:boy, 班級:C # >>> 學號:5, 姓名:HanMeiMei, 年齡:18, 性別:girl, 班級:B # >>> 學號:6, 姓名:Marry, 年齡:16, 性別:girl, 班級:A # >>> 學號:7, 姓名:Atom, 年齡:17, 性別:boy, 班級:B # >>> 學號:8, 姓名:Lucy, 年齡:18, 性別:girl, 班級:C # >>> ------------------------------------------------------ student_info.deletes([7, 8]) student_info.get_all_students() print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> ------------------------------------------------------ # >>> 學號7 學生Atom 已被移除 # >>> 學號8 學生Lucy 已被移除 # >>> 學號:1, 姓名:Neo, 年齡:18, 性別:boy, 班級:A # >>> 學號:2, 姓名:Jack, 年齡:16, 性別:boy, 班級:B # >>> 學號:3, 姓名:Lily, 年齡:18, 性別:girl, 班級:A # >>> 學號:4, 姓名:Adem, 年齡:18, 性別:boy, 班級:C # >>> 學號:5, 姓名:HanMeiMei, 年齡:18, 性別:girl, 班級:B # >>> 學號:6, 姓名:Marry, 年齡:16, 性別:girl, 班級:A # >>> ------------------------------------------------------ student_info.updates([ {1: {'name': 'Jone', 'age': 18, 'class_number': 'A', 'sex': 'boy'}}, {2: {'name': 'Nike', 'age': 18, 'class_number': 'A', 'sex': 'boy'}} ]) student_info.get_all_students() print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> ------------------------------------------------------ # >>> 所有用戶信息更新完成 # >>> 學號:1, 姓名:Jone, 年齡:18, 性別:boy, 班級:A # >>> 學號:2, 姓名:Nike, 年齡:18, 性別:boy, 班級:A # >>> 學號:3, 姓名:Lily, 年齡:18, 性別:girl, 班級:A # >>> 學號:4, 姓名:Adem, 年齡:18, 性別:boy, 班級:C # >>> 學號:5, 姓名:HanMeiMei, 年齡:18, 性別:girl, 班級:B # >>> 學號:6, 姓名:Marry, 年齡:16, 性別:girl, 班級:A # >>> ------------------------------------------------------ result = student_info.search_users(name='d') print(result) print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> [{'name': 'Adem', 'age': 18, 'class_number': 'C', 'sex': 'boy'}] result = student_info.search_users(name='小') print(result) print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> ------------------------------------------------------ # >>> [] # >>> ------------------------------------------------------ result = student_info.search_users(name='') print(result) result = student_info.search_users(name='小') print(result) print('------------------------------------------------------') # >>> 執(zhí)行結(jié)果如下: # >>> ------------------------------------------------------[{'name': 'Jone', 'age': 18, 'class_number': 'A', 'sex': 'boy'}, {'name': 'Nike', 'age': 18, 'class_number': 'A', 'sex': 'boy'}, {'name': 'Lily', 'age': 18, 'class_number': 'A', 'sex': 'girl'}, {'name': 'Adem', 'age': 18, 'class_number': 'C', 'sex': 'boy'}, {'name': 'HanMeiMei', 'age': 18, 'class_number': 'B', 'sex': 'girl'}, {'name': 'Marry', 'age': 16, 'class_number': 'A', 'sex': 'girl'}] # >>> [] result = student_info.search_users(name='') print(result)
讀到這里,這篇“Python面向函數(shù)轉(zhuǎn)面向?qū)ο髮嵗治觥蔽恼乱呀?jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。