Skip to content

Commit 32a041b

Browse files
committed
Separate Financial Core Formulas from UI
1 parent dbb058a commit 32a041b

File tree

6 files changed

+56
-41
lines changed

6 files changed

+56
-41
lines changed

src/core/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .currency import convert_currency_symbol
2+
from .financial_formulas import calculate_price_changes
3+
4+
financial_calculations = {
5+
"currency_symbol": convert_currency_symbol,
6+
"price_changes": calculate_price_changes,
7+
}

src/core/currency.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def convert_currency_symbol(currency_symbol):
2+
currency = {
3+
"USD": "$", # US Dollar
4+
"EUR": "€", # Euro
5+
"JPY": "¥", # Japanese Yen
6+
"GBP": "£", # British Pound Sterling
7+
"AUD": "A$", # Australian Dollar
8+
"CAD": "C$", # Canadian Dollar
9+
"CHF": "CHF", # Swiss Franc
10+
"CNY": "¥", # Chinese Yuan Renminbi
11+
"HKD": "HK$", # Hong Kong Dollar
12+
"INR": "₹", # Indian Rupee
13+
"RUB": "₽", # Russian Ruble
14+
"BRL": "R$", # Brazilian Real
15+
"ZAR": "R", # South African Rand
16+
"KRW": "₩", # South Korean Won
17+
"MXN": "$", # Mexican Peso
18+
"SGD": "S$", # Singapore Dollar
19+
"NZD": "NZ$", # New Zealand Dollar
20+
"TRY": "₺", # Turkish Lira
21+
"SEK": "kr", # Swedish Krona
22+
"NOK": "kr", # Norwegian Krone
23+
}
24+
25+
return currency[currency_symbol]

src/core/financial_formulas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def calculate_price_changes(current_price, previous_close_price):
2+
return round((current_price - previous_close_price) / previous_close_price, 2) * 100

src/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import core
12
import infra
23
import ui
34

45
_financial_provider = None
6+
_financial_calculations = None
57

68

79
def initialize_financial_provider(provider_name):
@@ -10,9 +12,16 @@ def initialize_financial_provider(provider_name):
1012
_financial_provider = infra.financial_data_provider(provider_name)
1113

1214

15+
def initialize_financial_formulas():
16+
global _financial_calculations
17+
if _financial_calculations is None:
18+
_financial_calculations = core.financial_calculations
19+
20+
1321
def main():
1422
initialize_financial_provider(infra.YAHOO)
15-
ui.run(_financial_provider)
23+
initialize_financial_formulas()
24+
ui.run(_financial_provider, _financial_calculations)
1625

1726

1827
if __name__ == "__main__":

src/ui/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
initial_state = {"ticker": ""}
77

88

9-
def run(financial_data):
9+
def run(financial_data, financial_calculations):
1010
st.set_page_config(
1111
page_title="Stock Analysis Dashboard",
1212
page_icon=":chart_with_upwards_trend:",
@@ -19,5 +19,5 @@ def run(financial_data):
1919
st.session_state[k] = v
2020

2121
# performs the rendering
22-
render_layout(financial_data)
22+
render_layout(financial_data, financial_calculations)
2323
render_sidebar()

src/ui/layout.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from .raw_content import homepage_content, footer_content, not_found_ticker_content
55

66

7-
def render(financial_data):
7+
def render(financial_data, financial_calculations):
88
ticker = st.session_state["ticker"]
99
if not ticker:
1010
render_homepage()
1111
elif not financial_data.is_valid_ticker(ticker):
1212
render_invalid_ticker_placeholder()
1313
else:
14-
render_stock_info(financial_data)
14+
render_stock_info(financial_data, financial_calculations)
1515
render_footer()
1616

1717

@@ -27,9 +27,9 @@ def render_footer():
2727
st.markdown(footer_content, unsafe_allow_html=True)
2828

2929

30-
def render_stock_info(financial_data):
30+
def render_stock_info(financial_data, financial_calculations):
3131
# Render header
32-
render_header(financial_data)
32+
render_header(financial_data, financial_calculations)
3333
# Renders tabs
3434
tab_titles = ["Overview", "Balance Sheet", "Income Statement", "CashFlow"]
3535
tabs_renderings = [
@@ -44,7 +44,7 @@ def render_stock_info(financial_data):
4444
render_action(financial_data)
4545

4646

47-
def render_header(financial_data):
47+
def render_header(financial_data, financial_calculations):
4848
stock_info = financial_data.get_stock_info(
4949
st.session_state["ticker"],
5050
)
@@ -55,8 +55,11 @@ def render_header(financial_data):
5555
current_price = stock_info["currentPrice"]
5656
previous_close_price = stock_info["previousClose"]
5757
currency = stock_info["currency"]
58-
current_price_content = f"{convert_currency_symbol(currency)}{current_price}"
59-
price_diff = calculate_price_changes(current_price, previous_close_price)
58+
currency_symbol = financial_calculations["currency_symbol"](currency)
59+
current_price_content = f"{currency_symbol}{current_price}"
60+
price_diff = financial_calculations["price_changes"](
61+
current_price, previous_close_price
62+
)
6063
price_diff_content = f"({price_diff}%)"
6164
price_change_content = (
6265
apply_text_color(price_diff_content, "red")
@@ -116,36 +119,5 @@ def render_cashflow(financial_data):
116119
st.dataframe(cashflow)
117120

118121

119-
def convert_currency_symbol(currency_symbol):
120-
currency = {
121-
"USD": "$", # US Dollar
122-
"EUR": "€", # Euro
123-
"JPY": "¥", # Japanese Yen
124-
"GBP": "£", # British Pound Sterling
125-
"AUD": "A$", # Australian Dollar
126-
"CAD": "C$", # Canadian Dollar
127-
"CHF": "CHF", # Swiss Franc
128-
"CNY": "¥", # Chinese Yuan Renminbi
129-
"HKD": "HK$", # Hong Kong Dollar
130-
"INR": "₹", # Indian Rupee
131-
"RUB": "₽", # Russian Ruble
132-
"BRL": "R$", # Brazilian Real
133-
"ZAR": "R", # South African Rand
134-
"KRW": "₩", # South Korean Won
135-
"MXN": "$", # Mexican Peso
136-
"SGD": "S$", # Singapore Dollar
137-
"NZD": "NZ$", # New Zealand Dollar
138-
"TRY": "₺", # Turkish Lira
139-
"SEK": "kr", # Swedish Krona
140-
"NOK": "kr", # Norwegian Krone
141-
}
142-
143-
return currency[currency_symbol]
144-
145-
146-
def calculate_price_changes(current_price, previous_close_price):
147-
return round((current_price - previous_close_price) / previous_close_price, 2) * 100
148-
149-
150122
def apply_text_color(text, color):
151123
return f"<span style='color:{color}'>{text}</span>"

0 commit comments

Comments
 (0)