findig.data_model — Data access for Findig applications

This module defines data models, which implement data access for a particular resource. In a typical Findig application, each resource has an AbstractDataModel attached which has functions defined implementing the data operations that are supported by that resource.

By default, Resources have a DataModel attached, but this can be replaced with any concrete AbstractDataModel.

class findig.data_model.AbstractDataModel[source]

Bases: collections.abc.Mapping

An object responsible for managing the data for a specific resource. Essentially, it is a mapping of data operations to the functions that perform.

The following data operations (and their signatures) are supported:

  • read()

    Retrieve the data for the resource.

  • write(data)

    Replace the resource’s existing data with the new data. If the resource doesn’t exist yet, create it.

  • delete()

    Completely obliterate a resource’s data; in general the resource should be thought to no longer exist after this occurs.

  • make(data)

    Create a child resource.

    Returns:A mapping that can identify the created child (i.e., a key).

To implement this abstract base class, do either of the following:

  • Implement methods on your subclass with the names of the data operations you want your model to support. For example, the following model implements read and write actions:

    class ReadWriteModel(AbstractDataModel):
        def read():
            '''Perform backend read.'''
    
        def write(new_data):
            '''Perform backend write.'''
    
  • Re-implement the mapping interface on your subclasses, such that instances will map from a data operation (str) to a function that implements it. This requires implementing __iter__, __len__ and __getitem__ at a minimum. For an example, take a look at the source code for this class.

class findig.data_model.DataModel[source]

Bases: findig.data_model.AbstractDataModel, collections.abc.MutableMapping

A generic, concrete implementation of AbstractDataModel

This class is implemented as a mutable mapping, so implementation functions for data operations can be set, accessed and deleted using the mapping interface:

>>> dm = DataModel()
>>> dm['read'] = lambda: ()
>>> 'read' in dm
True

Also, be aware that data model instances can be called to return a decorator for a specific data operation:

>>> @dm('write')
... def write_some_data(data):
...    pass
...
>>> dm['write'] == write_some_data
True