日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

django17:importlib应用中间件代码思想

發(fā)布時(shí)間:2023/12/4 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 django17:importlib应用中间件代码思想 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載:https://www.cnblogs.com/alice-bj/articles/9276880.html

?

背景

仿django的中間件的編程思想

用戶可通過(guò)配置,選擇是否啟用某個(gè)組件/某個(gè)功能,只需要配置

eg:報(bào)警系統(tǒng),發(fā)郵件,發(fā)微信 。。。

( 根據(jù)字符串導(dǎo)入模塊, 利用反射找到模塊下的類(lèi),實(shí)例化。執(zhí)行 )

?

code

?

# settings.pyNOTIFY_LIST = ['notify.email.Email','notify.msg.Msg','notify.wechat.Wechat', ]-----------------------------# app.pyfrom notify import send_xxxdef run():send_xxx("報(bào)警")if __name__ == "__main__":run()----------------------------# notify/__init__.py# 根據(jù)字符串 導(dǎo)入模塊import settings import importlibdef send_xxx(content):for path in settings.NOTIFY_LIST:# 'notify.email.Email',# 'notify.msg.Msg',# 'notify.wechat.Wechat',module_path,class_name = path.rsplit('.',maxsplit=1)# 根據(jù)字符串導(dǎo)入模塊module = importlib.import_module(module_path)# 根據(jù)類(lèi)名稱(chēng)去模塊中獲取類(lèi)cls = getattr(module,class_name)# 根據(jù)類(lèi)實(shí)例化obj = cls()obj.send(content)-----------------------------# notify/email.pyclass Email(object):def __init__(self):passdef send(self,content):print("email send..")# notify/msg.pyclass Msg(object):def __init__(self):passdef send(self,content):print("msg send..")# notify/wechat.pyclass Wechat(object):def __init__(self):passdef send(self,content):print("wechat send..")

?

總結(jié)

以上是生活随笔為你收集整理的django17:importlib应用中间件代码思想的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。