เช่นผมมี main.py อยู่ที่ . แล้วมี plugins ต่่างๆ ที่อยากให้ถูกโหลดขึ้นมาอยู่ที่ ./mylibs/ ใน mylib มี

__init__.py plugin_interface.py simple_plugin.py simple_plugin2.py

code ของ plugin_interface.py class PluginInterface(object): def __init__(self): pass def getInfo(self,inputData): raise NotImplementedError (ใช้ไม่ได้ก็เชิญดุด่าว่ากล่าวได้เลยครับ) code ของ simple_plugin.py from mylib.PluginInterface import PluginInterface class SimplePlugin(PluginInterface): def __init__(self): PluginInterface.__init__(self) def getInfo(self,inputData): print "Your output from plugin1 is",type(inputData) code ของ simple_plugin2.py from mylib.PluginInterface import PluginInterface class SimplePlugin2(PluginInterface): def __init__(self): PluginInterface.__init__(self) def getInfo(self,inputData): print "Your output from plugin2 is",inputData+inputData

main.py(ไม่อัตโนมัติ ต้อง import ใหม่หมด) from mylib.simple_plugin import SimplePlugin from mylib.simple_plugin2 import SimplePlugin2

plugins = [] plugins.append(SimplePlugin()) plugins.append(SimplePlugin2()) for plugin in plugins: plugin.getInfo(1)

output Your output from plugin1 is Your output from plugin2 is 2

ผมควรจะเขียน main.py อย่างไรถ้าเกิดว่าผมมี simple3_plugin.py ขึ้นมา โดยที่ไม่ต้องคอยมาเพิ่ม code ใน main พอมีทางเป็นไปได้หรือเปล่าครับ หรือว่าถ้าผมออกแบบมานี่มันเป็นไปไม่ได้ควรจะแก้ไขยังไงดีครับ

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

hereblur Tue, 16/09/2008 - 17:08

ลองดูตัวอย่างนี้นะครับ ไม่ต้อง import ก่อน

main.py

import imp

plug_ins_list = ["plugin1.MyPlugIn1", "plugin2.MyPlugIn2"]

class main :
def init(self) :
self.plugins = []

def loadPlugin( self, moduleName ) : 
    # find module file from specific directory
    PluginFilename, PluginClassName = moduleName.split('.')
    file, filename, description = imp.find_module( PluginFilename, ['.'])
    
    # load module
    pluginmodule = imp.load_module(PluginFilename, file, filename, description)
    
    pluginClass = getattr(pluginmodule, PluginClassName)
    self.plugins.append( pluginClass() )
    
def action(self) :
    print "I'M MAIN."
    for p in  self.plugins :
        p.action()

m = main()
for p in plug_ins_list :
m.loadPlugin(p)
m.action()

plugin1.py

class MyPlugIn1 :
def init(self) :
pass

def action(self) :
    print "I'M PLUG IN 1.\n"

plugin2.py

class MyPlugIn2 :
def init(self) :
pass

def action(self) :
    print "I'M PLUG IN TWO.\n"

น่าจะได้นะครับขอบคุณครับ แบบนี้ถ้าเราแยก plug_ins_list = ["plugin1.MyPlugIn1", "plugin2.MyPlugIn2"]

ออกมาเป็น config file เราก็แค่ 1. เพิ่ม file plugin เข้าไปที่ directory ที่เราจะเก็บ plug in 2. แก้ไข ไฟล์ config เพิ่มชื่อ plug-in เข้าไป

ทำนองนั้นใช่มั้ยครับ

ปล. python มี interface class หรือเปล่าครับ(หรือว่าควรไปตั้งกระทู้ใหม่) ปล.2 ถ้ามีแล้วทำยังไงหรือครับ ใช้เผื่อกำหนดให้ plug-in ต้องมี method action เหมือนในที่นี้น่ะครับ

อย่าเข้าไปอ่านนะ บทความของ Rookie

bow_der_kleine Wed, 17/09/2008 - 00:07

In reply to by mossila

interface class คืออะไรอะ ? เพิ่งได้ยินเป็นครั้งแรกครับ

นี่คือปัญหาอย่างหนึ่งของการเปลี่ยนภาษาเขียนโปรแกรมครับ คือ เราจะชอบคิดในรูปแบบภาษาเขียนโปรแกรมที่เรารู้จักอยู่แล้ว ใน Python ทุกอย่างเป็น dynamic ครับ ดังนั้น interface จึงไม่ค่อยมีประโยชน์ ยกตัวอย่าง


class PluginInterface(object):
def __init__(self):
pass
def getInfo(self,inputData):
raise NotImplementedError

จะไม่ค่อยเจอใน python ครับ เพราะไม่มีหน้าที่อะไร นอกจากตรวจสอบว่า sub class มีเมโธท getInfo หรือเปล่า ซึ่งหาก class ที่เราสร้างขึ้น ไม่ได้มี super class เป็น PluginInterface ทุกอย่างก็จบ ซึ่งการแก้ปัญหาเรื่องคลาสต้องมีเมโธทที่กำหนด ผมคิดว่าใช้ try, except ตอนเรียกปลั้กอินน่าจะดีกว่า


try :
plugin.action()
except AttributeError :
print 'Hey, your plugin needs method action()!'

BioLawCom.De

hereblur Wed, 17/09/2008 - 10:55

In reply to by mossila

Interface Class และ Abstract Class เป็นเพียงตัวช่วยให้การเขียน OOP ง่ายขึ้นครับ เวลา implement ระบบใหญ่ ๆ เขียนหลาย ๆ คน มีเฉพาะบางภาษา เช่น Java ,C#

แต่ไม่ใช่สิ่งที่จำเป็นต้องมี ใน Python ก็เลยไม่มี เพราะต้องการความยืดหยุ่นมากกว่า อย่างที่คุณ bow_der_kleine ว่าไว้