在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  網(wǎng)絡(luò)安全/ 如何更優(yōu)雅的實(shí)現(xiàn)工廠模式或其他解決方法

如何更優(yōu)雅的實(shí)現(xiàn)工廠模式或其他解決方法

場景是這樣的
commands目錄中會有一堆的py文件。每個文件都是一個class。每個class都有不同的方法。

比如

#user.py

class user():
    def login():
        pass

# order.py

class order():
    def close(order_id):
        pass

像這種文件在commands目錄中有很多,而且隨時會增加或者減少。有一個daemon.py會根據(jù)MQ消息隊(duì)列的命令動態(tài)的實(shí)例化某個命令并且傳遞對應(yīng)的參數(shù)過去。

# daemon.py 偽代碼
import commands

while True:
    message = json.loads(mq.receive())
    if not hasattr(commands, message['class'])
        logger.warn("error")
    command = commands[message['class']]()
    command. message['action'] (message) ????

應(yīng)該如何實(shí)現(xiàn)或者我不應(yīng)該這么用python?用php用慣了嗎?

回答
編輯回答
離殤

自問自答。

module = __import__('module.%s' % modulename)
class_ = module()
func = getattr(class_, 'func')
func()
2017年1月21日 04:20