Skip to content

Commit 098ddc6

Browse files
committed
magicformula class - add process and calc_rank now does ranking only
1 parent dd769d9 commit 098ddc6

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/magicformulabr/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,13 @@ def _filter_data(self):
297297
self._remove_rows(self.ret_on_capital, 0)
298298
self._remove_rows("Liq.2meses", 0)
299299

300-
def calc_rank(self):
300+
def _calculate_rank(self):
301301
"""
302302
Calculates the Magic Formula ranking for companies in `pd_df`.
303303
304304
Adds ranking columns to `pd_df` based on the earnings yield and return on capital,
305305
then calculates a final rank.
306306
"""
307-
self._apply_converters()
308-
self._filter_data()
309-
310307
if self.pd_df.empty:
311308
return self.pd_df
312309

@@ -323,6 +320,15 @@ def calc_rank(self):
323320

324321
return self.pd_df
325322

323+
def process(self):
324+
"""
325+
Executes the full Magic Formula process: conversion, filtering, and ranking.
326+
"""
327+
self._apply_converters()
328+
self._filter_data()
329+
df_ranked = self._calculate_rank()
330+
return df_ranked
331+
326332

327333
def display_results(df, args):
328334
"""
@@ -388,7 +394,7 @@ def main():
388394
pd_df = data_handler.get_data()
389395

390396
magicformula = MagicFormula(pd_df, args.method)
391-
ranked_df = magicformula.calc_rank()
397+
ranked_df = magicformula.process()
392398
if ranked_df.empty:
393399
print("No companies passed the filtering criteria. The ranking is empty.")
394400
else:
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
# -*- coding: utf-8 -*-
22
"""Test calc_rank method."""
33

4-
from unittest.mock import Mock
5-
64
import pandas as pd
75
import pytest
86

97
from magicformulabr.main import MagicFormula
108

119

12-
@pytest.fixture(scope="function")
13-
def sample_df():
10+
@pytest.fixture(name="sample_df", scope="function")
11+
def _sample_df():
1412
"""Create fake dataframe."""
1513
dic_com = {"EV/EBIT": [0.2, 8, 4, 2], "ROIC": [10, 60, 40, 90]}
1614
my_df = pd.DataFrame(data=dic_com, index=["AAAA3", "BBBB3", "CCCC3", "DDDD4"])
1715
return my_df
1816

1917

20-
def test_calc_rank_method_2(sample_df): # pylint: disable=redefined-outer-name
18+
def test_calc_rank_method_2(sample_df):
2119
"""Test calc_rank method."""
2220
expected_result = """\
2321
EV/EBIT ROIC Rank_earnings_yield Rank_return_on_capital Rank_Final
@@ -27,7 +25,5 @@ def test_calc_rank_method_2(sample_df): # pylint: disable=redefined-outer-name
2725
CCCC3 4.0 40 3.0 3.0 6.0"""
2826

2927
magic_formula = MagicFormula(sample_df, magic_method=2)
30-
magic_formula._apply_converters = Mock() # pylint: disable=protected-access
31-
magic_formula._filter_data = Mock() # pylint: disable=protected-access
32-
pd_df = magic_formula.calc_rank()
28+
pd_df = magic_formula._calculate_rank() # pylint: disable=protected-access
3329
assert pd_df.to_string() == expected_result

0 commit comments

Comments
 (0)