Skip to content

Commit

Permalink
version 0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
debpal committed Oct 6, 2024
1 parent 4abeee1 commit 039981e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 41 deletions.
2 changes: 1 addition & 1 deletion BharatFinTrack/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
22 changes: 14 additions & 8 deletions BharatFinTrack/nse_tri.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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:
Expand Down
42 changes: 10 additions & 32 deletions tests/test_bharatfintrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,72 +250,50 @@ 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(
excel_file=excel_file
)
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(
excel_file='equily.xl'
)
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(
excel_file=excel_file
)
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(
Expand All @@ -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
):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 039981e

Please sign in to comment.