diff options
Diffstat (limited to 'python/intex')
| -rw-r--r-- | python/intex/intex_scenarios.py | 76 |
1 files changed, 32 insertions, 44 deletions
diff --git a/python/intex/intex_scenarios.py b/python/intex/intex_scenarios.py index 79a52cc9..97d6dc8d 100644 --- a/python/intex/intex_scenarios.py +++ b/python/intex/intex_scenarios.py @@ -1,27 +1,28 @@ import csv import datetime -import json import logging import os -import os.path import psycopg2 import re import sys import yaml from csv import reader +from pathlib import Path + +BASE_DIR = Path(os.environ["BASE_DIR"]) # we do not want to depend on serenitas.utils logger = logging.getLogger(__name__) -pattern1 = re.compile("REINVEST\[\w+::REINV_TBA(\d)\]\[DEAL,(\d+)\]=.*$") +pattern1 = re.compile(r"REINVEST\[\w+::REINV_TBA(\d)\]\[DEAL,(\d+)\]=.*$") # reinv end date -pattern2 = re.compile("(STANDARD_VAR\[)\w+(::#REINV_END,\d+\]=)(\d.*$)") +pattern2 = re.compile(r"(STANDARD_VAR\[)\w+(::#REINV_END,\d+\]=)(\d.*$)") # reinvprices 1 -pattern3 = re.compile("STANDARD_VAR\[\w+::#PRICE100_TBA(\d),(\d+)\]=") -pattern5 = re.compile("STANDARD_VAR\[\w+::#REINVPCT_TBA(\d),(\d+)\]=") +pattern3 = re.compile(r"STANDARD_VAR\[\w+::#PRICE100_TBA(\d),(\d+)\]=") +pattern5 = re.compile(r"STANDARD_VAR\[\w+::#REINVPCT_TBA(\d),(\d+)\]=") pattern7 = re.compile("KINGS3") -pattern8 = re.compile("(#COLLATREINV_REINV_PCT_EXT\[)\w+(::\*\]\[DEAL,\d+\])=100") -pattern9 = re.compile("(?P<a>SEVERITY\[\w+,\d+\]=)mkt\(70\)") +pattern8 = re.compile(r"(#COLLATREINV_REINV_PCT_EXT\[)\w+(::\*\]\[DEAL,\d+\])=100") +pattern9 = re.compile(r"(?P<a>SEVERITY\[\w+,\d+\]=)mkt\(70\)") # we use 84 so that it's both dividable by 2 and 3 global_reinvfloatpercentage = 84 @@ -29,7 +30,7 @@ global_reinvfixedpercentage = 16 def get_reinv_assets(conn, dealname, workdate): - sqlstr = "SELECT * FROM et_historicaldealinfo(%s, %s) WHERE ReinvFlag IS TRUE" + sqlstr = "SELECT * FROM et_historicaldealinfo(%s, %s) WHERE ReinvFlag" d = {} with conn.cursor() as c: c.execute(sqlstr, (dealname, workdate)) @@ -71,21 +72,19 @@ def get_reinvenddate(conn, dealname, workdate): def generate_scenarios(workdate, dealname, conn): - prometheus = os.path.join(os.environ["BASE_DIR"], "Scenarios", "prometheus.sss") + prometheus = BASE_DIR / "Scenarios" / "prometheus.sss" n_scenarios = 100 - basedir = os.path.join( - os.environ["BASE_DIR"], "Scenarios", "Intex curves_" + workdate - ) + basedir = BASE_DIR / "Scenarios" / f"Intex curves_{workdate}" defaultedprice = get_recovery(conn, dealname, workdate) - replace = "\g<a>{0:.3f}".format(defaultedprice) + replace = r"\g<a>{0:.3f}".format(defaultedprice) try: - with open(os.path.join(basedir, "csv", dealname + ".config")) as fh: + with (basedir / "csv" / f"{dealname}.config").open() as fh: try: config = yaml.load(fh, Loader=yaml.FullLoader) except AttributeError: config = yaml.load(fh) except IOError: - logger.error("{0}: config file doesn't exist".format(dealname)) + logger.error(f"{dealname}: config file doesn't exist") return reinvflag = config["reinvflag"] if reinvflag: @@ -104,9 +103,7 @@ def generate_scenarios(workdate, dealname, conn): ) / n_float_assets try: - with open( - os.path.join(basedir, "csv", dealname + "-reinvprices.csv"), "r" - ) as fh: + with (basedir / "csv" / f"{dealname}-reinvprices.csv").open("r") as fh: dr = csv.DictReader(fh) reinvprices = {f: [] for f in dr.fieldnames} for line in dr: @@ -114,23 +111,20 @@ def generate_scenarios(workdate, dealname, conn): try: val = float(line[f]) except ValueError: - logger.error( - "Incorrect value in reinvprices for {}".format(dealname) - ) + logger.error("Incorrect value in reinvprices for {dealname}") else: - reinvprices[f].append("{0:.3f}".format(val)) + reinvprices[f].append(f"{val:.3f}") except IOError: reinvflag = False - sssfile = os.path.join(basedir, "sss", dealname + ".sss") - if not os.path.exists(os.path.join(basedir, "sss")): - os.makedirs(os.path.join(basedir, "sss")) - cdrscenarios = os.path.join(basedir, "csv", dealname + "-cdr.csv") - recoveryscenarios = os.path.join(basedir, "csv", dealname + "-recovery.csv") - fhsss = open(sssfile, "w") - fhcdr = open(cdrscenarios, "r") - fhrecovery = open(recoveryscenarios, "r") + sssfile = basedir / "sss" / f"{dealname}.sss" + (basedir / "sss").mkdir(exist_ok=True) + cdrscenarios = basedir / "csv" / f"{dealname}-cdr.csv" + recoveryscenarios = basedir / "csv" / f"{dealname}-recovery.csv" + fhsss = sssfile.open("w") + fhcdr = cdrscenarios.open("r") + fhrecovery = recoveryscenarios.open("r") csvcdr = reader(fhcdr) csvrecovery = reader(fhrecovery) cdrline = next(csvcdr) @@ -141,7 +135,7 @@ def generate_scenarios(workdate, dealname, conn): ) i = 1 - with open(prometheus) as fh: + with prometheus.open() as fh: for line in fh: line = line.rstrip() @@ -256,8 +250,7 @@ def generate_scenarios(workdate, dealname, conn): if __name__ == "__main__": - sys.path.append(".") - from utils.db import dbconn + from serenitas.utils.db import dbconn if len(sys.argv) > 1: workdate = sys.argv[1] @@ -267,16 +260,11 @@ if __name__ == "__main__": dealnames = sys.argv[2:] else: dealnames = [ - d.split(".")[0] - for d in os.listdir( - os.path.join( - os.environ["BASE_DIR"], - "Scenarios", - "Intex curves_" + workdate, - "csv", - ) - ) - if "RData" in d + d.stem + for d in ( + BASE_DIR / "Scenarios" / f"Intex curves_{workdate}" / "csv" + ).iterdir() + if d.suffix == ".RData" ] ET = dbconn("etdb") for dealname in dealnames: |
