aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/experiments/test_asyncpg.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/python/experiments/test_asyncpg.py b/python/experiments/test_asyncpg.py
index 315c82c8..e3c2b92b 100644
--- a/python/experiments/test_asyncpg.py
+++ b/python/experiments/test_asyncpg.py
@@ -1,21 +1,36 @@
import asyncio
import asyncpg
import datetime
+import lz4
+from pyisda.curve import YieldCurve
+import uvloop
+from asyncio_extras import async_contextmanager
+@async_contextmanager
async def dbconn():
conn = await asyncpg.connect(user='serenitas_user', password='Serenitas1',
database='serenitasdb', host='debian')
- return conn
+ yield conn
+ await conn.close()
-async def get_singlenames_quotes_async(indexname, date):
- con = await dbconn()
+async def get_singlenames_quotes_async(con, indexname, date):
stmt = await con.prepare('SELECT * FROM curve_quotes($1, $2)')
async with con.transaction():
# Postgres requires non-scrollable cursors to be created
# and used in a transaction.
async for record in stmt.cursor(indexname, date):
print(record)
- await con.close()
-loop = asyncio.get_event_loop()
-loop.run_until_complete(get_singlenames_quotes_async("ig27", datetime.date.today()))
+async def get_curves(con, currency="USD", date=None):
+ async with con.transaction():
+ return {record['effective_date']: YieldCurve.from_bytes(lz4.block.decompress(record['curve']))
+ async for record in con.cursor("SELECT * FROM {}_curves".format(currency))}
+
+async def main():
+ async with dbconn() as con:
+ return await get_curves(con)
+
+if __name__ == "__main__":
+ asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
+ loop = asyncio.get_event_loop()
+ pomme = loop.run_until_complete(main())