Skip to main content

An encapsulated persistance layer for Python

Project description

A Python encapsulated persistence layer for supporting many data access layers.

Very rough at the moment, only offers basic functionality.


### Context

The Context contains the current state for the active request. It should contain all meta data associated with the request such as active user and HTTP request parameters. It also provides access to Resources. When used in an HTTP framework typically one context is created at the start of the HTTP request and it ends before the HTTP response is sent.

When used with task managers such as Celery, the Context is created at the start of a task and ends before the task result is returned.

### DAL

The DAL is the “Data Access Layer”. The DAL is the registry for all Services. To make call a method on a Service, you start with the DAL.

result = dal.someservice.somemethod()

### Service

Services encapsulate business logic and data access. They are the Controller of MVC-like applications. Services can be nested within other services.


result = dal.someservice.subservice.somemethod()

### Resource

Resources are on-demand access to data backends such as SQL databases, key stores, and blob stores. Resources have a setup and teardown phase. Resources are only initialized and setup when they are first accessed within a context. This lazy loading ensures that only the Resources that are needed for a particular request are initialized.

The setup/teardown phases are particularly good for checking connections out from a connection pool and checking them back in at the end of the request.

def db_pool(context):
    conn = db.checkout_connection()
    yield conn

class ItemService(Service):
    def get_item(self, id):
        return self._data_manager.db.query(
            'SELECT * FROM table WHERE id={id}',

dm = DataManager()

with dm.dal() as dal:
    item = dal.items.get_item(1)

### Middleware

Middleware have a setup and teardown phase for each context. They are particularly useful for managing transactions or error handling.

Context Middleware may only see and modify the Context. With the Context, Context Middleware can gain access to Resources.

def transaction_middleware(context):
    trans = new_transaction()
        yield trans

dm = DataManager()


  • Methods that get an object should return None if an object can not be found.
  • Methods that rely on an object existing to work (such as create that relies on a parent object) should raise NotFound if the parent object does not exist.
  • All data access (SQL, MongoDB, Redis, S3, etc) must be done within a Service.


### Middleware vs Resource

A Resource is created on demand. It’s purpose is to create a needed resource for a request and clean it up when done. Middleware is ran on every context. It’s purpose is to do setup/teardown within the context.


To run tests you’ll need to install the test requirements:

pip install -e . pip install -r src/tests/requirements.txt

Run tests:

cd src/tests && py.test

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for polydatum, version 0.8.4rc1
Filename, size File type Python version Upload date Hashes
Filename, size polydatum-0.8.4rc1.tar.gz (10.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page