コード例 #1
0
def display_gdp_capita(
    start_year: int = 2010,
    raw: bool = False,
    export: str = "",
    external_axes: Optional[List[plt.Axes]] = None,
):
    """Display US GDP per Capita from AlphaVantage

    Parameters
    ----------
    start_year : int, optional
        Start year for plot, by default 2010
    raw : bool, optional
        Flag to show raw data, by default False
    export : str, optional
        Format to export data, by default
    external_axes : Optional[List[plt.Axes]], optional
        External axes (1 axis is expected in the list), by default None
    """
    gdp_capita = alphavantage_model.get_gdp_capita()
    if gdp_capita.empty:
        console.print("Error getting data.  Check API Key")
        return
    gdp = gdp_capita[gdp_capita.date >= f"{start_year}-01-01"]

    # This plot has 1 axis
    if not external_axes:
        _, ax = plt.subplots(figsize=plot_autoscale(), dpi=cfp.PLOT_DPI)
    else:
        if len(external_axes) != 1:
            logger.error("Expected list of one axis item.")
            console.print("[red]Expected list of one axis item./n[/red]")
            return
        (ax, ) = external_axes

    ax.plot(gdp.date, gdp.GDP, marker="o")
    ax.set_title(f"US GDP per Capita (Chained 2012 USD) from {start_year}")
    ax.set_ylabel("US GDP (Chained 2012 USD) ")
    theme.style_primary_axis(ax)
    if external_axes is None:
        theme.visualize_output()

    export_data(
        export,
        os.path.dirname(os.path.abspath(__file__)),
        "gdpc",
        gdp_capita,
    )
    if raw:
        print_rich_table(
            gdp.head(20),
            headers=["Date", "GDP"],
            show_index=False,
            title="US GDP Per Capita",
        )
        console.print("")
コード例 #2
0
def test_get_gdp_capita_no_response(mocker):
    # MOCK GET
    mock_response = requests.Response()
    mock_response.status_code = 400
    mocker.patch(target="requests.get",
                 new=mocker.Mock(return_value=mock_response))

    result_df = alphavantage_model.get_gdp_capita()

    assert isinstance(result_df, pd.DataFrame)
    assert result_df.empty
コード例 #3
0
def display_gdp_capita(start_year: int = 2010,
                       raw: bool = False,
                       export: str = ""):
    """Display US GDP per Capita from AlphaVantage

    Parameters
    ----------
    start_year : int, optional
        Start year for plot, by default 2010
    raw : bool, optional
        Flag to show raw data, by default False
    export : str, optional
        Format to export data, by default ""
    """
    gdp_capita = alphavantage_model.get_gdp_capita()
    if gdp_capita.empty:
        print("Error getting data.  Check API Key")
        return
    gdp = gdp_capita[gdp_capita.date >= f"{start_year}-01-01"]
    fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=cfp.PLOT_DPI)
    ax.plot(gdp.date, gdp.GDP, marker="o", c="dodgerblue")
    ax.set_xlabel("Date")
    ax.set_title(f"US GDP per Capita (Chained 2012 USD) from {start_year}")
    ax.set_ylabel("US GDP (Chained 2012 USD)  ")
    ax.grid("on")
    fig.tight_layout()
    if gtff.USE_ION:
        plt.ion()
    plt.show()

    export_data(
        export,
        os.path.dirname(os.path.abspath(__file__)),
        "gdpc",
        gdp_capita,
    )
    if raw:
        if gtff.USE_TABULATE_DF:
            print(
                tabulate(
                    gdp.head(20),
                    headers=["Date", "GDP"],
                    tablefmt="fancy_grid",
                    showindex=False,
                ))
        else:
            print(gdp.head(20).to_string())
    print("")
コード例 #4
0
def test_get_gdp_capita(recorder):
    result_df = alphavantage_model.get_gdp_capita()

    recorder.capture(result_df)