mmfutils.interface¶
Stand-in for zope.interface if it is not available.
-
class
mmfutils.interface.
Attribute
(__name__, __doc__='')[source]¶ Bases:
zope.interface.interface.Element
Attribute descriptions
-
interface
= None¶
-
-
interface
mmfutils.interface.
Interface
¶
-
mmfutils.interface.
describe_interface
(interface, format='ipython')[source]¶ Return an HTML object for Jupyter notebooks that describes the interface.
- Parameters
interface (Interface) – Interface to extract documentation from.
format ('rst', 'html', 'ipython') – Return format. ‘rst’ is raw RestructuredText, ‘html’ is packaged as HTML, and ‘ipython’ is packaged as an IPython.display.HTML() object suitable for Jupyter notebooks.
Example
>>> class IExample(Interface): ... x = Attribute("Floating point number") ... def two(): ... "Return two" >>> print(describe_interface(IExample, format='rst').strip()) ``IExample`` Attributes: ``x`` -- Floating point number Methods: ``two()`` -- Return two
You can also get this wrapped as HTML:
>>> print(describe_interface(IExample, format='html').strip()) <!DOCTYPE html ... <p><tt class="docutils literal">IExample</tt></p> <blockquote> <p>Attributes:</p> <blockquote> <tt class="docutils literal">x</tt> -- Floating point number</blockquote> <p>Methods:</p> <blockquote> <tt class="docutils literal">two()</tt> -- Return two</blockquote> </blockquote> </div>
In a Jupyter notebook, this will properly display:
>>> describe_interface(IExample) <IPython.core.display.HTML object>
Other formats are not yet supported:
>>> describe_interface(IExample, format='WYSIWYG') Traceback (most recent call last): ... NotImplementedError: format WYSIWYG not supported
-
class
mmfutils.interface.
implementer
(*interfaces)[source]¶ Bases:
object
Declare the interfaces implemented by instances of a class.
This function is called as a class decorator.
The arguments are one or more interfaces or interface specifications (~zope.interface.interfaces.IDeclaration objects).
The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared, unless the interface is already implemented.
Previous declarations include declarations for base classes unless implementsOnly was used.
This function is provided for convenience. It provides a more convenient way to call classImplements. For example:
@implementer(I1) class C(object): pass
is equivalent to calling:
classImplements(C, I1)
after the class has been created.
See also
classImplements The change history provided there applies to this function too.
-
interfaces
¶
-
-
mmfutils.interface.
verifyClass
(iface, candidate, tentative=False)[source]¶ Verify that the candidate might correctly provide iface.
-
mmfutils.interface.
verifyObject
(iface, candidate, tentative=False)[source]¶ Verify that candidate might correctly provide iface.
This involves:
Making sure the candidate claims that it provides the interface using
iface.providedBy
(unless tentative is True, in which case this step is skipped). This means that the candidate’s class declares that it implements <zope.interface.implementer> the interface, or the candidate itself declares that it provides <zope.interface.provider> the interfaceMaking sure the candidate defines all the necessary methods
Making sure the methods have the correct signature (to the extent possible)
Making sure the candidate defines all the necessary attributes
- Return bool
Returns a true value if everything that could be checked passed.
- Raises
zope.interface.Invalid – If any of the previous conditions does not hold.
Changed in version 5.0: If multiple methods or attributes are invalid, all such errors are collected and reported. Previously, only the first error was reported. As a special case, if only one such error is present, it is raised alone, like before.