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

Add pre-commit configuration and autoformat #75

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file lists commits to be ignored when running git-blame(1)
# Add commit IDs to this when the commit is a trivial change such
# as autoformatting commits

# You can use this file with git-blame by using --ignore-revs-file:
# git blame --ignore-revs-file .git-blame-ignore-revs
# To set this file to be ignored on each git-blame invocation:
# git config blame.ignoreRevsFile .git-blame-ignore-revs
# By default, GitHub will use the set of commits in this file in its Blame view:
# https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view

# initial pre-commit run and ruff autoformatting
9031aafa07bdc3a93a2019648bbf50b894446b70
2 changes: 1 addition & 1 deletion .github/workflows/docs-build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- name: Install cats
- name: Install cats
run: python3 -m pip install .
- run: |
pip install -r docs/docs-requirements.txt
Expand Down
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.2
hooks:
- id: ruff
args: [ --fix ]
- id: ruff-format
2 changes: 1 addition & 1 deletion .vscode/settings.json
abhidg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"python.formatting.provider": "none",
"python.linting.flake8Enabled": true,
"python.linting.enabled": true
}
}
17 changes: 10 additions & 7 deletions cats/CI_api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class InvalidLocationError(Exception):
pass


APIInterface = namedtuple('APIInterface', ['get_request_url', 'parse_response_data'])
APIInterface = namedtuple("APIInterface", ["get_request_url", "parse_response_data"])


def ciuk_request_url(timestamp: datetime, postcode: str):
# This transformation is specific to the CI-UK API.
Expand All @@ -25,7 +26,7 @@ def ciuk_request_url(timestamp: datetime, postcode: str):
else:
dt = timestamp.replace(minute=1, second=0, microsecond=0)

if (len(postcode) > 4):
if len(postcode) > 4:
sys.stderr.write(f"Warning: truncating postcode {postcode} to ")
postcode = postcode[:-3].strip()
sys.stderr.write(f"{postcode}.\n")
Expand All @@ -49,9 +50,10 @@ def ciuk_parse_response_data(response: dict):
:param response:
:return:
"""

def invalid_code(r: dict):
try:
return "postcode" in r['error']['message']
return "postcode" in r["error"]["message"]
except KeyError:
return False

Expand All @@ -65,9 +67,9 @@ def invalid_code(r: dict):

datefmt = "%Y-%m-%dT%H:%MZ"
# The "Z" at the end of the format string indicates UTC,
# however, strptime does not know how to parse this, so we
# however, strptime does not know how to parse this, so we
# need to add tzinfo data.
utc = ZoneInfo('UTC')
utc = ZoneInfo("UTC")
return [
CarbonIntensityPointEstimate(
datetime=datetime.strptime(d["from"], datefmt).replace(tzinfo=utc),
Expand All @@ -76,9 +78,10 @@ def invalid_code(r: dict):
for d in response["data"]["data"]
]


API_interfaces = {
"carbonintensity.org.uk": APIInterface(
get_request_url=ciuk_request_url,
parse_response_data=ciuk_parse_response_data,
),
}
),
}
15 changes: 11 additions & 4 deletions cats/CI_api_query.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import requests_cache
from datetime import datetime, timezone

import requests_cache

from .forecast import CarbonIntensityPointEstimate

def get_CI_forecast(location: str, CI_API_interface) -> list[CarbonIntensityPointEstimate]:

def get_CI_forecast(
location: str, CI_API_interface
) -> list[CarbonIntensityPointEstimate]:
"""
Get carbon intensity from an API

Expand All @@ -16,16 +20,19 @@ def get_CI_forecast(location: str, CI_API_interface) -> list[CarbonIntensityPoin

# Setup a session for the API call. This uses a global HTTP cache
# with the URL as the key. Failed attempts are not cached.
session = requests_cache.CachedSession('cats_cache', use_temp=True)
session = requests_cache.CachedSession("cats_cache", use_temp=True)

# get the carbon intensity api data
r = session.get(CI_API_interface.get_request_url(datetime.now(timezone.utc), location))
r = session.get(
CI_API_interface.get_request_url(datetime.now(timezone.utc), location)
)
data = r.json()

return CI_API_interface.parse_response_data(data)


if __name__ == "__main__":
from .CI_api_interface import API_interfaces

# test example using Manchester as a location
data_tuples = get_CI_forecast("M15", API_interfaces["carbonintensity.org.uk"])
Loading
Loading