Skip to content

Commit 78066f0

Browse files
committed
Initial documentation pass
1 parent 14f906f commit 78066f0

File tree

7 files changed

+246
-0
lines changed

7 files changed

+246
-0
lines changed

asyncpg/connection.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,40 @@ def cursor(self, query, *args, prefetch=None, timeout=None):
104104
return cursor.CursorFactory(self, query, None, args, prefetch, timeout)
105105

106106
async def prepare(self, query, *, timeout=None):
107+
"""Create a *prepared statement* for the specified query.
108+
109+
:param str query: Text of the query to create a prepared statement for.
110+
:param int timeout: Optional timeout value in seconds.
111+
112+
:return: A :class:`PreparedStatement` instance.
113+
"""
107114
stmt = await self._get_statement(query, timeout)
108115
return prepared_stmt.PreparedStatement(self, query, stmt)
109116

110117
async def fetch(self, query, *args, timeout=None):
118+
"""Run a query and return the results as a list of :class:`Record`.
119+
120+
:param str query: Query text
121+
:param args: Query arguments
122+
:param int timeout: Optional timeout value in seconds.
123+
124+
:return: A list of :class:`Record` instances.
125+
"""
111126
stmt = await self._get_statement(query, timeout)
112127
protocol = self._protocol
113128
data = await protocol.bind_execute(stmt, args, '', 0, False, timeout)
114129
return data
115130

116131
async def fetchval(self, query, *args, column=0, timeout=None):
132+
"""Run a query and return the value of the specified column of the first row.
133+
134+
:param str query: Query text
135+
:param args: Query arguments
136+
:param int timeout: Optional column index (defaults to 0).
137+
:param int timeout: Optional timeout value in seconds.
138+
139+
:return: The value of the specified column of the first record.
140+
"""
117141
stmt = await self._get_statement(query, timeout)
118142
protocol = self._protocol
119143
data = await protocol.bind_execute(stmt, args, '', 1, False, timeout)
@@ -122,6 +146,14 @@ async def fetchval(self, query, *args, column=0, timeout=None):
122146
return data[0][column]
123147

124148
async def fetchrow(self, query, *args, timeout=None):
149+
"""Run a query and return the first row.
150+
151+
:param str query: Query text
152+
:param args: Query arguments
153+
:param int timeout: Optional timeout value in seconds.
154+
155+
:return: The first row as a :class:`Record` instance.
156+
"""
125157
stmt = await self._get_statement(query, timeout)
126158
protocol = self._protocol
127159
data = await protocol.bind_execute(stmt, args, '', 1, False, timeout)
@@ -191,9 +223,15 @@ async def set_builtin_type_codec(self, typename, *,
191223
oid, typename, schema, 'scalar', codec_name)
192224

193225
def is_closed(self):
226+
"""Return ``True`` if the connection is closed, ``False`` otherwise.
227+
228+
:return bool: ``True`` if the connection is closed, ``False``
229+
otherwise.
230+
"""
194231
return not self._protocol.is_connected() or self._aborted
195232

196233
async def close(self):
234+
"""Close the connection gracefully."""
197235
if self.is_closed():
198236
return
199237
self._close_stmts()
@@ -202,6 +240,7 @@ async def close(self):
202240
await protocol.close()
203241

204242
def terminate(self):
243+
"""Terminate the connection without waiting for pending data."""
205244
self._close_stmts()
206245
self._aborted = True
207246
self._protocol.abort()

asyncpg/prepared_stmt.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ def __init__(self, connection, query, state):
2323
self._last_status = None
2424

2525
def get_query(self):
26+
"""Return the text of the query for this prepared statement."""
2627
return self._query
2728

2829
def get_statusmsg(self):
30+
"""Return the status of the executed command."""
2931
if self._last_status is None:
3032
return self._last_status
3133
return self._last_status.decode()
@@ -76,6 +78,14 @@ async def explain(self, *args, analyze=False):
7678
return json.loads(data)
7779

7880
async def fetch(self, *args, timeout=None):
81+
"""Execute the statement and return the results as a list of :class:`Record`.
82+
83+
:param str query: Query text
84+
:param args: Query arguments
85+
:param int timeout: Optional timeout value in seconds.
86+
87+
:return: A list of :class:`Record` instances.
88+
"""
7989
self.__check_open()
8090
protocol = self._connection._protocol
8191
data, status, _ = await protocol.bind_execute(
@@ -84,6 +94,15 @@ async def fetch(self, *args, timeout=None):
8494
return data
8595

8696
async def fetchval(self, *args, column=0, timeout=None):
97+
r"""Execute the statement and return the value of the specified \
98+
column of the first row.
99+
100+
:param args: Query arguments
101+
:param int timeout: Optional column index (defaults to 0).
102+
:param int timeout: Optional timeout value in seconds.
103+
104+
:return: The value of the specified column of the first record.
105+
"""
87106
self.__check_open()
88107
protocol = self._connection._protocol
89108
data, status, _ = await protocol.bind_execute(
@@ -94,6 +113,14 @@ async def fetchval(self, *args, column=0, timeout=None):
94113
return data[0][column]
95114

96115
async def fetchrow(self, *args, timeout=None):
116+
"""Execute the statement and return the first row.
117+
118+
:param str query: Query text
119+
:param args: Query arguments
120+
:param int timeout: Optional timeout value in seconds.
121+
122+
:return: The first row as a :class:`Record` instance.
123+
"""
97124
self.__check_open()
98125
protocol = self._connection._protocol
99126
data, status, _ = await protocol.bind_execute(

docs/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
_build
2+
_static
3+
_templates

docs/api/index.rst

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.. _asyncpg-api-reference:
2+
3+
=============
4+
API Reference
5+
=============
6+
7+
.. module:: asyncpg
8+
:synopsis: A fast PostgreSQL Database Client Library for Python/asyncio
9+
.. currentmodule:: asyncpg
10+
11+
12+
.. _asyncpg-api-connection:
13+
14+
Connection
15+
==========
16+
17+
.. coroutinefunction:: connect(dsn=None, *, host=None, port=None, \
18+
user=None, password=None, \
19+
database=None, loop=None, timeout=60, \
20+
statement_cache_size=100, \
21+
command_timeout=None, \
22+
**opts)
23+
24+
Establish a connection to a :term:`PostgreSQL` server and return a new
25+
:class:`Connection` object.
26+
27+
:param dsn: Connection arguments specified using as a single string in the
28+
following format:
29+
``postgres://user:pass@host:port/database?option=value``
30+
31+
:param host: database host address or a path to the directory containing
32+
database server UNIX socket (defaults to the default UNIX
33+
socket, or the value of the ``PGHOST`` environment variable,
34+
if set).
35+
36+
:param port: connection port number (defaults to 5432, or the value of
37+
the ``PGPORT`` environment variable, if set)
38+
39+
:param user: the name of the database role used for authentication
40+
(defaults to the name of the effective user of the process
41+
making the connection, or the value of ``PGUSER`` environment
42+
variable, if set)
43+
44+
:param password: password used for authentication
45+
46+
:param loop: An asyncio event loop instance. If ``None``, the default
47+
event loop will be used.
48+
49+
:param float timeout: connection timeout (in seconds, defaults to 60
50+
seconds).
51+
52+
:param float statement_timeout: the default timeout for operations on
53+
this connection (the default is no timeout).
54+
55+
:param int statement_cache_size: the size of prepared statement LRU cache
56+
(defaults to 100).
57+
58+
:returns: :class:`Connection` instance.
59+
60+
61+
.. autoclass:: asyncpg.connection.Connection
62+
:members:
63+
64+
65+
.. autoclass:: asyncpg.prepared_stmt.PreparedStatement
66+
:members:

docs/conf.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
3+
import alabaster
4+
import os
5+
import re
6+
import sys
7+
8+
sys.path.insert(0, os.path.abspath('..'))
9+
10+
with open(os.path.abspath('../setup.py'), 'rt') as f:
11+
_m = re.search(r'''version=(?P<q>'|")(?P<ver>[\d\.]+)(?P=q)''', f.read())
12+
if not _m:
13+
raise RuntimeError('unable to read the version from setup.py')
14+
version = _m.group('ver')
15+
16+
17+
# -- General configuration ------------------------------------------------
18+
19+
extensions = [
20+
'sphinx.ext.autodoc',
21+
'sphinxcontrib.asyncio',
22+
'alabaster',
23+
]
24+
templates_path = ['_templates']
25+
source_suffix = '.rst'
26+
master_doc = 'index'
27+
project = 'asyncpg'
28+
copyright = '2016-present, the ayncpg authors and contributors'
29+
author = '<See AUTHORS file>'
30+
release = version
31+
language = None
32+
exclude_patterns = ['_build']
33+
pygments_style = 'sphinx'
34+
todo_include_todos = False
35+
36+
37+
# -- Options for HTML output ----------------------------------------------
38+
39+
html_theme = 'alabaster'
40+
html_theme_options = {
41+
'description': 'asyncpg is a fast PostgreSQL client library for the '
42+
'Python asyncio framework',
43+
'show_powered_by': False,
44+
}
45+
html_theme_path = [alabaster.get_path()]
46+
html_title = 'asyncpg Documentation'
47+
html_short_title = 'asyncpg'
48+
html_static_path = []
49+
html_sidebars = {
50+
'**': [
51+
'about.html',
52+
'navigation.html',
53+
]
54+
}
55+
html_show_sourcelink = False
56+
html_show_sphinx = False
57+
html_show_copyright = True
58+
htmlhelp_basename = 'asyncpgdoc'
59+
60+
61+
# -- Options for LaTeX output ---------------------------------------------
62+
63+
latex_elements = {}
64+
65+
latex_documents = [
66+
(master_doc, 'asyncpg.tex', 'asyncpg Documentation',
67+
author, 'manual'),
68+
]
69+
70+
71+
# -- Options for manual page output ---------------------------------------
72+
73+
man_pages = [
74+
(master_doc, 'asyncpg', 'asyncpg Documentation',
75+
[author], 1)
76+
]
77+
78+
79+
# -- Options for Texinfo output -------------------------------------------
80+
81+
texinfo_documents = [
82+
(master_doc, 'asyncpg', 'asyncpg Documentation',
83+
author, 'asyncpg',
84+
'asyncpg is a fast PostgreSQL client library for the '
85+
'Python asyncio framework',
86+
'Miscellaneous'),
87+
]

docs/index.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. image:: https://travis-ci.org/MagicStack/asyncpg.svg?branch=master
2+
:target: https://travis-ci.org/MagicStack/asyncpg
3+
4+
.. image:: https://img.shields.io/pypi/status/asyncpg.svg?maxAge=2592000?style=plastic
5+
:target: https://pypi.python.org/pypi/asyncpg
6+
7+
8+
asyncpg
9+
=======
10+
11+
**asyncpg** is a database interface library designed specifically for
12+
PostgreSQL and Python/asyncio. asyncpg is an efficient, clean implementation
13+
of PostgreSQL server binary protocol for use with Python's ``asyncio``
14+
framework.
15+
16+
17+
Contents
18+
--------
19+
20+
.. toctree::
21+
:maxdepth: 1
22+
23+
api/index

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sphinxcontrib-asyncio

0 commit comments

Comments
 (0)