From 31604a49f8c4c42652716d68b684b41f32c3d3b3 Mon Sep 17 00:00:00 2001 From: Rafael Elter Date: Thu, 25 Nov 2021 14:44:24 -0300 Subject: [PATCH 1/4] collect_many_bds empty data --- src/blp/blp.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/blp/blp.py b/src/blp/blp.py index 7ace55a..0ac26f9 100644 --- a/src/blp/blp.py +++ b/src/blp/blp.py @@ -886,9 +886,10 @@ def collect_many_to_bds(self, responses) -> Dict: sec_dict = res.get(security, {}) for field in response["data"]: data = response["data"][field] - rows = sec_dict.get(field, []) - rows.extend(data) - sec_dict[field] = rows + if data: + rows = sec_dict.get(field, []) + rows.extend(data) + sec_dict[field] = rows res[security] = sec_dict for s in res: for f in res[s]: From 1fcd020b6d6cce1670397f2df166e2226d3c3aa3 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 9 Jan 2022 17:14:22 -0500 Subject: [PATCH 2/4] ENH: Black formatting --- src/blp/blp.py | 8 +----- src/blp/test/test_blp.py | 56 +++++++--------------------------------- 2 files changed, 11 insertions(+), 53 deletions(-) diff --git a/src/blp/blp.py b/src/blp/blp.py index 0ac26f9..b7b2add 100644 --- a/src/blp/blp.py +++ b/src/blp/blp.py @@ -925,13 +925,7 @@ def bdib( """ query = create_intraday_bar_query( - security, - event_type, - interval, - start_datetime, - end_datetime, - overrides, - options, + security, event_type, interval, start_datetime, end_datetime, overrides, options, ) res = self.query(query, self.parser, self.collect_to_bdib) dfs = [] diff --git a/src/blp/test/test_blp.py b/src/blp/test/test_blp.py index d6b31e6..9e39b86 100644 --- a/src/blp/test/test_blp.py +++ b/src/blp/test/test_blp.py @@ -60,13 +60,7 @@ class MockBlpQuery(blp.BlpQuery): } def __init__( - self, - host="localhost", - port=8194, - timeout=10000, - parser=None, - cache_data=None, - **kwargs, + self, host="localhost", port=8194, timeout=10000, parser=None, cache_data=None, **kwargs, ): self._cache = cache_data super().__init__(host=host, port=port, timeout=timeout, parser=parser, **kwargs) @@ -198,8 +192,7 @@ def assert_info_equal(data, exp_data, ignore_overrides=True): d["message"]["element"]["fieldResponse"], key=lambda x: x["fieldData"]["id"] ) ed["message"]["element"]["fieldResponse"] = sorted( - ed["message"]["element"]["fieldResponse"], - key=lambda x: x["fieldData"]["id"], + ed["message"]["element"]["fieldResponse"], key=lambda x: x["fieldData"]["id"], ) if ignore_overrides: for di in [d, ed]: @@ -2451,16 +2444,8 @@ def collector_params(): return { "ids": ["collect_many_to_bds_single_bulk", "collect_many_to_bds_with_multi_bulk"], "argvalues": [ - ( - bulk_reference_parsed_data, - blp.BlpQuery().collect_many_to_bds, - bulk_reference_collected_exp, - ), - ( - bulk_reference_parsed_data_multi, - blp.BlpQuery().collect_many_to_bds, - bulk_reference_collected_multi_exp, - ), + (bulk_reference_parsed_data, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_exp,), + (bulk_reference_parsed_data_multi, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_multi_exp,), ], } @@ -2489,12 +2474,7 @@ def test_bdh_infer(): host = HOST port = PORT bquery = blp.BlpQuery(host, port, timeout=QUERY_TIMEOUT, field_column_map={}).start() - df = bquery.bdh( - ["SPY US Equity"], - ["PX_LAST_ACTUAL"], - start_date="20180102", - end_date="20180103", - ) + df = bquery.bdh(["SPY US Equity"], ["PX_LAST_ACTUAL"], start_date="20180102", end_date="20180103",) df_expect = pandas.DataFrame( [(TS(2018, 1, 2), "SPY US Equity", 268.77), (TS(2018, 1, 3), "SPY US Equity", 270.47)], columns=["date", "security", "PX_LAST_ACTUAL"], @@ -2509,16 +2489,10 @@ def test_bdh_coerce_none(): bquery = blp.BlpQuery( host, port, timeout=QUERY_TIMEOUT, field_column_map={"PX_VOLUME": lambda x: pandas.Series(x, dtype="float64")} ).start() - df = bquery.bdh( - ["DOESCRUD Index"], - ["PX_VOLUME", "PX_LAST"], - start_date="20180105", - end_date="20180105", - ) + df = bquery.bdh(["DOESCRUD Index"], ["PX_VOLUME", "PX_LAST"], start_date="20180105", end_date="20180105",) dtypes = {"PX_VOLUME": numpy.dtype("float64")} df_expect = pandas.DataFrame( - [(TS(2018, 1, 5), "DOESCRUD Index", None, 419515.0)], - columns=["date", "security", "PX_VOLUME", "PX_LAST"], + [(TS(2018, 1, 5), "DOESCRUD Index", None, 419515.0)], columns=["date", "security", "PX_VOLUME", "PX_LAST"], ).astype(dtypes) assert_frame_equal(df, df_expect) @@ -2550,8 +2524,7 @@ def test_bdp_not_applicable(bquery): df = bquery.bdp(["RSF82 Comdty", "PLG18 Comdty"], ["ID_EXCH_SYMBOL"]) dtypes = {"security": numpy.dtype("O"), "ID_EXCH_SYMBOL": numpy.dtype("O")} df_expect = pandas.DataFrame( - [["RSF82 Comdty", None], ["PLG18 Comdty", "PL"]], - columns=["security", "ID_EXCH_SYMBOL"], + [["RSF82 Comdty", None], ["PLG18 Comdty", "PL"]], columns=["security", "ID_EXCH_SYMBOL"], ).astype(dtypes) assert_frame_equal(df, df_expect) @@ -2607,12 +2580,7 @@ def bdit_params(): "ids": ["bdit", "bdit_with_condition_codes"], "argvalues": [ ("CL1 Comdty", ["TRADE"], None, dtype_expect), - ( - "CL1 Comdty", - ["TRADE"], - [("includeConditionCodes", True)], - dtype_expect_cc, - ), + ("CL1 Comdty", ["TRADE"], [("includeConditionCodes", True)], dtype_expect_cc,), ], } @@ -2645,11 +2613,7 @@ def test_create_historical_query(): } } res = blp.create_historical_query( - ["SPY US Equity"], - ["PX_LAST"], - "20190101", - "20190110", - options=[("periodicitySelection", "DAILY")], + ["SPY US Equity"], ["PX_LAST"], "20190101", "20190110", options=[("periodicitySelection", "DAILY")], ) assert res == exp_res From dfe3c4af9eadb6b7ea1c80bc783a5b0c630ba4c8 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 9 Jan 2022 17:44:04 -0500 Subject: [PATCH 3/4] TST: Update bulk collector test --- src/blp/test/test_blp.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/blp/test/test_blp.py b/src/blp/test/test_blp.py index 9e39b86..7027e9c 100644 --- a/src/blp/test/test_blp.py +++ b/src/blp/test/test_blp.py @@ -2441,11 +2441,22 @@ def collector_params(): "INDX_MWEIGHT_HIST": pandas.DataFrame({"Index Member": ["BON9", "C N9"], "Percent Weight": [2.89, 5.32]}) } } + # test for https://github.com/matthewgilbert/blp/pull/8 + bulk_reference_parsed_data_empty = [ + {"security": "TRAD3 BZ Equity", "fields": ["DVD_HIST_ALL"], "data": {"DVD_HIST_ALL": None}} + ] + bulk_reference_empty_collected_exp = {"TRAD3 BZ Equity": {}} + return { - "ids": ["collect_many_to_bds_single_bulk", "collect_many_to_bds_with_multi_bulk"], + "ids": [ + "collect_many_to_bds_single_bulk", + "collect_many_to_bds_with_multi_bulk", + "collect_many_empty_to_bds_single_bulk", + ], "argvalues": [ - (bulk_reference_parsed_data, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_exp,), - (bulk_reference_parsed_data_multi, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_multi_exp,), + (bulk_reference_parsed_data, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_exp), + (bulk_reference_parsed_data_multi, blp.BlpQuery().collect_many_to_bds, bulk_reference_collected_multi_exp), + (bulk_reference_parsed_data_empty, blp.BlpQuery().collect_many_to_bds, bulk_reference_empty_collected_exp), ], } From 2817dda8716de3733345a13bcaa8ece62b316ae1 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 9 Jan 2022 18:04:30 -0500 Subject: [PATCH 4/4] TST: Black version bump fixes --- src/blp/blp.py | 8 +++++++- src/blp/test/test_blp.py | 44 ++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/blp/blp.py b/src/blp/blp.py index b7b2add..0ac26f9 100644 --- a/src/blp/blp.py +++ b/src/blp/blp.py @@ -925,7 +925,13 @@ def bdib( """ query = create_intraday_bar_query( - security, event_type, interval, start_datetime, end_datetime, overrides, options, + security, + event_type, + interval, + start_datetime, + end_datetime, + overrides, + options, ) res = self.query(query, self.parser, self.collect_to_bdib) dfs = [] diff --git a/src/blp/test/test_blp.py b/src/blp/test/test_blp.py index 7027e9c..acf1ca6 100644 --- a/src/blp/test/test_blp.py +++ b/src/blp/test/test_blp.py @@ -60,7 +60,13 @@ class MockBlpQuery(blp.BlpQuery): } def __init__( - self, host="localhost", port=8194, timeout=10000, parser=None, cache_data=None, **kwargs, + self, + host="localhost", + port=8194, + timeout=10000, + parser=None, + cache_data=None, + **kwargs, ): self._cache = cache_data super().__init__(host=host, port=port, timeout=timeout, parser=parser, **kwargs) @@ -192,7 +198,8 @@ def assert_info_equal(data, exp_data, ignore_overrides=True): d["message"]["element"]["fieldResponse"], key=lambda x: x["fieldData"]["id"] ) ed["message"]["element"]["fieldResponse"] = sorted( - ed["message"]["element"]["fieldResponse"], key=lambda x: x["fieldData"]["id"], + ed["message"]["element"]["fieldResponse"], + key=lambda x: x["fieldData"]["id"], ) if ignore_overrides: for di in [d, ed]: @@ -2485,7 +2492,12 @@ def test_bdh_infer(): host = HOST port = PORT bquery = blp.BlpQuery(host, port, timeout=QUERY_TIMEOUT, field_column_map={}).start() - df = bquery.bdh(["SPY US Equity"], ["PX_LAST_ACTUAL"], start_date="20180102", end_date="20180103",) + df = bquery.bdh( + ["SPY US Equity"], + ["PX_LAST_ACTUAL"], + start_date="20180102", + end_date="20180103", + ) df_expect = pandas.DataFrame( [(TS(2018, 1, 2), "SPY US Equity", 268.77), (TS(2018, 1, 3), "SPY US Equity", 270.47)], columns=["date", "security", "PX_LAST_ACTUAL"], @@ -2500,10 +2512,16 @@ def test_bdh_coerce_none(): bquery = blp.BlpQuery( host, port, timeout=QUERY_TIMEOUT, field_column_map={"PX_VOLUME": lambda x: pandas.Series(x, dtype="float64")} ).start() - df = bquery.bdh(["DOESCRUD Index"], ["PX_VOLUME", "PX_LAST"], start_date="20180105", end_date="20180105",) + df = bquery.bdh( + ["DOESCRUD Index"], + ["PX_VOLUME", "PX_LAST"], + start_date="20180105", + end_date="20180105", + ) dtypes = {"PX_VOLUME": numpy.dtype("float64")} df_expect = pandas.DataFrame( - [(TS(2018, 1, 5), "DOESCRUD Index", None, 419515.0)], columns=["date", "security", "PX_VOLUME", "PX_LAST"], + [(TS(2018, 1, 5), "DOESCRUD Index", None, 419515.0)], + columns=["date", "security", "PX_VOLUME", "PX_LAST"], ).astype(dtypes) assert_frame_equal(df, df_expect) @@ -2535,7 +2553,8 @@ def test_bdp_not_applicable(bquery): df = bquery.bdp(["RSF82 Comdty", "PLG18 Comdty"], ["ID_EXCH_SYMBOL"]) dtypes = {"security": numpy.dtype("O"), "ID_EXCH_SYMBOL": numpy.dtype("O")} df_expect = pandas.DataFrame( - [["RSF82 Comdty", None], ["PLG18 Comdty", "PL"]], columns=["security", "ID_EXCH_SYMBOL"], + [["RSF82 Comdty", None], ["PLG18 Comdty", "PL"]], + columns=["security", "ID_EXCH_SYMBOL"], ).astype(dtypes) assert_frame_equal(df, df_expect) @@ -2591,7 +2610,12 @@ def bdit_params(): "ids": ["bdit", "bdit_with_condition_codes"], "argvalues": [ ("CL1 Comdty", ["TRADE"], None, dtype_expect), - ("CL1 Comdty", ["TRADE"], [("includeConditionCodes", True)], dtype_expect_cc,), + ( + "CL1 Comdty", + ["TRADE"], + [("includeConditionCodes", True)], + dtype_expect_cc, + ), ], } @@ -2624,7 +2648,11 @@ def test_create_historical_query(): } } res = blp.create_historical_query( - ["SPY US Equity"], ["PX_LAST"], "20190101", "20190110", options=[("periodicitySelection", "DAILY")], + ["SPY US Equity"], + ["PX_LAST"], + "20190101", + "20190110", + options=[("periodicitySelection", "DAILY")], ) assert res == exp_res