From 039981ea7bfa74eb8a438a182f1871cda57f898b Mon Sep 17 00:00:00 2001 From: Debasish Pal <48341250+debpal@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:15:38 +0300 Subject: [PATCH] version 0.1.3 --- BharatFinTrack/core.py | 2 +- BharatFinTrack/nse_tri.py | 22 ++++++++++++------- tests/test_bharatfintrack.py | 42 +++++++++--------------------------- 3 files changed, 25 insertions(+), 41 deletions(-) diff --git a/BharatFinTrack/core.py b/BharatFinTrack/core.py index b83806d..1445aaf 100644 --- a/BharatFinTrack/core.py +++ b/BharatFinTrack/core.py @@ -126,7 +126,7 @@ def _download_nse_tri( url=self.url_nse_index_tri_data, headers=headers, data=payload, - timeout=30 + timeout=15 ) response_data = response.json() records = json.loads(response_data['d']) diff --git a/BharatFinTrack/nse_tri.py b/BharatFinTrack/nse_tri.py index 8f09c1e..b9fbab7 100644 --- a/BharatFinTrack/nse_tri.py +++ b/BharatFinTrack/nse_tri.py @@ -195,7 +195,6 @@ def download_equity_indices_updated_value( # processing base DataFrame base_df = NSEProduct()._dataframe_equity_index base_df = base_df.reset_index() - base_df = base_df[base_df['API TRI'] != 'NON OPEN SOURCE'].reset_index(drop=True) base_df = base_df.drop(columns=['ID', 'API TRI']) base_df['Base Date'] = base_df['Base Date'].apply(lambda x: x.date()) @@ -212,13 +211,20 @@ def download_equity_indices_updated_value( end_date = today.strftime('%d-%b-%Y') start_date = week_ago.strftime('%d-%b-%Y') for base_index in base_df.index: - index_df = self.download_historical_daily_data( - index=base_df.loc[base_index, 'Index Name'], - start_date=start_date, - end_date=end_date - ) - base_df.loc[base_index, 'Close Date'] = index_df.iloc[-1, 0] - base_df.loc[base_index, 'Close Value'] = index_df.iloc[-1, -1] + try: + index_df = self.download_historical_daily_data( + index=base_df.loc[base_index, 'Index Name'], + start_date=start_date, + end_date=end_date + ) + base_df.loc[base_index, 'Close Date'] = index_df.iloc[-1, 0] + base_df.loc[base_index, 'Close Value'] = index_df.iloc[-1, -1] + except Exception: + base_df.loc[base_index, 'Close Date'] = end_date + base_df.loc[base_index, 'Close Value'] = -1000 + + # removing error rows from the DataFrame + base_df = base_df[base_df['Close Value'] != -1000].reset_index(drop=True) # saving the DataFrame with pandas.ExcelWriter(excel_file, engine='xlsxwriter') as excel_writer: diff --git a/tests/test_bharatfintrack.py b/tests/test_bharatfintrack.py index 6e7f13e..7b25bb1 100644 --- a/tests/test_bharatfintrack.py +++ b/tests/test_bharatfintrack.py @@ -250,41 +250,27 @@ def test_index_download_historical_daily_data( assert float(df.iloc[-1, -1]) == expected_value -def test_download_daily_summary_report( +def test_equity_index_price_download_updated_value( nse_index, - message + message, + capsys ): - # test for error when the input is a invalid folder path + # error test for folder path with tempfile.TemporaryDirectory() as tmp_dir: pass with pytest.raises(Exception) as exc_info: nse_index.download_daily_summary_report(tmp_dir) assert exc_info.value.args[0] == message['error_folder'] - - -def test_equity_cagr_from_launch( - nse_index, - capsys -): - + # pass test for capturing print statement nse_index.equity_cagr_from_launch( untracked_indices=True ) - - # capture the printed output capture_print = capsys.readouterr() - assert 'List of untracked download indices' in capture_print.out assert 'List of untracked base indices' in capture_print.out - -def test_sort_equity_cagr_from_launch( - nse_index, - message -): - - # pass test + # pass test for sorting of NSE equity indices by CAGR (%) value with tempfile.TemporaryDirectory() as tmp_dir: excel_file = os.path.join(tmp_dir, 'equity.xlsx') nse_index.sort_equity_cagr_from_launch( @@ -292,7 +278,6 @@ def test_sort_equity_cagr_from_launch( ) df = pandas.read_excel(excel_file) assert len(df.index.names) == 1 - # error test for invalid Excel file input with pytest.raises(Exception) as exc_info: nse_index.sort_equity_cagr_from_launch( @@ -300,13 +285,7 @@ def test_sort_equity_cagr_from_launch( ) assert exc_info.value.args[0] == message['error_excel'] - -def test_category_sort_equity_cagr_from_launch( - nse_index, - message -): - - # pass test + # pass test for categorical sorting NSE equity indices by CAGR (%) value with tempfile.TemporaryDirectory() as tmp_dir: excel_file = os.path.join(tmp_dir, 'equity.xlsx') nse_index.category_sort_equity_cagr_from_launch( @@ -314,8 +293,7 @@ def test_category_sort_equity_cagr_from_launch( ) df = pandas.read_excel(excel_file, index_col=[0, 1]) assert df.shape[1] == 9 - assert len(df.index.get_level_values('Category').unique()) == 5 - + assert len(df.index.get_level_values('Category').unique()) <= 5 # error test for invalid Excel file input with pytest.raises(Exception) as exc_info: nse_index.category_sort_equity_cagr_from_launch( @@ -324,7 +302,7 @@ def test_category_sort_equity_cagr_from_launch( assert exc_info.value.args[0] == message['error_excel'] -def test_download_equity_indices_updated_value( +def test_equity_index_tri_download_updated_value( nse_tri, message ): @@ -380,7 +358,7 @@ def test_download_equity_indices_updated_value( output_excel=output_excel ) df = pandas.read_excel(output_excel, index_col=[0, 1]) - assert len(df.index.get_level_values('Category').unique()) == 5 + assert len(df.index.get_level_values('Category').unique()) <= 5 # error test for invalid Excel file input with pytest.raises(Exception) as exc_info: nse_tri.category_sort_equity_cagr_from_launch(