interfaces

Simple decorator implementation of an interface
Download

interfaces Ranking & Summary

Advertisement

  • Rating:
  • License:
  • The Apache License 2.0
  • Price:
  • FREE
  • Publisher Name:
  • Josh Marshall
  • Publisher web site:
  • http://github.com/joshmarshall/

interfaces Tags


interfaces Description

interfaces is a Python library, a trivial implementation of an interface in Python, with the following aspects / features:* It fails at import time, not at construction, so you know immediately when you have a problem.* It's quite simple (very few LOC) and lenient where it counts* It exclusively uses decorators, so...* It does not require inheritance (reducing 'forced' subclassing)* It does not enforce any typing checks* It is intended to 'enhance' duck typing by avoiding common pitfalls (forgot to implement something on your fake duck class, overwrote something fundamental, etc.)UsageGiven a simple interface like:python@interfaces.defineclass DuckInterface(object): @interfaces.require def quack(self): """All Ducks must implement a 'quack' method.""" pass...the following will raise a MissingRequiredAttribute exception at import time:python@interfaces.define(object)class Silent(object): # no quack methodpassThis, however works:python@interfaces.implement(DuckInterface)class Tree(object): @interfaces.final def quack(self): return "the tree appears to quack."tree = Tree()tree.quack()Additionally, if you are interested in using the `final` method decorator outside of an interface, you can do so using the `strict` class decorator around any class you want to check:python@interfaces.strictclass BaseClass(object): @interfaces.final class method(self): return "Old functionality!"# the following will raise an exception at import:@interfaces.strictclass SubClass(object): class method(self): return "New functionality!"Product's homepage


interfaces Related Software