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)
class BaseMessage(object):
def send(self):
"""
必须继承BaseMessage,然后其中必须编写send方法。用于完成具体业务逻辑。
"""
raise Exception()
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()
obj = Msg()
func(obj)
示例
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()
伪代码:
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
# 知识点:如何自定义异常类?
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)
import hashlib
def md5(pwd):
# 实例化对象
obj = hashlib.md5()
# 写入要加密的字节
obj.update(pwd.encode('utf-8'))
# 获取密文
return obj.hexdigest()
user = input("请输入用户名:")
pwd = input("请输入密码:")
print(md5(pwd)) #21232f297a57a5a743894a0e4a801fc3
import hashlib
def md5(pwd):
# 实例化对象
obj = hashlib.md5()
# 写入要加密的字节
obj.update(pwd.encode('utf-8'))
# 获取密文
return obj.hexdigest()
user = input("请输入用户名:")
pwd = input("请输入密码:")
if user == 'oldboy' and md5(pwd) == '21232f297a57a5a743894a0e4a801fc3':
print('登录成功')
else:
print('登录失败')
SALT = b'2erer3asdfwerxdf34sdfsdfs90'
import hashlib
def md5(pwd):
# 实例化对象
obj = hashlib.md5(SALT)
# 写入要加密的字节
obj.update(pwd.encode('utf-8'))
# 获取密文
return obj.hexdigest()
user = input("请输入用户名:")
pwd = input("请输入密码:") #21232f297a57a5a743894a0e4a801fc3
if user == 'oldboy' and md5(pwd) == 'c5395258d82599e5f1bec3be1e4dea4a':
print('登录成功')
else:
print('登录失败')
import logging
logger = logging.basicConfig(filename='rizhi.txt',
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=30) #level 将大于等于30的错误级别写入日志中
logging.debug('x1') # 10
logging.info('x2') # 20
logging.warning('x3') # 30
logging.error('x4') # 40
logging.critical('x5') # 50
logging.log(10,'x6')
import logging
logger = logging.basicConfig(filename='rizhi.txt',
format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=30)
import traceback
def func():
try:
a = a +1
except Exception as e:
# 获取当前错误的堆栈信息
msg = traceback.format_exc()
logging.error(msg)
func()
import logging
# 创建一个操作日志的对象logger(依赖FileHandler)
file_handler = logging.FileHandler('l1.log', 'a', encoding='utf-8')
file_handler.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s"))
logger1 = logging.Logger('s1', level=logging.ERROR)
logger1.addHandler(file_handler)
logger1.error('123123123')
# 在创建一个操作日志的对象logger(依赖FileHandler)
file_handler2 = logging.FileHandler('l2.log', 'a', encoding='utf-8')
file_handler2.setFormatter(logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s"))
logger2 = logging.Logger('s2', level=logging.ERROR)
logger2.addHandler(file_handler2)
logger2.error('666')