Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (testpypi.python.org).
Help us improve Python packaging - Donate today!

Thin wrapper around psycopg2 to provide helpers for Postgresql

Project Description

Psychopath is a thin wrapper around psycopg2 to provide helpers for Postgresql.

Features

  • Parse automatically the environment variable DATABASE_URL
  • Helpers to simplify SQL statement writing
  • SQL statement builders
  • Basic schema migration tool

Requirements

  • Python >= 3.4
  • psycopg2 >= 2.7
  • Postgresql 9.x

Installation

pip install psychopath

Running tests

pip install tox
tox

Usage

Initialize a new connection pool:

from psychopath import db

db.init('postgres://postgres:postgres@localhost/psychopath_tests', maximum_connections=10)

You could also use an environment variable DATABASE_URL to configure automatically the connection.

db.init()

Execute a SQL statement:

from psychopath import db

db.run('CREATE TABLE foobar (id serial PRIMARY KEY, value text)')

Get table records as namedtuple:

from psychopath import db

records = db.find_all('SELECT * FROM foobar ORDER BY id DESC')

for record in records:
    print(record.id)
    print(record.value)

Get only one record:

from psychopath import db

record = db.find_one('SELECT * FROM foobar')
print(record.value)

Get first column value:

from psychopath import db

print(db.first('SELECT value FROM foobar'))

Get numeric value:

from psychopath import db

print(db.count('SELECT count(*) FROM foobar'))

Insert a new record:

from psychopath import db

db.run('INSERT INTO integration_test (value) VALUES (%s)', ['something'])

db.run() is a wrapper around psycopg2 cursors.

Transactions:

from psychopath import db

with db.transaction() as t:
    t.execute('CREATE TABLE test_create_table (value text)')
    t.execute('DROP TABLE test_create_table')

Author

Frédéric Guillot

License

Psychopath is distributed under Apache 2.0 LICENSE.

Release History

Release History

This version
History Node

0.0.1.dev2

History Node

0.0.1.dev1

History Node

0.0.1.dev0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
psychopath-0.0.1.dev2.tar.gz (5.3 kB) Copy SHA256 Checksum SHA256 Source May 14, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting