GitXplorerGitXplorer
n

cauldron

public
2 stars
5 forks
8 issues

Commits

List of commits on branch master.
Unverified
d363bac763781bb2da18debfa0fdd4be28288b92

Adding pypi badge

nnerandell committed 9 years ago
Unverified
3af19e53597a223e6c62534fb64da058146adde1

Updating version number

nnerandell committed 9 years ago
Unverified
5fd5e650d4ae3bbf04da0f41bb3ef672d6622799

Merge pull request #6 from nerandell/f_confugurable_connection_params

nnerandell committed 9 years ago
Unverified
420006c2d87fa575984760cf75288607822e1bd3

Making connection params configurable by accepting params which using connect

nnerandell committed 9 years ago
Unverified
8e457bfdda59bfb0fa50ffdf0c60f256dbeba6b9

Adding more examples

nnerandell committed 9 years ago
Unverified
50211f9cd90420c56e6779d21faa11e7e5791059

Update README.rst

nnerandell committed 9 years ago

README

The README file for this repository.

Cauldron

.. image:: https://badge.fury.io/py/cauldron.svg :target: https://pypi.python.org/pypi/cauldron/1.0.14 | Cauldron is an asyncio based library that removes boilerplate code when using databases. Currently it supports using postgresql and redis.

Requirements

  • Python >= 3.4.3
  • asyncio_

.. _asyncio: https://pypi.python.org/pypi/asyncio

Installation

To install via pip:

.. code-block:: bash

$ pip install cauldron

To install from source:

.. code-block:: bash

$ 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.

.. _aiopg: https://github.com/aio-libs/aiopg .. _aioredis: https://github.com/aio-libs/aioredis

Sample code using aiopg:

.. code-block:: python

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:

.. code-block:: python

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


.. code-block:: python

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

.. code-block:: python

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)

.. _namedtuple: https://docs.python.org/3/library/collections.html#collections.namedtuple

Using dict cursor:

.. code-block:: python

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:

.. code-block:: python

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