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 details)

Uploaded Source

File details

Details for the file cauldron-1.0.14.tar.gz.

File metadata

  • Download URL: cauldron-1.0.14.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cauldron-1.0.14.tar.gz
Algorithm Hash digest
SHA256 2f548946fdefae11a82ff95426e2061989c4a9a97e518c769efb106b74842606
MD5 ccbc1fc52932362bde677722f529f491
BLAKE2b-256 c06a35aa62b60e752c8fd207dadeefcc6ebe95a9ac3aad8114451d7af4b1aa8c

See more details on using hashes here.

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