Refactoring Fred menu as a controller

This commit is contained in:
Artem Veremey
2021-04-01 09:20:25 -07:00
parent b6abcb68a7
commit 7862b39ce0
5 changed files with 195 additions and 94 deletions

View File

@@ -0,0 +1 @@
from . import fred_controller # noqa: F401

View File

@@ -26,6 +26,7 @@ api_map = {
"usdcad": "DEXCAUS",
"unemp": "UNRATE",
}
title_map = {
"gdp": "Gross Domestic Product",
"t10": "10-Year Treasury Constant Maturity Rate",
@@ -47,8 +48,8 @@ def get_fred_data(l_args, choice):
add_help=False,
prog="Custom",
description="""
Custom Data
""",
Custom Data
""",
)
parser.add_argument(

View File

@@ -0,0 +1,189 @@
""" Disc Controller """
__docformat__ = "numpy"
import argparse
from typing import List
from matplotlib import pyplot as plt
from prompt_toolkit.completion import NestedCompleter
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.helper_funcs import get_flair
from gamestonk_terminal.menu import session
from gamestonk_terminal.fred import fred_api
class FredCotroller:
""" Fred Controller """
# Command choices
CHOICES = [
"help",
"q",
"quit",
"gdp",
"unemp",
"t1",
"t5",
"t10",
"t30",
"mort30",
"fedrate",
"moodAAA",
"usdcad",
"cust",
]
def __init__(self):
"""Constructor"""
self.disc_parser = argparse.ArgumentParser(add_help=False, prog="fred")
self.disc_parser.add_argument(
"cmd",
choices=self.CHOICES,
)
@staticmethod
def print_help():
""" Print help """
print("\nFred Economic Data:")
print(" help show this menu again")
print(" q quit this menu, and shows back to main menu")
print(" quit quit to abandon program")
print(" ")
print(" gdp GDP")
print(" unemp Unemployment Rate")
print(" t1 1-Year Treasury Constant Maturity Rate")
print(" t5 5-Year Treasury Constant Maturity Rate")
print(" t10 10-Year Treasury Constant Maturity Rate")
print(" t30 30-Year Treasury Constant Maturity Rate")
print(" mort30 30-Year Fixed Rate Mortgage Average")
print(" fedrate Effective Federal Funds Rate")
print(" moodAAA Moody's Seasoned AAA Corporate Bond Yield")
print(" usdcad Canada / U.S. Foreign Exchange Rate (CAD per 1 USD)")
print("")
print(" cust User Specified FRED Data - Please Specify --id")
print("")
return
def switch(self, an_input: str):
"""Process and dispatch input
Returns
-------
True, False or None
False - quit the menu
True - quit the program
None - continue in the menu
"""
(known_args, other_args) = self.disc_parser.parse_known_args(an_input.split())
fred_data_list = [
"gdp",
"unemp",
"t1",
"t5",
"t10",
"t30",
"mort30",
"fedrate",
"moodAAA",
"usdcad",
]
if known_args.cmd in fred_data_list:
return getattr(self, "call_fred_api")(other_args, known_args.cmd)
return getattr(
self, "call_" + known_args.cmd, lambda: "Command not recognized!"
)(other_args)
def call_help(self, _):
"""Process Help command"""
self.print_help()
def call_q(self, _):
"""Process Q command - quit the menu"""
return False
def call_quit(self, _):
"""Process Quit command - quit the program"""
return True
def call_fred_api(self, other_args: List[str], cmd: str):
"""Process Fred Data call"""
fred_api.get_fred_data(other_args, cmd)
def call_cust(self, other_args: List[str]):
"""Process cust call"""
fred_api.custom_data(other_args)
def menu():
"""Fred Menu"""
fred_controller = FredCotroller()
plt.close("all")
# fred_parser = argparse.ArgumentParser(prog="fred", add_help=False)
# defined_choices = [
# "gdp",
# "unemp",
# "t1",
# "t5",
# "t10",
# "t30",
# "mort30",
# "fedrate",
# "moodAAA",
# "usdcad",
# ]
# choices = ["help", "q", "quit", "cust"] + defined_choices
# fred_parser.add_argument("cmd", choices=choices)
# completer = NestedCompleter.from_nested_dict({c: None for c in choices})
fred_controller.print_help()
# Loop forever and ever
while True:
# Get input command from user
if session and gtff.USE_PROMPT_TOOLKIT:
completer = NestedCompleter.from_nested_dict(
{c: None for c in fred_controller.CHOICES}
)
an_input = session.prompt(
f"{get_flair()} (fred)> ",
completer=completer,
)
else:
an_input = input(f"{get_flair()} (fred)> ")
try:
process_input = fred_controller.switch(an_input)
if process_input is not None:
return process_input
except SystemExit:
print("The command selected doesn't exist\n")
continue
# except SystemExit:
# print("The command selected doesn't exist\n")
# continue
# if ns_known_args.cmd == "help":
# should_print_help = True
# elif ns_known_args.cmd == "q":
# # Leave the fred menu
# return False
# elif ns_known_args.cmd == "quit":
# # Abandon the program
# return True
# elif ns_known_args.cmd in defined_choices:
# fred_api.get_fred_data(l_args, ns_known_args.cmd)
# elif ns_known_args.cmd == "cust":
# fred_api.custom_data(l_args)

View File

@@ -1,90 +0,0 @@
import argparse
from matplotlib import pyplot as plt
from prompt_toolkit.completion import NestedCompleter
from gamestonk_terminal import feature_flags as gtff
from gamestonk_terminal.helper_funcs import get_flair
from gamestonk_terminal.menu import session
from gamestonk_terminal.fred import fred_api
def print_fred():
""" Print help """
print("\nFred Economic Data:")
print(" help show this menu again")
print(" q quit this menu, and shows back to main menu")
print(" quit quit to abandon program")
print(" ")
print(" gdp GDP")
print(" unemp Unemployment Rate")
print(" t1 1-Year Treasury Constant Maturity Rate")
print(" t5 5-Year Treasury Constant Maturity Rate")
print(" t10 10-Year Treasury Constant Maturity Rate")
print(" t30 30-Year Treasury Constant Maturity Rate")
print(" mort30 30-Year Fixed Rate Mortgage Average")
print(" fedrate Effective Federal Funds Rate")
print(" moodAAA Moody's Seasoned AAA Corporate Bond Yield")
print(" usdcad Canada / U.S. Foreign Exchange Rate (CAD per 1 USD)")
print("")
print(" cust User Specified FRED Data - Please Specify --id")
print("")
return
def fred_menu():
plt.close("all")
fred_parser = argparse.ArgumentParser(prog="fred", add_help=False)
defined_choices = [
"gdp",
"unemp",
"t1",
"t5",
"t10",
"t30",
"mort30",
"fedrate",
"moodAAA",
"usdcad",
]
choices = ["help", "q", "quit", "cust"] + defined_choices
fred_parser.add_argument("cmd", choices=choices)
completer = NestedCompleter.from_nested_dict({c: None for c in choices})
should_print_help = True
while True:
if should_print_help:
print_fred()
should_print_help = False
# Get input command from user
if session and gtff.USE_PROMPT_TOOLKIT:
as_input = session.prompt(
f"{get_flair()} (fred)> ",
completer=completer,
)
else:
as_input = input(f"{get_flair()} (fred)> ")
try:
(ns_known_args, l_args) = fred_parser.parse_known_args(as_input.split())
except SystemExit:
print("The command selected doesn't exist\n")
continue
if ns_known_args.cmd == "help":
should_print_help = True
elif ns_known_args.cmd == "q":
# Leave the fred menu
return False
elif ns_known_args.cmd == "quit":
# Abandon the program
return True
elif ns_known_args.cmd in defined_choices:
fred_api.get_fred_data(l_args, ns_known_args.cmd)
elif ns_known_args.cmd == "cust":
fred_api.custom_data(l_args)

View File

@@ -26,7 +26,7 @@ from gamestonk_terminal.technical_analysis import ta_controller
from gamestonk_terminal.comparison_analysis import ca_controller
from gamestonk_terminal.exploratory_data_analysis import eda_controller
from gamestonk_terminal.options import op_controller
from gamestonk_terminal.fred import fred_menu as fm
from gamestonk_terminal.fred import fred_controller
from gamestonk_terminal.residuals_analysis import ra_controller
from gamestonk_terminal.portfolio import port_controller
from gamestonk_terminal.cryptocurrency import crypto_controller
@@ -231,7 +231,7 @@ def main():
b_quit = op_controller.menu(s_ticker)
elif ns_known_args.opt == "fred":
b_quit = fm.fred_menu()
b_quit = fred_controller.menu()
elif ns_known_args.opt == "pa":
b_quit = port_controller.menu()