Skip to main content

Utils to reduce boilerplate code

Project description

Cauldron is an asyncio based library that removes boilerplate code when using databases. Currently it supports using postgresql and redis.

Requirements

Installation

To install via pip:

$ pip install cauldron

To install from source:

$ git clone https://github.com/nerandell/cauldron
$ cd cauldron
$ python setup.py install

Usage

Cauldron currently supports postgres and redis. It uses aiopg and aioredis internally but removes a lot of boilerplate code that is usually written.

Sample code using aiopg:

import asyncio
from aiopg.pool import create_pool

dsn = 'dbname=jetty user=nick password=1234 host=localhost port=5432'


class UsePostgres():

    @classmethod
    def test_select(cls):
        pool = yield from create_pool(dsn)

        with (yield from pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute('SELECT 1')
            ret = yield from cur.fetchone()
            assert ret == (1,), ret

Using Cauldron:

from cauldron import PostgresStore

class UseCauldron(PostgresStore):
    @classmethod
    def test_select(cls):
        rows = yield from cls.raw_query('select 1')
        print(rows)

Other Examples

cauldron also supports using different cursors in a way that you have to write minimal code.

Using default cursor

from cauldron import PostgresStore

class UseCauldron(PostgresStore):
    @classmethod
    @cursor
    def test_select(cls, cur):
        rows = yield from cls.raw_sql('select * from users')
        print(rows)

Using namedtuple cursor

from cauldron import PostgresStore

class UseCauldron(PostgresStore):
    @classmethod
    @nt_cursor
    def test_select(cls, cur):
        rows = yield from cls.raw_sql('select * from users')
        print(rows)

Using dict cursor:

from cauldron import PostgresStore

class UseCauldron(PostgresStore):
    @classmethod
    @dict_cursor
    def test_select(cls, cur):
        rows = yield from cls.raw_sql('select * from users')
        print(rows)

cauldron also provides functionalities for common DB operations to make your code more readable

Inserting into db:

from cauldron import PostgresStore

class UseCauldron(PostgresStore):
    @classmethod
    def store_user(cls, username, password):
        insert_dict = {'username': username, 'password': password}
        yield from cls.insert('user_table', insert_dict)

License

cauldron is offered under the MIT license.

Source code

The latest developer version is available in a github repository: https://github.com/nerandell/cauldron

Project details


Download files

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

Source Distribution

cauldron-1.0.14.tar.gz (5.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page