接口数据
interface-meta的Python项目详细描述
interface_meta
提供了一种方便的方式来公开可扩展api
强制的方法签名和一致的文档。
- documentation:请参阅下面的内容(完整的文档将在某个时刻提供)。
- 来源:https://github.com/matthewwardrop/interface_meta
- bug报告:https://github.com/matthewwardrop/interface_meta/issues
概述
这个库是从
^{
- 接口的所有子类都必须符合父类的api。
- 分级运行时属性的存在和方法签名检查。方法 允许添加额外的可选参数,否则必须 符合其父类的api(它们本身可能已经扩展了 接口的api)。
- 子类定义时间挂钩(例如,将子类注册到 插件库等)。
- 子类中方法到显式修饰方法的可选要求
当替换接口上的方法时,使用
override
decorator 更清楚的是一个类何时引入新方法而不是替换 那些构成接口api的一部分。 - 在将 带有任何下游扩展和怪癖的基本接口文档。
- 支持从其他方法中提取方法的quirks文档 如果子类实现是在内部 方法。
- 与标准库中的abcmeta兼容。
示例代码
from abc import abstractmethod, abstractproperty
from six import with_metaclass
from interface_meta import InterfaceMeta, override, quirk_docs
class MyInterface(with_metaclass(InterfaceMeta, object)):
"""
An example interface.
"""
INTERFACE_EXPLICIT_OVERRIDES = True
INTERFACE_RAISE_ON_VIOLATION = False
INTERFACE_SKIPPED_NAMES = {'__init__'}
def __init__(self):
"""
MyInterface constructor.
"""
pass
@abstractproperty
def name(self):
"""
The name of this interface.
"""
pass
@quirk_docs(method='_do_stuff')
def do_stuff(self, a, b, c=1):
"""
Do things with the parameters.
"""
return self._do_stuff(a, b, c)
@abstractmethod
def _do_stuff(self, a, b, c):
pass
class MyImplementation(MyInterface):
"""
This implementation of the example interface works nicely.
"""
@quirk_docs(method='_init', mro=False)
def __init__(self, a):
"""
MyImplementation constructor.
"""
self._init(a)
def _init(self, a):
"""
In this instance, we do nothing with a.
"""
pass
@property
@override
def name(self):
return "Peter"
@override
def _do_stuff(self, a, b, c):
"""
In this implementation, we sum the parameters.
"""
return a + b + c
运行help(MyImplementation)
可以揭示文档是如何生成的:
class MyImplementation(MyInterface)
| This implementation of the example interface works nicely.
|
| Method resolution order:
| MyImplementation
| MyInterface
| builtins.object
|
| Methods defined here:
|
| __init__(self, a)
| MyImplementation constructor.
|
| In this instance, we do nothing with a.
|
| do_stuff(self, a, b, c=1)
| Do things with the parameters.
|
| MyImplementation Quirks:
| In this implementation, we sum the parameters.
...
相关项目和现有技术
这个图书馆被放在一个已经很拥挤的地方,作者会 喜欢承认别人已经做了一些很好的工作。初选 这个库和其他库的区别通常是这些其他库 更多地关注抽象接口定义和遵从性,而较少关注 文档和插件注册工作。而这项工作与这些 项目,其方法完全不同(作者认为) 保证有一个独立的图书馆。
python-interface
强调确保
接口严格遵循与
接口,当违反该接口时将引发有用的错误。
由 比较这个库的重点是函数与父类的一致性, 允许子类上的方法包含其他参数。它 还着重于确保方法签名中此类异常的文档被正确地组合到为该方法呈现的最终文档中。