股理财

# 一.约束

#约束某各类
# 用于约束其派生类,保证其派生类中有send方法,不然执行可能会报错




# 约束其派生类,python中用类来实现,Java和C#中用接口或类来实现
# 对于Java/C#:
# 类:(java和C#是先编译后运行的,所以如果不满足条件,一开始就会报错,根本无法运行)

# 类:
# class Foo:
# def f1(self):
# pass #可认为抛出异常

# 抽象类:约束他的派生类必须实现他其中的抽象方法
# abstact class Foo:
# def f1(self): #此方法可以不用继承
# pass
#
# abstact def f2(self):
# pass
# class Bar:
# def f2(self):
# pass

# 接口:接口中可以有多个接口

# interface Foo:
# def f1(self,x1):pass
#
# def f2(self,x1):pass
#
# interface Bar:
# def f3(self,x1):pass
#
# def f4(self,x1):pass
#
# class Aou(Foo,Bar):
# def f1(self,x1):pass
#
# def f2(self,x1):pass
#
# def f3(self,x1):pass
#
# def f4(self,x1):pass


#python中:
# 类:
# class BaseMessage:
# def send(self):
# '''
# 必须继承BaseMessage,然后其中必须编写send方法,用于完成具体的业务逻辑
# '''
# # raise Exception('...')
# raise NotImplementedError('send方法必须被重写') #更专业 #NotImplementedError是没有实现的意思

#class Email(BaseMessage):
# def send(self):
# pass #发送邮件
#
# def f1(self):
# pass
#
# def f2(self):
# pass
#
# class Wechat(BaseMessage):
# def send(self):
# pass
#
# def f3(self):
# pass
#
# class Messege(BaseMessage):
# def send(self):
# pass
#
# def f4(self):
# pass
#
# def func(arg):
# '''
# 报警通知
# :param arg:
# :return:
# '''
# 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()
# obj.f2()

#总结:

#1.什么是接口,以及作用?
# 接口是一种数据类型,用于约束派生类中必须实现指定方法
# python中不存在,在Java和C#中存在
#2.python中用什么来约束
# 抽象方法,抽象类(编写上麻烦)
# 认为主动抛出异常
#3.约束时抛出的异常是否可以用其他的
# 不专业:raise Exception('...')
# 专业:raise NotImplementedError('send方法必须被重写')
#4.以后看代码时,揣摩写代码的人的心思
#
#5.应用场景:
# 多个类,内部都必须有某各类或某个方法时,需要使用基类+异常进行约束


#学员管理系统;
# class Base:
# def login(self):
# raise NotImplementedError('...')
#
# class Student(Base):
# def login(self):
# pass
# def score(self):
# pass
#
# class Teather(Base):
# def login(self):
# pass
#
# def exam(self):
# pass
#
# class Messaer(Base):
# def login(self):
# pass
# def set(self):
# pass

#二.自定义异常
# 1.业务逻辑简单时
# import os
# def func(path,prev):
# '''
# 去路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者'
# 1000 成功
# 1001 文件不存在
# 1002 关键字为空
# 1003 未知错误
# :param path:
# :param prev:
# :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
# except Exception as e:
# response['code']=1003
# response['data']='未知错误'
# return response
# def func2():
# return 8
# def show():
# v1=func()
# v2=func2()

#业务逻辑复杂时
# class ExistsError(Exception): #自定义异常类,自定义的异常类要继承Exception
# pass
# class KeyInvalidError(Exception):
# pass
# import os
# def new_func(path,prev):
# '''
# 去路径的文件中,找到前缀为prev的一行数据,获取数据并返回给调用者'
# 1000 成功
# 1001 文件不存在
# 1002 关键字为空
# 1003 未知错误
# '''
# response ={'code':1000,'data':None}
# try: #目的就是为了让try中的代码简单明了
# if not os.path.exists(path):
# raise ExistsError
# if not prev:
# raise KeyInvalidError
# 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 func2():
# return 8
# def show():
# v1=new_func()
# v2=func2()

# 自定义异常还可以这样写
# class MyException(Exception): #异常也是一个类
# def __init__(self,code,msg):
# self.code=code
# self.msg=msg
#
# try : #主动抛出异常
# raise MyException(1000,'异常类型')
#
# except MyException as e: #捕获异常
# print(e.code,e.msg)

#三. 加密(一些密码类的内容,如果不加密一但泄露,会造成严重的)

# import hashlib #帮助加密的模块
#
# obj=hashlib.md5(b'6khiy78g76tfmjyvf64')
# # 写入要加加密的字节
# obj.update('admin'.encode('utf-8'))
#
# v=obj.hexdigest() #获取密文
# print(v)

# 关键词:撞库 将常见的密文归纳总结,一个一个试

# 加盐:obj=hashlib.md5(b'6khiy78g76tfmjyvf64')

# Hash objects have these methods:
# - update(arg): Update the hash object with the bytes in arg. Repeated calls
# are equivalent to a single call with the concatenation of all
# the arguments.
# - digest(): Return the digest of the bytes passed to the update() method
# so far.
# - hexdigest(): Like digest() except the digest is returned as a unicode
# object of double length, containing only hexadecimal digits.
# - copy(): Return a copy (clone) of the hash object. This can be used to
# efficiently compute the digests of strings that share a common
# initial substring.


# 实例

# import hashlib
# mi=b'6khiy78g76tfmjyvf64'
# def md5(ped):
#
# obj=hashlib.md5(mi)
#
# obj.update('admin'.encode('utf-8'))
#
# return obj.hexdigest()
#
# name=input('亲输入用户名')
# pwd=input('亲输入用密码')
#
# if name=='ninini' and md5(pwd)=='263930cf6ae488d074e32def60d973cc':
# print('登录成功')


# 四.日志(logging)

# 为什么要有日志:
# 给开发人员看,用于排查错误

# import logging
# #路径或文件名
# logger=logging.basicConfig(filename='log.log',
# format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
# datefmt='%Y-%m-%d %H:%M:%S %p',
# level=10) #用于控制日志的错误级别
# logging.debug('x1') #10 正常测试
# logging.info('x2') #20 正常的信息
# logging.warning('x3') #30 警告
# logging.error('x4') #40 错误
# logging.critical('x5') #50 立即解决的错误
# logging.log(10,'log')
#
# def func():
# try:
# a=a+1
#
# except Exception as e:
# print(e)
# logging.error(str(e))
# func()

#日志错误处理
# import logging
# #路径或文件名
# logger=logging.basicConfig(filename='log.log',
# format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
# datefmt='%Y-%m-%d %H:%M:%S %p',
# level=10) #用于控制日志的错误级别
# logging.debug('x1') #10 正常测试
# logging.info('x2') #20 正常的信息
# logging.warning('x3') #30 警告
# logging.error('x4') #40 错误
# logging.critical('x5') #50 立即解决的错误
# logging.log(10,'log')
# import traceback
# def func():
# try:
# a=a+1
#
# except Exception as e:
# # 获取当前错误的堆栈信息
# msg=traceback.format_exc()
# logging.error(msg)
# func()

猜你喜欢

转载自www.cnblogs.com/shanghongyun/p/9566230.html