Skip to content

Commit a8562a7

Browse files
committed
Restructure project architecture
1 parent f6e54ec commit a8562a7

30 files changed

+184
-230
lines changed

.github/workflows/pipeline.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ jobs:
2323
uses: streamlit/streamlit-app-action@v0.0.3
2424
with:
2525
skip-smoke: 'true'
26-
app-path: 'src/app.py'
26+
app-path: 'src/main.py'
2727
ruff: 'true'

src/__init__.py

Whitespace-only changes.

src/app.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/infra/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .yahoo import YahooFinance
2+
3+
YAHOO = "yahoo-finance"
4+
5+
6+
def financial_data_provider(provider_name: str) -> object:
7+
if provider_name is YAHOO:
8+
return YahooFinance
9+
raise ValueError(f"provider_name must be one of {['yahoo-finance']}")
Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
11
import logging
2-
from abc import ABC
32

43
import pandas as pd
54
import yfinance as yf
65

7-
from .FinancialDataProvider import FinancialDataProvider
8-
96
logging.basicConfig(filename="YahooFinanceProvider.log", level=logging.ERROR)
107

118

12-
class YahooFinanceProvider(FinancialDataProvider, ABC):
13-
def __init__(self, ticker):
14-
super().__init__(ticker)
15-
self.stock = yf.Ticker(ticker)
16-
self.general_info = self.stock.info
17-
self.balance_sheet = self.stock.balance_sheet
18-
self.income_stmt = self.stock.income_stmt
19-
self.cash_flow = self.stock.cash_flow
9+
class YahooFinance:
10+
@staticmethod
11+
def is_valid_ticker(ticker: str) -> bool:
12+
stock = yf.Ticker(ticker)
13+
return stock.isin is not None and len(stock.info) > 1
2014

21-
def get_historical_data(self, period: str, interval: str) -> pd.DataFrame:
15+
@staticmethod
16+
def get_historical_data(
17+
ticker: str, columns: [], period: str, interval: str
18+
) -> pd.DataFrame:
19+
stock = yf.Ticker(ticker)
2220
try:
23-
return self.stock.history(period=period, interval=interval)
21+
return stock.history(period=period, interval=interval)[columns]
2422
except Exception as e:
2523
logging.error(
26-
f"Error fetching historical data from Yahoo Finance for {self.stock}: {e}"
24+
f"Error fetching historical data from Yahoo Finance for {stock}: {e}"
2725
)
2826

29-
def get_stock_info(self) -> dict:
27+
@staticmethod
28+
def get_stock_info(ticker: str) -> dict:
29+
stock = yf.Ticker(ticker)
3030
try:
31-
return self.general_info
31+
return stock.info
3232
except Exception as e:
33-
logging.error(f"Error fetching stock info for {self.stock}: {e}")
33+
logging.error(f"Error fetching stock info for {stock}: {e}")
34+
35+
@staticmethod
36+
def get_balance_sheet(ticker: str) -> pd.DataFrame:
37+
stock = yf.Ticker(ticker)
3438

35-
def get_balance_sheet(self) -> pd.DataFrame:
3639
try:
3740
fields = [
3841
"Current Assets",
@@ -47,23 +50,26 @@ def get_balance_sheet(self) -> pd.DataFrame:
4750
"Stockholders Equity",
4851
]
4952

50-
self.balance_sheet.loc["Debt to Equity"] = (
51-
self.balance_sheet.loc["Total Liabilities Net Minority Interest"]
52-
/ self.balance_sheet.loc["Stockholders Equity"]
53+
stock.balance_sheet.loc["Debt to Equity"] = (
54+
stock.balance_sheet.loc["Total Liabilities Net Minority Interest"]
55+
/ stock.balance_sheet.loc["Stockholders Equity"]
5356
)
54-
self.balance_sheet.loc["Current Ratio"] = (
55-
self.balance_sheet.loc["Current Assets"]
56-
/ self.balance_sheet.loc["Current Liabilities"]
57+
stock.balance_sheet.loc["Current Ratio"] = (
58+
stock.balance_sheet.loc["Current Assets"]
59+
/ stock.balance_sheet.loc["Current Liabilities"]
5760
)
5861

59-
df_balance_sheet = self.balance_sheet.transpose()
62+
df_balance_sheet = stock.balance_sheet.transpose()
6063
df_balance_sheet = df_balance_sheet.reindex(columns=fields, fill_value=0)
6164

6265
return df_balance_sheet.sort_index().tail(4)
6366
except Exception as e:
64-
logging.error(f"Error fetching balance sheet for {self.stock}: {e}")
67+
logging.error(f"Error fetching balance sheet for {stock}: {e}")
68+
69+
@staticmethod
70+
def get_income_statement(ticker: str) -> pd.DataFrame:
71+
stock = yf.Ticker(ticker)
6572

66-
def get_income_statement(self) -> pd.DataFrame:
6773
try:
6874
fields = [
6975
"Total Revenue",
@@ -81,15 +87,17 @@ def get_income_statement(self) -> pd.DataFrame:
8187
"Basic EPS",
8288
]
8389

84-
df_income_stmt = self.income_stmt.transpose()
90+
df_income_stmt = stock.income_stmt.transpose()
8591
df_income_stmt = df_income_stmt.reindex(columns=fields, fill_value=0)
8692

8793
return df_income_stmt.sort_index().tail(4)
8894

8995
except Exception as e:
90-
logging.error(f"Error fetching income statement for {self.stock}: {e}")
96+
logging.error(f"Error fetching income statement for {stock}: {e}")
9197

92-
def get_cash_flow(self):
98+
@staticmethod
99+
def get_cashflow(ticker: str):
100+
stock = yf.Ticker(ticker)
93101
try:
94102
fields = [
95103
"Operating Cash Flow",
@@ -98,10 +106,10 @@ def get_cash_flow(self):
98106
"Capital Expenditure",
99107
"Free Cash Flow",
100108
]
101-
df_cashflow = self.cash_flow.transpose()
109+
df_cashflow = stock.cash_flow.transpose()
102110
df_cashflow = df_cashflow.reindex(columns=fields, fill_value=0)
103111

104112
return df_cashflow.sort_index().tail(4)
105113

106114
except Exception as e:
107-
logging.error(f"Error fetching cash flow for {self.stock}: {e}")
115+
logging.error(f"Error fetching cash flow for {stock}: {e}")

src/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import infra
2+
import ui
3+
4+
_financial_provider = None
5+
6+
7+
def initialize_financial_provider(provider_name):
8+
global _financial_provider
9+
if _financial_provider is None:
10+
_financial_provider = infra.financial_data_provider(provider_name)
11+
12+
13+
def main():
14+
initialize_financial_provider(infra.YAHOO)
15+
ui.run(_financial_provider)
16+
17+
18+
if __name__ == "__main__":
19+
main()

src/ui/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import streamlit as st
2+
3+
from .layout import render as render_layout
4+
from .sidebar import render as render_sidebar
5+
6+
initial_state = {"ticker": ""}
7+
8+
9+
def run(financial_data):
10+
st.set_page_config(
11+
page_title="Stock Analysis Dashboard",
12+
page_icon=":chart_with_upwards_trend:",
13+
layout="wide",
14+
initial_sidebar_state="expanded",
15+
)
16+
17+
for k, v in initial_state.items():
18+
if k not in st.session_state:
19+
st.session_state[k] = v
20+
21+
# performs the rendering
22+
render_layout(financial_data)
23+
render_sidebar()

src/ui/components/__init__.py

Whitespace-only changes.

src/ui/components/balance_sheet.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ui/components/cashflow.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/ui/components/container.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/ui/components/content.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/ui/components/footer.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/ui/components/header.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/ui/components/homepage.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/ui/components/income_stmt.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/ui/components/overview.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/ui/components/stock.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/ui/content/__init__.py

Whitespace-only changes.

src/ui/content/footer.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/ui/data_providers/FinancialDataProvider.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/ui/data_providers/FinancialDataProviderFactory.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/ui/data_providers/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)