[英]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.