11""":py:mod:`postgres` is a high-value abstraction over `psycopg2`_.
22
3- Features:
4-
5- - Use URLs instead of just connection strings.
6- - Connections are pooled.
7- - Calls are isolated in transactions.
8- - Cursors yield dictionaries.
9- - Text is unicode instead of bytestrings.
10-
113
124Installation
135------------
2012Tutorial
2113--------
2214
23- Instantiate a :py:class:`Postgres` object when your application starts::
15+ Instantiate a :py:class:`Postgres` object when your application starts:
2416
2517 >>> from postgres import Postgres
2618 >>> db = Postgres("postgres://jdoe@localhost/testdb")
2719
28- Use it to run SQL statements::
20+ Use it to run SQL statements:
2921
3022 >>> db.execute("CREATE TABLE foo (bar text)")
3123 >>> db.execute("INSERT INTO foo VALUES ('baz')")
4335 >>> db.fetchone("SELECT * FROM foo WHERE bar='blam'")
4436 None
4537
38+ Work with a cursor directly:
39+
40+ >>> with db.get_cursor('SELECT * FROM foo ORDER BY bar') as cursor:
41+ ... results = cursor.fetchall()
42+
4643
4744API
4845---
@@ -92,20 +89,28 @@ def url_to_dsn(url):
9289
9390
9491class Postgres (object ):
95- """Interact with a PostgreSQL datastore.
92+ """Interact with a ` PostgreSQL <http://www.postgresql.org/>`_ datastore.
9693
9794 This is the main object that :py:mod:`postgres` provides, and you should
9895 have one instance per process. Here are the arguments:
9996
100- - url - A " postgres://" url or a `PostgreSQL connection string
97+ - `` url`` - A `` postgres://`` URL or a `PostgreSQL connection string
10198 <http://www.postgresql.org/docs/current/static/libpq-connect.html>`_
10299
103- - minconn - The minimum size of the connection pool
100+ - ``minconn`` - The minimum size of the connection pool
101+
102+ - ``maxconn`` - The maximum size of the connection pool
103+
104+ When instantiated, this object creates a `thread-safe connection pool
105+ <http://initd.org/psycopg/docs/pool.html#psycopg2.pool.ThreadedConnectionPool>`_,
106+ which opens ``minconn`` connections immediately and up to ``maxconn``
107+ according to demand.
108+
109+ Features:
104110
105- - maxconn - The maximum size of the connection pool
111+ - Calls are isolated in transactions.
112+ - Get back unicode instead of bytestrings.
106113
107- The connection pool is a `psycopg2.pool.ThreadedConnectionPool
108- <http://initd.org/psycopg/docs/pool.html#psycopg2.pool.ThreadedConnectionPool>`_.
109114
110115 """
111116
@@ -119,19 +124,19 @@ def __init__(self, url, minconn=1, maxconn=10):
119124 )
120125
121126 def execute (self , * a , ** kw ):
122- """Execute the query and discard the results.
127+ """Execute the query and discard any results.
123128 """
124129 with self .get_cursor (* a , ** kw ):
125130 pass
126131
127132 def fetchone (self , * a , ** kw ):
128- """Execute the query and return a single result or None.
133+ """Execute the query and return a single result (``dict`` or `` None``) .
129134 """
130135 with self .get_cursor (* a , ** kw ) as cursor :
131136 return cursor .fetchone ()
132137
133138 def fetchall (self , * a , ** kw ):
134- """Execute the query and yield the results.
139+ """Execute the query and yield the results (``dict``) .
135140 """
136141 with self .get_cursor (* a , ** kw ) as cursor :
137142 for row in cursor :
0 commit comments