一个接口思路

utils\message

base.py

1
2
3
4
5
class MsgBase(object):
def __init__(self):
pass
def send(self):
raise NotImplementedError('必须实现send方法')

email.py

1
2
3
4
5
6
7
8
9
10
from .base import MsgBase
class Email(MsgBase):
"""初始化相关"""
def __init__(self):
"""相关信息初始化"""
self.username = 'zh'
self.password = '123'
def send(self,msg):
"""发送"""
pass

wechart.py

1
2
3
4
5
6
7
8
9
10
from .base import MsgBase
class Wechart(MsgBase):
"""初始化相关"""
def __init__(self):
"""相关信息初始化"""
self.username = 'zh'
self.password = '123'
def send(self,msg):
"""发送"""
pass

__init__.py

1
2
3
4
5
6
7
8
import utils.settings
import importlib
def send_msg(msg):
for path in utils.settings.MSG_LIST:
m,c = path.rsplit(".",maxsplit=1)
md = importlib.import_module(m)
cls = getattr(md,c)()
cls.send(msg)

setting.py

1
2
3
4
MSG_LIST = [
'utils.message.email.Email',
'utils.message.wechart.Wechart'
]

主逻辑.py

1
2
3
4
from utils.message import send_msg

def xxx(self):
send_msg('....')
都看到这里了,不赏点银子吗^v^