[英]Can I adapt strings and built-in types in Z
我有一個像這樣的接口:
class IRepository(Interface):
def __init__(path, **options):
pass
我對Git和Mercurial都有此接口的實現。 現在,我想編寫一個采用字符串(路徑)並通過查詢git
或hg
存儲庫來返回IRepository的repository-factory。
但是,簡單地說:
registerAdapter(repofactory, (str, unicode, ), IRepository)
不起作用,因為str
和unicode
都不支持IInterface
接口。
現在,我要使用:
registerAdapter(repofactory, (Interface, ), IRepository)
但是我想知道是否存在僅匹配字符串對象和其他Python內置類型的接口。
不,字符串和unicode對象不能具有接口。 但是對於這種用例,我將注冊一個名為實用程序的名稱,並按名稱查找該實用程序,或列出所有可用的實用程序:
from zope.component import getUtilitiesFor, getUtility
names = [name for name, utility in getUtilitiesFor(IRepository)]
gitrepo = getUtility(IRepository, name='git')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.