zoukankan      html  css  js  c++  java
  • 约束、异常处理、加密及日志

    一、约束

    1、通过继承来约束派生类的内部必须要实现的方法

     1 class BaseMessage(object):
     2     def send(self,x1):
     3         """
     4         必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。
     5         """
     6         raise NotImplementedError(".send() 必须被重写.")
     7 
     8 class Email(BaseMessage):
     9     def send(self,x1):
    10         """
    11         必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。
    12         """
    13         pass
    14 
    15 
    16 obj = Email()
    17 obj.send(1)

    抛出异常:raise NotImplementedError()
    2、通过继承抽象类来完成约束

    继承抽象类并实现抽象类里的抽象方法、从而达到约束 (抽象类里除了抽象方法外也可以包含实例方法)

     1 abstact class Foo:
     2     def f1(self):
     3         print(1,3,4) 
     4                             
     5 abstact def f2(self):pass
     6                     
     7 class Bar(Foo):
     8     def f2(self):
     9         print('111')
    10                             

    3、通过接口也可以实现约束
    接口是一种数据类型,接口里都是未实现的方法,通过继承接口,完成所有方法

    二、异常处理

     1 import os
     2 
     3 class ExistsError(Exception):
     4     pass
     5 
     6 class KeyInvalidError(Exception):
     7     pass
     8 
     9 def new_func(path,prev):
    10     """
    11     去path路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者。
    12         1000,成功
    13         1001,文件不存在
    14         1002,关键字为空
    15         1003,未知错误
    16         ...
    17     :return:
    18     """
    19     response = {'code':1000,'data':None}
    20     try:
    21         if not os.path.exists(path):
    22             raise ExistsError()
    23 
    24         if not prev:
    25             raise KeyInvalidError()
    26         pass
    27     except ExistsError as e:
    28         response['code'] = 1001
    29         response['data'] = '文件不存在'
    30     except KeyInvalidError as e:
    31         response['code'] = 1002
    32         response['data'] = '关键字为空'
    33     except Exception as e:
    34         response['code'] = 1003
    35         response['data'] = '未知错误'
    36     return response

    自定义异常:

    1 class MyException(Exception):
    2     def __init__(self,code,msg):
    3         self.code = code
    4         self.msg = msg
    5 try:
    6     raise MyException(1000,'操作异常')  #实例化,将数据封装
    7 
    8 except MyException as obj: 
    9     print(obj.code,obj.msg)       #打印出异常
     1 class MyException(Exception):
     2     def __init__(self,code,msg):
     3         self.code = code
     4         self.msg = msg
     5 try:
     6     raise MyException(1000,'操作异常')
     7 
     8 except KeyError as obj:
     9     print(obj,1111)
    10 except MyException as obj: #捕获异常就处理
    11     print(obj,2222)
    12 except Exception as obj:   # 捕获所有类型异常
    13     print(obj,3333)

    三、加密
    1、普通加密密码

    1 import hashlib
    2 
    3 def md5():
    4     obj=hashlib.md5()
    5     obj.update("admin".encode("utf-8"))
    6     return obj.hexdigest()
    7 ret=md5()
    8 print(ret)    #21232f297a57a5a743894a0e4a801fc3  admin加密后的密码

    2、加盐

     1 import hashlib
     2 
     3 salt=b"sadasfdsfdfgd23ew23ewr"  #加盐
     4 def md5():
     5     # 实例化对象
     6     obj=hashlib.md5(salt)
     7     # 写入要加密的字节
     8     obj.update("admin".encode("utf-8"))
     9     # 获取密文
    10     return obj.hexdigest()
    11 ret=md5()
    12 print(ret)        #ae224d80a1cd1ab295f73fe335eb8516

    3、验证用户登录

     1 import hashlib
     2 
     3 salt=b"sadasfdsfdfgd23ew23ewr"
     4 def md5(pwd):
     5     # 实例化对象
     6     obj=hashlib.md5(salt)
     7     # 写入要加密的字节
     8     obj.update(pwd.encode("utf-8"))
     9     # 获取密文
    10     return obj.hexdigest()
    11 
    12 pwd=input("请输入密码")
    13 ret=md5(pwd)
    14 if ret=="ae224d80a1cd1ab295f73fe335eb8516":
    15     print("登录成功")
    16 else:
    17     print("登录失败")

    四、日志处理

  • 相关阅读:
    UnknownHostException: xxx,使用nacos远程调用服务(负载均衡)报错
    Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, java.lang.String>日志级别的问题
    JS 如何返回到父页面?多重跳转之后返回到初始页(父页面)?或者说返回父页面的父页面?
    绝对路径${pageContext.request.contextPath}用法及其与web.xml中Servlet的url-pattern匹配过程
    VS在release模式下进行调试
    visual studio 运行找不到dll库
    C++ 字符串格式化
    SWIG使用遇到的问题
    其他技术---域名中转
    mongoDB增删改查(命令行操作数据库)
  • 原文地址:https://www.cnblogs.com/liaopeng123/p/9566902.html
Copyright © 2011-2022 走看看