Skip to content

Commit 87f0167

Browse files
committed
Add an example on how to decode numerics as floats
1 parent 3ca5e3b commit 87f0167

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

asyncpg/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ async def connect(dsn=None, *,
14271427
:param passfile:
14281428
the name of the file used to store passwords
14291429
(defaults to ``~/.pgpass``, or ``%APPDATA%\postgresql\pgpass.conf``
1430-
on Windows)
1430+
on Windows)
14311431
14321432
:param loop:
14331433
An asyncio event loop instance. If ``None``, the default

docs/usage.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,37 @@ will work.
259259
asyncio.get_event_loop().run_until_complete(main())
260260
261261
262+
Example: decoding numeric columns as floats
263+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264+
265+
By default asyncpg decodes numeric columns as Python
266+
:class:`Decimal <python:decimal.Decimal>` instances. The example below
267+
shows how to instruct asyncpg to use floats instead.
268+
269+
.. code-block:: python
270+
271+
import asyncio
272+
import asyncpg
273+
274+
275+
async def main():
276+
conn = await asyncpg.connect()
277+
278+
try:
279+
await conn.set_type_codec(
280+
'numeric', encoder=str, decoder=float,
281+
schema='pg_catalog', format='text'
282+
)
283+
284+
res = await conn.fetchval("SELECT $1::numeric", 11.123)
285+
print(res, type(res))
286+
287+
finally:
288+
await conn.close()
289+
290+
asyncio.get_event_loop().run_until_complete(main())
291+
292+
262293
Transactions
263294
------------
264295

0 commit comments

Comments
 (0)