From 373f387cb43a8980b05d0fadd0a2891b24bb3b9f Mon Sep 17 00:00:00 2001 From: Joseph Walker Date: Thu, 9 Feb 2023 14:48:07 -0500 Subject: [PATCH 1/5] Return a dataframe from stocks search, allow override to not export to file system (#3923) --- openbb_terminal/stocks/stocks_helper.py | 45 ++++++++++++++----------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index 0f713db6fd7e..5d50cf664bbb 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -106,7 +106,8 @@ def search( limit: int = 0, export: str = "", sheet_name: Optional[str] = "", -) -> None: + export_to_file: bool = True, +) -> Optional[pd.DataFrame]: """Search selected query for tickers. Parameters @@ -127,6 +128,13 @@ def search( The limit of companies shown. export : str Export data + export_to_file : bool + Whether results should be exported to file or not. + + Returns + ------- + df: Optional[pd.DataFrame] + Dataframe of search results if any results are found Examples -------- @@ -183,18 +191,13 @@ def search( df = pd.DataFrame.from_dict(d).T[ ["long_name", "short_name", "country", "sector", "industry", "exchange"] ] - if exchange_country: - if exchange_country in market_coverage_suffix: - suffix_tickers = [ - ticker.split(".")[1] if "." in ticker else "" - for ticker in list(df.index) - ] - df = df[ - [ - val in market_coverage_suffix[exchange_country] - for val in suffix_tickers - ] - ] + if exchange_country and exchange_country in market_coverage_suffix: + suffix_tickers = [ + ticker.split(".")[1] if "." in ticker else "" for ticker in list(df.index) + ] + df = df[ + [val in market_coverage_suffix[exchange_country] for val in suffix_tickers] + ] exchange_suffix = {} for k, v in market_coverage_suffix.items(): @@ -227,14 +230,16 @@ def search( headers=["Name", "Country", "Sector", "Industry", "Exchange"], title=title, ) + if export_to_file: + export_data( + export, + os.path.dirname(os.path.abspath(__file__)), + "search", + df, + sheet_name, + ) - export_data( - export, - os.path.dirname(os.path.abspath(__file__)), - "search", - df, - sheet_name, - ) + return df def load( From 06b919630ece241957249c4dbb67a708b610b20c Mon Sep 17 00:00:00 2001 From: Joseph Walker Date: Thu, 9 Feb 2023 15:27:29 -0500 Subject: [PATCH 2/5] Fix documentation to reflect changes in search method in sdk --- .../content/sdk/reference/stocks/search.md | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/website/content/sdk/reference/stocks/search.md b/website/content/sdk/reference/stocks/search.md index c98fc22cbc63..2b7de17c21b5 100644 --- a/website/content/sdk/reference/stocks/search.md +++ b/website/content/sdk/reference/stocks/search.md @@ -10,29 +10,34 @@ Search selected query for tickers. Source Code: [[link](https://github.com/OpenBB-finance/OpenBBTerminal/tree/main/openbb_terminal/stocks/stocks_helper.py#L98)] ```python -openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "") +openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "", sheet_name: Optional[str] = "", export_to_file: bool = True) ``` --- ## Parameters -| Name | Type | Description | Default | Optional | -| ---- | ---- | ----------- | ------- | -------- | -| query | str | The search term used to find company tickers | | True | -| country | str | Search by country to find stocks matching the criteria | | True | -| sector | str | Search by sector to find stocks matching the criteria | | True | -| industry | str | Search by industry to find stocks matching the criteria | | True | -| exchange_country | str | Search by exchange country to find stock matching | | True | -| limit | int | The limit of companies shown. | 0 | True | -| export | str | Export data | | True | - +| Name | Type | Description | Default | Optional | +|------------------|------|---------------------------------------------------------|---------|----------| +| query | str | The search term used to find company tickers | | True | +| country | str | Search by country to find stocks matching the criteria | | True | +| sector | str | Search by sector to find stocks matching the criteria | | True | +| industry | str | Search by industry to find stocks matching the criteria | | True | +| exchange_country | str | Search by exchange country to find stock matching | | True | +| limit | int | The limit of companies shown. | 0 | True | +| export | str | Export data | | True | +| sheet_name | str | Sheet Name | | True | +| export_to_file | bool | Whether results should be exported to file or not. | | True | --- ## Returns -This function does not return anything +| Type | Description | +|------------------------|---------------------------------| +| Optional[pd.DataFrame] | Search results if any are found | +--- + --- From f247b6da347971799d73e926eb4e5b7cf57206a2 Mon Sep 17 00:00:00 2001 From: Joseph Walker Date: Fri, 10 Feb 2023 11:51:38 -0500 Subject: [PATCH 3/5] Remove export_to_file flag for redundancy --- openbb_terminal/stocks/stocks_helper.py | 18 +++++++----------- website/content/sdk/reference/stocks/search.md | 3 +-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index 5d50cf664bbb..58ce3b4b1854 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -106,7 +106,6 @@ def search( limit: int = 0, export: str = "", sheet_name: Optional[str] = "", - export_to_file: bool = True, ) -> Optional[pd.DataFrame]: """Search selected query for tickers. @@ -128,8 +127,6 @@ def search( The limit of companies shown. export : str Export data - export_to_file : bool - Whether results should be exported to file or not. Returns ------- @@ -230,14 +227,13 @@ def search( headers=["Name", "Country", "Sector", "Industry", "Exchange"], title=title, ) - if export_to_file: - export_data( - export, - os.path.dirname(os.path.abspath(__file__)), - "search", - df, - sheet_name, - ) + export_data( + export, + os.path.dirname(os.path.abspath(__file__)), + "search", + df, + sheet_name, + ) return df diff --git a/website/content/sdk/reference/stocks/search.md b/website/content/sdk/reference/stocks/search.md index 2b7de17c21b5..6faca1bba8e4 100644 --- a/website/content/sdk/reference/stocks/search.md +++ b/website/content/sdk/reference/stocks/search.md @@ -10,7 +10,7 @@ Search selected query for tickers. Source Code: [[link](https://github.com/OpenBB-finance/OpenBBTerminal/tree/main/openbb_terminal/stocks/stocks_helper.py#L98)] ```python -openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "", sheet_name: Optional[str] = "", export_to_file: bool = True) +openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "", sheet_name: Optional[str] = "") ``` --- @@ -27,7 +27,6 @@ openbb.stocks.search(query: str = "", country: str = "", sector: str = "", indus | limit | int | The limit of companies shown. | 0 | True | | export | str | Export data | | True | | sheet_name | str | Sheet Name | | True | -| export_to_file | bool | Whether results should be exported to file or not. | | True | --- From d0d1eb8e280003fe78c4fa675ff377b4a0873d71 Mon Sep 17 00:00:00 2001 From: Joseph Walker Date: Fri, 10 Feb 2023 11:54:09 -0500 Subject: [PATCH 4/5] Remove erroneous extra line from documentation --- website/content/sdk/reference/stocks/search.md | 1 - 1 file changed, 1 deletion(-) diff --git a/website/content/sdk/reference/stocks/search.md b/website/content/sdk/reference/stocks/search.md index 6faca1bba8e4..a1ac3ec74154 100644 --- a/website/content/sdk/reference/stocks/search.md +++ b/website/content/sdk/reference/stocks/search.md @@ -35,7 +35,6 @@ openbb.stocks.search(query: str = "", country: str = "", sector: str = "", indus | Type | Description | |------------------------|---------------------------------| | Optional[pd.DataFrame] | Search results if any are found | ---- --- From a11810ac74dff4b2edeb0be0fc8506c9e951f092 Mon Sep 17 00:00:00 2001 From: Joseph Walker Date: Fri, 10 Feb 2023 13:21:28 -0500 Subject: [PATCH 5/5] Remove export to file now that we are returning a dataframe. Always return a dataframe even if empty. Update tests to reflect removed function arguments. Update documentation as well. --- openbb_terminal/stocks/stocks_controller.py | 4 --- openbb_terminal/stocks/stocks_helper.py | 25 ++++++------------- .../stocks/test_stocks_controller.py | 2 -- .../stocks/test_stocks_helper.py | 1 - .../content/sdk/reference/stocks/search.md | 10 +++----- 5 files changed, 11 insertions(+), 31 deletions(-) diff --git a/openbb_terminal/stocks/stocks_controller.py b/openbb_terminal/stocks/stocks_controller.py index fe0bc20eb169..e33009823629 100644 --- a/openbb_terminal/stocks/stocks_controller.py +++ b/openbb_terminal/stocks/stocks_controller.py @@ -242,10 +242,6 @@ def call_search(self, other_args: List[str]): exchange_country=exchange, all_exchanges=ns_parser.all_exchanges, limit=ns_parser.limit, - export=ns_parser.export, - sheet_name=" ".join(ns_parser.sheet_name) - if ns_parser.sheet_name - else None, ) @log_start_end(log=logger) diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index 2d0a4c677b1f..34856e9a5272 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -27,7 +27,6 @@ from openbb_terminal import config_terminal as cfg from openbb_terminal.helper_funcs import ( - export_data, lambda_long_number_format_y_axis, plot_autoscale, print_rich_table, @@ -106,9 +105,7 @@ def search( exchange_country: str = "", all_exchanges: bool = False, limit: int = 0, - export: str = "", - sheet_name: Optional[str] = "", -) -> Optional[pd.DataFrame]: +) -> pd.DataFrame: """Search selected query for tickers. Parameters @@ -127,13 +124,12 @@ def search( Whether to search all exchanges, without this option only the United States market is searched limit : int The limit of companies shown. - export : str - Export data Returns ------- - df: Optional[pd.DataFrame] - Dataframe of search results if any results are found + df: pd.DataFrame + Dataframe of search results. + Empty Dataframe if none are found. Examples -------- @@ -162,10 +158,10 @@ def search( console.print( "[red]No companies were found that match the given criteria.[/red]\n" ) - return + return pd.DataFrame() if not data: console.print("No companies found.\n") - return + return pd.DataFrame() if query: d = fd.search_products( @@ -185,7 +181,7 @@ def search( if not d: console.print("No companies found.\n") - return + return pd.DataFrame() df = pd.DataFrame.from_dict(d).T[ ["long_name", "short_name", "country", "sector", "industry", "exchange"] @@ -229,13 +225,6 @@ def search( headers=["Name", "Country", "Sector", "Industry", "Exchange"], title=title, ) - export_data( - export, - os.path.dirname(os.path.abspath(__file__)), - "search", - df, - sheet_name, - ) return df diff --git a/tests/openbb_terminal/stocks/test_stocks_controller.py b/tests/openbb_terminal/stocks/test_stocks_controller.py index 3316100ca583..c21086406fc6 100644 --- a/tests/openbb_terminal/stocks/test_stocks_controller.py +++ b/tests/openbb_terminal/stocks/test_stocks_controller.py @@ -263,8 +263,6 @@ def test_call_func_expect_queue(expected_queue, func, queue): industry="", all_exchanges=False, exchange_country="", - export="csv", - sheet_name=None, ), ), ( diff --git a/tests/openbb_terminal/stocks/test_stocks_helper.py b/tests/openbb_terminal/stocks/test_stocks_helper.py index 7c53cee644d7..811d4073b5e6 100644 --- a/tests/openbb_terminal/stocks/test_stocks_helper.py +++ b/tests/openbb_terminal/stocks/test_stocks_helper.py @@ -50,7 +50,6 @@ def test_search(mocker, use_tab): exchange_country="", all_exchanges=False, limit=5, - export="", ) diff --git a/website/content/sdk/reference/stocks/search.md b/website/content/sdk/reference/stocks/search.md index a1ac3ec74154..085839cac16d 100644 --- a/website/content/sdk/reference/stocks/search.md +++ b/website/content/sdk/reference/stocks/search.md @@ -10,7 +10,7 @@ Search selected query for tickers. Source Code: [[link](https://github.com/OpenBB-finance/OpenBBTerminal/tree/main/openbb_terminal/stocks/stocks_helper.py#L98)] ```python -openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0, export: str = "", sheet_name: Optional[str] = "") +openbb.stocks.search(query: str = "", country: str = "", sector: str = "", industry: str = "", exchange_country: str = "", limit: int = 0) ``` --- @@ -25,16 +25,14 @@ openbb.stocks.search(query: str = "", country: str = "", sector: str = "", indus | industry | str | Search by industry to find stocks matching the criteria | | True | | exchange_country | str | Search by exchange country to find stock matching | | True | | limit | int | The limit of companies shown. | 0 | True | -| export | str | Export data | | True | -| sheet_name | str | Sheet Name | | True | --- ## Returns -| Type | Description | -|------------------------|---------------------------------| -| Optional[pd.DataFrame] | Search results if any are found | +| Type | Description | +|--------------|----------------| +| pd.DataFrame | Search results | ---