link.feature
stable
  • Tutorial
  • API documentation
link.feature
  • Docs »
  • link.feature 2.1 documentation
  • Edit on GitHub

Welcome to link.feature’s documentation!¶

link.feature is used to make classes providing features.

Check out the source code on Github.

License Development Status Latest release Supported Python versions Supported Python implementations Download format Build status Code test coverage Downloads Code Health

Installation¶

pip install link.feature

Contents¶

Tutorial¶

This package is used to make classes providing features. A feature is a class, providing an API using the class’ instance.

For example:

from link.feature import Feature
import json


class JSONFeature(Feature):
    name = 'json'

    def to_json(self):
        return json.dumps(self.obj.data)

Then, we just have to add the feature to the class, using the decorator:

from link.feature import addfeatures


@addfeatures([JSONFeature])
class MyClass(object):
    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)

        self.data = {'foo': 'bar'}

You can now get a list of all features provided by an instance:

from link.feature import getfeatures

obj = MyClass()
result = getfeatures(obj)

# result == [(obj, JSONFeature)]

NB: getfeatures() look for featured properties (a special kind of property, used to indicate that there may be resolvable features):

from link.feature import featuredprop

class Dummy(object):
    def __init__(self, *args, **kwargs):
        super(Dummy, self).__init__(*args, **kwargs)

        self._inner = MyClass()

    @featuredprop
    def inner(self):
        return self._inner

obj2 = Dummy()
result = getfeatures(obj2)

# result == [(obj2.inner, JSONFeature)]

Now, you can check if an object provides a feature, and instantiate it:

from link.feature import hasfeature, getfeature

assert hasfeature(obj, 'json')

try:
    f = getfeature(obj, 'json')

except AttributeError:
    print('obj has no feature json')

result = f.to_json()

API documentation¶

link.feature package¶

Module contents¶
class link.feature.Feature(obj, *args, **kwargs)¶

Bases: object

Base class for feature.

Parameters:obj (any) – Object providing this feature
name = None¶
class link.feature.featuredprop¶

Bases: property

Special property to allow traversal by the getfeatures() function.

link.feature.addfeatures(features)¶

Add features to a class.

Parameters:features (list) – Features to add
Returns:decorator
Return type:callable
link.feature.getfeatures(obj)¶

Get list of features provided by an object’s class.

Parameters:obj (any) – Object
Returns:List of provided features
Return type:list
link.feature.hasfeature(obj, name)¶

Check if object’s class provides a feature.

Parameters:
  • obj (any) – Object
  • name (str) – Feature’s name
Returns:

True if feature is provided

Return type:

bool

link.feature.getfeature(obj, name, *args, **kwargs)¶

Instantiate a feature.

Parameters:
  • obj (any) – Object
  • name (str) – Feature’s name
  • args (Iterable) – Positional arguments for feature’s constructor
  • kwargs (dict) – Keyword arguments for feature’s constructor
Returns:

Instance of feature

Return type:

Feature

Raises:

AttributeError – if feature is not provided by object’s class

Donating¶

Support via Liberapay

© Copyright 2016, David Delassus. Revision 72199e72.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: stable
Versions
latest
stable
Downloads
pdf
htmlzip
epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.