日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

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

?

背景

仿django的中間件的編程思想

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

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

( 根據(jù)字符串導(dǎo)入模塊, 利用反射找到模塊下的類,實(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ù)類名稱去模塊中獲取類cls = getattr(module,class_name)# 根據(jù)類實(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应用中间件代码思想的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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