标签:业务逻辑 异常 lap err 分享 iba path sed 调用
一.为检验上一篇博客学过后的水平
class Course: def __init__(self,name,price,period): self.name = name self.price = price self.period = period class Student: def __init__(self,name): self.name = name self.courses = [] def select_course(self): """ 选择课程,已选则不能再选 :return: """ pass def show_selected_course(self): """ 查看自己所选课程 :return: """ pass def show_del_course(self): """ 删除已选课程 :return: """ pass def run(): """ 主程序 1.根据Course类创建10个课程 2.用户输入学生姓名,动态创建学生对象 3.查看所有课程 4.为学生选课 5.查看学生已选课程 6.删除已选课程 :return: """ pass
total_course_list = [] class Course: def __init__ (self,name,price,period): self.name = name self.price = price self.period = period class Student: func_list = [ {‘text‘:‘选课‘,‘name‘:‘select_course‘}, {‘text‘:‘查看课程‘,‘name‘:‘select_course‘}, {‘text‘:‘删除课程‘,‘name‘:‘select_course‘}, ] def __init__ (self,name): self.name = name self.courses = [] def select_course(self): for i,item in enumerate(total_course_list,1): print(i,item.name,item.price,item.period) num = int(input(‘请选择要选择的课程‘)) num = num - 1 course_obj = total_course_list[num] if course_obj not in self.courses: self.courses.append(course_obj) def show_selected_course(self): pass def show_del_course(self): pass def run(): for i in range(1,11): obj = Course(‘xx-%s‘ % i, 90, 90) total_course_list.append(obj) stu_name = input(‘请输入学生姓名‘) stu = Student(stu_name) for i,item in enumerate(stu.func_list,1): print(i,item[‘text‘]) while True: num = int(input(‘请选择要执行的功能序号:‘)) num = num-1 row = stu.func_list[num] name = row[‘name‘] func = getattr(stu,name) func() if __name__ == ‘__main__‘: run()
class Course: def __init__ (self,name,price,period): self.name = name self.price = price self.period = period class Student: func_list = [ {‘text‘:‘选课‘,‘name‘:‘select_course‘}, {‘text‘:‘查看课程‘,‘name‘:‘show_selected_course‘}, {‘text‘:‘删除课程‘,‘name‘:‘show_del_course‘}, ] def __init__ (self,name): self.name = name self.courses = [] def select_course(self,csl): pass def show_selected_course(self,csl): pass def show_del_course(self,csl): pass def run(): total_course_list = [] for i in range(1,11): obj = Course(‘xx-%s‘ % i, 90, 90) total_course_list.append(obj) stu_name = input(‘请输入学生姓名‘) stu = Student(stu_name) for i,item in enumerate(stu.func_list,1): print(i,item[‘text‘]) while True: num = int(input(‘请选择要执行的功能序号:‘)) num = num-1 row = stu.func_list[num] name = row[‘name‘] func = getattr(stu,name) func(total_course_list) if __name__ == ‘__main__‘: run()
二.约束
1.Python中建议使用:
class BaseMessage(object): def send(self,x1): """ 必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。 """ raise NotImplementedError(".send() 必须被重写.") class Email(BaseMessage): def send(self,x1): """ 必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。 """ pass obj = Email() obj.send(1)
BaseMessage类用于约束,约束其派生类:保证派生类中必须编写send方法,不然执行可能就会报错。
2.小练习加补充
class BaseMessage(object): def send(self): """ 必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。 """ raise NotImplementedError(".send() 必须被重写.") # raise Exception(".send() 必须被重写.") class Email(BaseMessage): def send(self): pass # 发送邮件 def f1(self): pass def f2(self): pass class Wechat(BaseMessage): def send(self): pass # 发送微信 def f1(self): pass def f2(self): pass class Msg(BaseMessage): def send(self): pass # 发送短信 def f1(self): pass def f2(self): pass def func(arg): """ 报警通知的功能 """ arg.send()
from abc import ABCMeta,abstractmethod class Base(metaclass=ABCMeta): # 抽象类 def f1(self): print(123) @abstractmethod def f2(self): # 抽象方法 pass class Foo(Base): def f2(self): print(666) obj = Foo() obj.f1()
抽象类,约束,约束继承它的派生类必须实现它其中的抽象方法。
3.需要知道的:接口
接口,接口中不允许在方法内部写代码,只能约束继承它的类必须实现接口中定义的所有方法。
interface IFoo: def f1(self,x1):pass def f2(self,x1):pass interface IBar: def f3(self,x1):pass def f4(self,x1):pass class Foo(IFoo,IBar):# 实现了2个接口 def f1(self,x1):pass def f2(self,x1):pass def f3(self,x1):pass def f4(self,x1):pass
4.总结:
(1).什么是接口以及作用
接口是一种数据类型,主要用于约束派生类中必须实现指定的方法
Python中,不存在;JAVA,C# 中是存在的
(2).Python中使用过什么来约束呐?
抽象类+抽象方法 --> 编写上麻烦
人为主动抛出异常
(3).约束时,抛出的异常是否可以用其他的?
不专业:raise Exception(".send() 必须被重写")
专业:raise NotImplementedError(".send() 必须被重写")
(4).以后看代码揣摩心思
5.应用场景
多个类.内部都必须有某些方法时,需要使用基类+异常进行约束
class IBase: def login(): raise NotImplementedError(".send() 必须被重写.") class Student: def login(self): pass def score(self): pass class Teacher: def login(self): pass def exam(self): pass class Manager(self): def login(self): pass ....
三.异常处理
import os class ExistsError(Exception): pass class KeyInvalidError(Exception): pass def new_func(path,prev): """ 去path路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者。 1000,成功 1001,文件不存在 1002,关键字为空 1003,未知错误 ... :return: """ response = {‘code‘:1000,‘data‘:None} try: if not os.path.exists(path): raise ExistsError() if not prev: raise KeyInvalidError() pass except ExistsError as e: response[‘code‘] = 1001 response[‘data‘] = ‘文件不存在‘ except KeyInvalidError as e: response[‘code‘] = 1002 response[‘data‘] = ‘关键字为空‘ except Exception as e: response[‘code‘] = 1003 response[‘data‘] = ‘未知错误‘ return response def func(path,prev): """ 去path路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者。 1000,成功 1001,文件不存在 1002,关键字为空 1003,未知错误 ... :return: """ response = {‘code‘:1000,‘data‘:None} try: if not os.path.exists(path): response[‘code‘] = 1001 response[‘data‘] = ‘文件不存在‘ return response if not prev: response[‘code‘] = 1002 response[‘data‘] = ‘关键字为空‘ return response pass except Exception as e: response[‘code‘] = 1003 response[‘data‘] = ‘未知错误‘ return response def show(): return 8 def run(): pass
# 知识点:如何自定义异常类? class MyException(Exception): def __init__(self,code,msg): self.code = code self.msg = msg try: # 知识点:主动抛出异常 raise MyException(1000,‘操作异常‘) except KeyError as obj: print(obj,1111) except MyException as obj: # 知识点:捕获异常 print(obj,2222) except Exception as obj: print(obj,3333)
四.加密,hashlib模块
hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, sha512等算法
11
标签:业务逻辑 异常 lap err 分享 iba path sed 调用
原文地址:https://www.cnblogs.com/rixian/p/9567240.html