Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rework lib to add static typing #6

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"
os:
Expand Down
56 changes: 7 additions & 49 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "pyomie"
version = "0.1.0"
description = "A client for OMIE - Spain and Portugal electricity market data"
authors = ["Luis Miranda <luuuis@fastmail.com>"]
authors = ["Luis Miranda <soft.road8065@fastmail.com>"]
license = "Apache Software License 2.0"
readme = "README.md"
repository = "https://github.com/luuuis/pyomie"
Expand All @@ -17,6 +17,7 @@ classifiers = [
packages = [
{ include = "pyomie", from = "src" },
]
keywords = ["omie", "electricity", "spain", "portugal", "MIBEL"]

[tool.poetry.urls]
"Bug Tracker" = "https://github.com/luuuis/pyomie/issues"
Expand All @@ -26,7 +27,7 @@ packages = [
pyomie = "pyomie.cli:app"

[tool.poetry.dependencies]
python = "^3.10"
python = "^3.11"
rich = ">=10"
typer = {extras = ["all"], version = "^0.9.0"}
aiohttp = "^3.9.1"
Expand Down
28 changes: 17 additions & 11 deletions src/pyomie/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datetime as dt
import json
import sys
from typing import Awaitable, Callable
from typing import Awaitable, Callable, NamedTuple, TypeVar

import aiohttp
import typer
Expand All @@ -13,12 +13,14 @@
from pyomie.main import adjustment_price, spot_price
from pyomie.model import OMIEResults

_NamedTupleT = TypeVar("_NamedTupleT", bound=NamedTuple)

app = typer.Typer()

_DATE_DEFAULT = "today's date"


def _parse_iso8601_date(a_date: str) -> dt.date:
def _parse_date_arg(a_date: str) -> dt.date:
if a_date is _DATE_DEFAULT:
return dt.date.today()
else:
Expand All @@ -30,44 +32,48 @@ def spot(
date: dt.date = typer.Argument( # noqa: B008
default=_DATE_DEFAULT,
help="Date to fetch in YYYY-MM-DD format",
parser=_parse_iso8601_date,
parser=_parse_date_arg,
),
csv: bool = typer.Option(
default=False, help="Print the CSV as returned by OMIE, without parsing."
),
) -> None:
"""Fetches the OMIE spot price data."""
_sync_fetch_and_print(spot_price, date, csv)
_fetch_and_print(spot_price, date, csv)


@app.command()
def adjustment(
date: dt.date = typer.Argument( # noqa: B008
default=_DATE_DEFAULT,
help="Date to fetch in YYYY-MM-DD format",
parser=_parse_iso8601_date,
parser=_parse_date_arg,
),
csv: bool = typer.Option(
default=False, help="Print the CSV as returned by OMIE, without parsing."
),
) -> None:
"""Fetches the OMIE adjustment mechanism data."""
_sync_fetch_and_print(adjustment_price, date, csv)
_fetch_and_print(adjustment_price, date, csv)


def _sync_fetch_and_print(
def _fetch_and_print(
fetch_omie_data: Callable[
[ClientSession, dt.date], Awaitable[tuple[OMIEResults, str] | None]
[ClientSession, dt.date], Awaitable[OMIEResults[_NamedTupleT] | None]
],
market_date: dt.date,
raw: bool,
print_raw: bool,
) -> None:
async def fetch_and_print() -> None:
async with aiohttp.ClientSession() as session:
fetched_data = await fetch_omie_data(session, market_date)
if fetched_data:
parsed, unparsed = fetched_data
sys.stdout.write(unparsed if raw else json.dumps(parsed.contents))
# noinspection PyProtectedMember
sys.stdout.write(
fetched_data.raw
if print_raw
else json.dumps(fetched_data.contents._asdict())
)

asyncio.get_event_loop().run_until_complete(fetch_and_print())

Expand Down
Loading
Loading