diff --git a/gamestonk_terminal/stocks/fundamental_analysis/dcf_model.py b/gamestonk_terminal/stocks/fundamental_analysis/dcf_model.py index e1cf5e04321..75260e947a5 100644 --- a/gamestonk_terminal/stocks/fundamental_analysis/dcf_model.py +++ b/gamestonk_terminal/stocks/fundamental_analysis/dcf_model.py @@ -3,9 +3,11 @@ __docformat__ = "numpy" from urllib.request import urlopen from typing import List, Union, Dict, Any, Tuple +from pathlib import Path from zipfile import ZipFile from io import BytesIO import re +import os from sklearn.linear_model import LinearRegression from openpyxl import worksheet @@ -432,3 +434,32 @@ def frac(num: float, denom: float) -> Union[str, float]: The fraction """ return "N/A" if denom == 0 else num / denom + + +def generate_path(n: int, ticker: str, date: str) -> Path: + """ + Create the path to save an excel file to + + Parameters + ---------- + n : int + The try number + ticker : str + The ticker to be saved + date : str + The date the dcf was generated + + Returns + ------- + path : Path + The path to save a file to + """ + val = "" if n == 0 else f"({n})" + trypath = os.path.join( + os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")), + "exports", + "stocks", + "fundamental_analysis", + f"{ticker} {date}{val}.xlsx", + ) + return Path(trypath) diff --git a/gamestonk_terminal/stocks/fundamental_analysis/dcf_view.py b/gamestonk_terminal/stocks/fundamental_analysis/dcf_view.py index 8224f40497e..6e72cef47fe 100644 --- a/gamestonk_terminal/stocks/fundamental_analysis/dcf_view.py +++ b/gamestonk_terminal/stocks/fundamental_analysis/dcf_view.py @@ -3,8 +3,6 @@ __docformat__ = "numpy" from typing import List, Union, Dict, Any from datetime import datetime -from pathlib import Path -import os from openpyxl.styles.numbers import FORMAT_PERCENTAGE_00 from openpyxl import Workbook @@ -102,22 +100,15 @@ class CreateExcelFA: if self.info["audit"]: self.run_audit() - trypath = os.path.join( - os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")), - "exports", - "stocks", - "fundamental_analysis", - f"{self.info['ticker']} {self.data['now']}.xlsx", - ) + i = 0 + while True: + path = dcf_model.generate_path(i, self.info["ticker"], self.data["now"]) - my_file = Path(trypath) - if my_file.is_file(): - console.print("Analysis already ran. Please move file to rerun.") - else: - self.wb.save(trypath) - console.print( - f"Analysis ran for {self.info['ticker']}\nFile in {trypath}.\n" - ) + if not path.is_file(): + self.wb.save(path) + console.print(f"Analysis for {self.info['ticker']} At:\n{path}.\n") + break + i += 1 def get_data(self, statement: str, row: int, header: bool) -> pd.DataFrame: df, rounding = dcf_model.create_dataframe(self.info["ticker"], statement)