我正在爲python中的內部基於xml的元數據格式編寫解析器。我需要提供不同的類來處理不同的標籤。將需要一個相當大的處理程序集合,所以我將它想象成一個簡單的插件系統。我想要做的只是加載包中的每個類,並將其註冊到我的解析器。 我現在的嘗試是這樣的:
(處理程序是包含處理器的包裝,每個處理器有一個靜態成員變量,這是一個字符串數組)簡單的python插件系統
class MetadataParser:
def __init__(self):
#...
self.handlers={}
self.currentHandler=None
for handler in dir(Handlers): # Make a list of all symbols exported by Handlers
if handler[-7:] == 'Handler': # and for each of those ending in "Handler"
handlerMod=my_import('MetadataLoader.Handlers.' + handler)
self.registerHandler(handlerMod, handlerMod.tags) # register them for their tags
# ...
def registerHandler(self, handler, tags):
""" Register a handler class for each xml tag in a given list of tags """
if not isSequenceType(tags):
tags=(tags,) # Sanity check, make sure the tag-list is indeed a list
for tag in tags:
self.handlers[tag]=handler
但是,這是行不通的。我收到錯誤AttributeError: 'module' object has no attribute 'tags'
我在做什麼錯?
修復您的縮進。 – SilentGhost 2009-06-17 11:59:45
你並沒有提供太多的信息。錯誤信息可能會說*錯誤發生在哪一行,這顯然有助於查明問題。此外,你的代碼有語法錯誤(在類名稱縮進後缺少冒號),所以你在這裏發佈的內容根本不會運行。此外,錯誤可能發生在您未發佈的代碼的某些部分中...... – sth 2009-06-17 12:13:00