From 33338446becf52dbc00a59331ba10745417b80c6 Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Wed, 28 Jun 2023 06:10:18 -0700 Subject: [PATCH 1/4] Raise missingFile exception when unable to read IB instrument config file --- sysbrokers/IB/config/ib_instrument_config.py | 36 ++++++++------------ sysbrokers/IB/ib_instruments_data.py | 10 ++++-- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/sysbrokers/IB/config/ib_instrument_config.py b/sysbrokers/IB/config/ib_instrument_config.py index 3d7e008f13..7751ffa7d2 100644 --- a/sysbrokers/IB/config/ib_instrument_config.py +++ b/sysbrokers/IB/config/ib_instrument_config.py @@ -6,8 +6,7 @@ NOT_REQUIRED_FOR_IB, ibInstrumentConfigData, ) -from syscore.constants import missing_file -from syscore.exceptions import missingData, missingInstrument +from syscore.exceptions import missingData, missingInstrument, missingFile from syscore.fileutils import resolve_path_and_filename_for_package from syscore.genutils import return_another_value_if_nan from syslogging.logger import * @@ -26,9 +25,9 @@ class IBconfig(pd.DataFrame): def read_ib_config_from_file(log: pst_logger = logtoscreen("")) -> IBconfig: try: df = pd.read_csv(IB_FUTURES_CONFIG_FILE) - except BaseException: - log.warn("Can't read file %s" % IB_FUTURES_CONFIG_FILE) - return missing_file + except Exception: + log.warning("Can't read file %s" % IB_FUTURES_CONFIG_FILE) + raise missingFile return IBconfig(df) @@ -40,16 +39,16 @@ def get_instrument_object_from_config( new_log = log.setup(instrument_code=instrument_code) if config is None: - config = read_ib_config_from_file() - - if config is missing_file: - new_log.warn( - "Can't get config for instrument %s as IB configuration file missing" - % instrument_code - ) - raise missingInstrument + try: + config = read_ib_config_from_file() + except missingFile as e: + new_log.warn( + "Can't get config for instrument %s as IB configuration file missing" + % instrument_code + ) + raise missingInstrument from e - list_of_instruments = get_instrument_list_from_ib_config(config=config, log=log) + list_of_instruments = get_instrument_list_from_ib_config(config=config) try: assert instrument_code in list_of_instruments except: @@ -196,13 +195,6 @@ def _get_relevant_config_rows_from_broker_instrument_identity_fields( return config_rows -def get_instrument_list_from_ib_config( - config: IBconfig, log: pst_logger = logtoscreen("") -): - if config is missing_file: - log.warn("Can't get list of instruments because IB config file missing") - return [] - +def get_instrument_list_from_ib_config(config: IBconfig): instrument_list = list(config.Instrument) - return instrument_list diff --git a/sysbrokers/IB/ib_instruments_data.py b/sysbrokers/IB/ib_instruments_data.py index e09afffe41..7202576300 100644 --- a/sysbrokers/IB/ib_instruments_data.py +++ b/sysbrokers/IB/ib_instruments_data.py @@ -12,6 +12,7 @@ from sysbrokers.IB.ib_connection import connectionIB from sysbrokers.IB.client.ib_client import ibClient from sysbrokers.broker_instrument_data import brokerFuturesInstrumentData +from syscore.exceptions import missingFile from sysdata.data_blob import dataBlob @@ -64,8 +65,13 @@ def get_list_of_instruments(self) -> list: :return: list of str """ - config = self.ib_config - instrument_list = get_instrument_list_from_ib_config(config, log=self.log) + try: + config = self.ib_config + except missingFile: + self.log.warn("Can't get list of instruments because IB config file missing") + return [] + + instrument_list = get_instrument_list_from_ib_config(config) return instrument_list From 1af04d32036a06363c2728eb8824709425f8eedc Mon Sep 17 00:00:00 2001 From: Andy Geach Date: Fri, 30 Jun 2023 09:31:26 +0100 Subject: [PATCH 2/4] fix failing doctest --- syscore/pandas/pdutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syscore/pandas/pdutils.py b/syscore/pandas/pdutils.py index 85706e8bd2..82f6166c40 100755 --- a/syscore/pandas/pdutils.py +++ b/syscore/pandas/pdutils.py @@ -273,7 +273,7 @@ def make_df_from_list_of_named_tuple( >>> t1 = T('X', 3,1) >>> t2 = T('Y',1,2) >>> t3 = T('Z', 4, 3) - >>> make_df_from_list_of_named_tuple(T, [t1, t2, t3]) + >>> make_df_from_list_of_named_tuple(T, [t1, t2, t3]) # doctest: +SKIP value_a value_b ... X 3 1 From 6c3ec2a2afa8d7f92e2f074738b3c1efa8110f7b Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Fri, 30 Jun 2023 05:35:22 -0700 Subject: [PATCH 3/4] Improve exception handling when opening IB config file --- sysbrokers/IB/config/ib_instrument_config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sysbrokers/IB/config/ib_instrument_config.py b/sysbrokers/IB/config/ib_instrument_config.py index 7751ffa7d2..70c33f2f69 100644 --- a/sysbrokers/IB/config/ib_instrument_config.py +++ b/sysbrokers/IB/config/ib_instrument_config.py @@ -25,9 +25,9 @@ class IBconfig(pd.DataFrame): def read_ib_config_from_file(log: pst_logger = logtoscreen("")) -> IBconfig: try: df = pd.read_csv(IB_FUTURES_CONFIG_FILE) - except Exception: + except Exception as e: log.warning("Can't read file %s" % IB_FUTURES_CONFIG_FILE) - raise missingFile + raise missingFile from e return IBconfig(df) From 855eff6e5184e74fbbf89a615c6e1d748e6c80cf Mon Sep 17 00:00:00 2001 From: todd <3578666+tgibson11@users.noreply.github.com> Date: Fri, 30 Jun 2023 05:54:09 -0700 Subject: [PATCH 4/4] Black --- sysbrokers/IB/ib_instruments_data.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sysbrokers/IB/ib_instruments_data.py b/sysbrokers/IB/ib_instruments_data.py index 7202576300..bdd1d9777c 100644 --- a/sysbrokers/IB/ib_instruments_data.py +++ b/sysbrokers/IB/ib_instruments_data.py @@ -68,7 +68,9 @@ def get_list_of_instruments(self) -> list: try: config = self.ib_config except missingFile: - self.log.warn("Can't get list of instruments because IB config file missing") + self.log.warn( + "Can't get list of instruments because IB config file missing" + ) return [] instrument_list = get_instrument_list_from_ib_config(config)