|
| 1 | +"""CLI command: fetch data and evaluate a pandas expression.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import typer |
| 8 | + |
| 9 | +from eia.cli._output import render_result |
| 10 | +from eia.cli.get_cmd import _parse_facets |
| 11 | + |
| 12 | + |
| 13 | +def exec_command( |
| 14 | + route: str = typer.Argument(..., help="Route path to a data endpoint"), |
| 15 | + start: Optional[str] = typer.Option(None, "--start", "-s", help="Start date (e.g. 2024-01-01)"), |
| 16 | + end: Optional[str] = typer.Option(None, "--end", "-e", help="End date (e.g. 2024-01-31)"), |
| 17 | + frequency: Optional[str] = typer.Option(None, "--frequency", help="Data frequency (e.g. hourly, monthly)"), |
| 18 | + facet: Optional[list[str]] = typer.Option(None, "--facet", help="Facet filter as key=value (repeatable)"), |
| 19 | + data: Optional[list[str]] = typer.Option(None, "--data", "-d", help="Data column to include (repeatable)"), |
| 20 | + expr: str = typer.Option("df", "--expr", "-x", help="Python expression to evaluate (df, pd, np available)"), |
| 21 | + format: str = typer.Option("table", "--format", "-f", help="Output format: table, csv, json"), |
| 22 | + output: Optional[str] = typer.Option(None, "--output", "-o", help="Output file path"), |
| 23 | + api_key: Optional[str] = typer.Option(None, "--api-key", "-k", help="EIA API key"), |
| 24 | +): |
| 25 | + """Fetch data and evaluate a Python expression on it. |
| 26 | +
|
| 27 | + The fetched data is available as `df` (pandas DataFrame). |
| 28 | + `pd` (pandas) and `np` (numpy) are also available. |
| 29 | +
|
| 30 | + \b |
| 31 | + Examples: |
| 32 | + eia exec electricity/rto/fuel-type-data \\ |
| 33 | + --start 2024-06-01 --end 2024-06-08 \\ |
| 34 | + --frequency hourly \\ |
| 35 | + --facet respondent=CISO --data value \\ |
| 36 | + -x "df.groupby('fueltype')['value'].mean()" |
| 37 | +
|
| 38 | + eia exec electricity/rto/fuel-type-data \\ |
| 39 | + --start 2024-06-01 --end 2024-06-03 \\ |
| 40 | + --frequency hourly \\ |
| 41 | + --facet respondent=CISO --data value \\ |
| 42 | + -x "df.describe()" |
| 43 | + """ |
| 44 | + import numpy as np |
| 45 | + import pandas as pd |
| 46 | + |
| 47 | + from eia.cli.app import get_client |
| 48 | + |
| 49 | + client = get_client(api_key) |
| 50 | + |
| 51 | + # Build the Data object |
| 52 | + data_endpoint = client.get_data_endpoint(route) |
| 53 | + |
| 54 | + # Parse facets |
| 55 | + facets = _parse_facets(facet) if facet else None |
| 56 | + |
| 57 | + # Fetch |
| 58 | + df = data_endpoint.get( |
| 59 | + data_columns=data or None, |
| 60 | + facets=facets, |
| 61 | + frequency=frequency, |
| 62 | + start=start, |
| 63 | + end=end, |
| 64 | + ) |
| 65 | + |
| 66 | + if df.empty: |
| 67 | + typer.echo("No data returned.") |
| 68 | + raise typer.Exit(0) |
| 69 | + |
| 70 | + # Evaluate expression |
| 71 | + namespace = {"df": df, "pd": pd, "np": np} |
| 72 | + try: |
| 73 | + result = eval(expr, {"__builtins__": {}}, namespace) # noqa: S307 |
| 74 | + except Exception as exc: |
| 75 | + typer.echo(f"Error evaluating expression: {exc}", err=True) |
| 76 | + raise typer.Exit(1) |
| 77 | + |
| 78 | + render_result(result, format=format, output=output) |
0 commit comments