aboutsummaryrefslogtreecommitdiffstats
path: root/python/markit
diff options
context:
space:
mode:
Diffstat (limited to 'python/markit')
-rw-r--r--python/markit/rates.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/python/markit/rates.py b/python/markit/rates.py
index 261e3db5..e3bd6469 100644
--- a/python/markit/rates.py
+++ b/python/markit/rates.py
@@ -4,6 +4,7 @@ from io import BytesIO
import lz4
import os
import pandas as pd
+from psycopg2 import sql
import requests
import xml.etree.ElementTree as ET
import zipfile
@@ -16,9 +17,9 @@ def downloadMarkitIRData(download_date=datetime.date.today(),
## T+1 rates are published in the evening
effective_date = download_date + datetime.timedelta(days=1)
basedir = os.path.join(os.environ['DATA_DIR'], "Yield Curves")
- filename = "InterestRates_{0}_{1:%Y%m%d}".format(currency, effective_date)
+ filename = f"InterestRates_{currency}_{effective_date:%Y%m%d}"
if not os.path.exists(os.path.join(basedir, filename + '.xml')):
- r = requests.get('http://www.markit.com/news/{0}.zip'.format(filename))
+ r = requests.get(f'http://www.markit.com/news/{filename}.zip')
if "zip" in r.headers['content-type']:
with zipfile.ZipFile(BytesIO(r.content)) as z:
z.extractall(path = os.path.join(os.environ['DATA_DIR'], "Yield Curves"))
@@ -36,15 +37,16 @@ def downloadMarkitIRData(download_date=datetime.date.today(),
'effectiveasof': pd.Timestamp(effectiveasof).date()}
ql_yc = YC(currency=currency, MarkitData=MarkitData, evaluation_date=MarkitData['effectiveasof'])
jp_yc = ql_to_jp(ql_yc)
- sql_str = "INSERT INTO {}_curves VALUES(%s, %s) ON CONFLICT DO NOTHING".format(currency)
+ sql_str = f"INSERT INTO {currency}_curves VALUES(%s, %s) ON CONFLICT DO NOTHING"
with conn.cursor() as c:
c.execute(sql_str, (MarkitData['effectiveasof'],
lz4.block.compress(jp_yc.__getstate__())))
instruments = MarkitData['deposits'] + MarkitData['swaps']
- names = ",".join(['"{}"'.format(r[0]) for r in instruments])
- values = ",".join(["%s"] * (len(instruments) + 1)) # +1 for effective_date
- insert_str = ("INSERT INTO {0}_rates(effective_date, {1}) VALUES({2}) ON CONFLICT DO NOTHING".
- format(currency, names, values))
+ names = sql.SQL(", ").join([sql.Identifier(r[0]) for r in instruments])
+ values = sql.SQL(", ").join(sql.Placeholder() * (len(instruments) + 1)) # +1 for effective_date
+ insert_str = (sql.SQL(f"INSERT INTO {currency}_rates(effective_date, {{}}) VALUES({{}}) " \
+ "ON CONFLICT DO NOTHING").
+ format(names, values))
with conn.cursor() as c:
c.execute(insert_str, [MarkitData['effectiveasof']] +[r[1] for r in instruments])
conn.commit()