fix po dates params (#3619)

Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Chavithra <chavithra@gmail.com>
This commit is contained in:
montezdesousa
2022-11-28 18:19:30 +00:00
committed by GitHub
parent d5d8e71e15
commit d8c46b0aef
2 changed files with 48 additions and 4 deletions

View File

@@ -1,8 +1,10 @@
import argparse
from typing import Dict
from datetime import datetime
from typing import Dict, List
from pathlib import Path
from openbb_terminal.helper_funcs import log_and_raise
from openbb_terminal.core.config import paths
from openbb_terminal.rich_config import console
def check_save_file(file: str) -> str:
@@ -40,3 +42,40 @@ def load_data_files() -> Dict[str, Path]:
data_files[filepath.name] = filepath
return data_files
def check_convert_dates(params: dict, param_name_list: List[str]) -> dict:
"""Check if the argument is in the list and convert it to a str object
Parameters
----------
params : dict
The parameters to be converted
param_name_list : List[str]
The list of arguments to be converted
Returns
-------
dict
The converted parameters
"""
for param_name in param_name_list:
if param_name in params:
param_value = params[param_name]
if isinstance(param_value, datetime):
params[param_name] = param_value.strftime("%Y-%m-%d")
elif isinstance(param_value, str):
try:
param_value = datetime.strptime(param_value, "%Y-%m-%d")
except ValueError:
console.print(
f"[red]'{param_name}' format is not a valid date, must be YYYY-MM-DD.\n[/red]"
)
params.pop(param_name)
else:
console.print(
f"[red]'{param_name}' format is not a valid date, must be YYYY-MM-DD.\n[/red]"
)
params.pop(param_name)
return params

View File

@@ -8,6 +8,9 @@ from openbb_terminal.helper_funcs import print_rich_table
from openbb_terminal.portfolio.portfolio_optimization import excel_model
from openbb_terminal.rich_config import console
from openbb_terminal.portfolio.portfolio_optimization.parameters import params_statics
from openbb_terminal.portfolio.portfolio_optimization.parameters.params_helpers import (
check_convert_dates,
)
from openbb_terminal.core.config import paths
@@ -41,11 +44,13 @@ def load_file(path: str = "") -> Tuple[dict, str]:
params, _ = excel_model.load_configuration(path)
current_model = params["technique"]
else:
console.print(
"Can not load in the file due to not being an .ini or .xlsx file."
)
console.print("Cannot load in the file due to not being an .ini or .xlsx file.")
return {}, ""
params = check_convert_dates(
params=params, param_name_list=["start_period", "end_period"]
)
max_len = max(len(k) for k in params.keys())
help_text = "[info]Parameters:[/info]\n"