Skip to content

Commit

Permalink
Hi Mom
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhou0515 committed Oct 4, 2024
1 parent 85f85ea commit 5066f68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tests/test_carbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from zeus.carbon import (
ElectrictyMapsClient,
get_ip_lat_long,
CarbonIntensityNotFoundError,
ZeusCarbonIntensityNotFoundError,
)


Expand Down Expand Up @@ -100,5 +100,5 @@ def test_get_current_carbon_intensity_no_response(mock_requests):
assert latlong == (pytest.approx(42.2776), pytest.approx(-83.7409))
provider = ElectrictyMapsClient(latlong)

with pytest.raises(CarbonIntensityNotFoundError):
with pytest.raises(ZeusCarbonIntensityNotFoundError):
provider.get_current_carbon_intensity()
10 changes: 5 additions & 5 deletions zeus/carbon.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_ip_lat_long() -> tuple[float, float]:
raise


class CarbonIntensityNotFoundError(ZeusBaseError):
class ZeusCarbonIntensityNotFoundError(ZeusBaseError):
"""Exception when carbon intensity measurement could not be retrieved."""

def __init__(self, message: str) -> None:
Expand All @@ -51,9 +51,7 @@ class ElectrictyMapsClient(CarbonIntensityProvider):
Reference:
1. [ElectricityMaps](https://www.electricitymaps.com/)
2. [ElectricityMaps API](https://static.electricitymaps.com/api/docs/index.html)
3. [ElectricityMaps GitHub](https://github.com/electricitymaps/electricitymaps-contrib)
"""

Expand All @@ -80,6 +78,7 @@ def get_current_carbon_intensity(self) -> float:
!!! Note
In some locations, there is no recent carbon intensity data. `self.estimate` can be used to approximate the carbon intensity in such cases.
"""
resp = None
try:
url = (
f"https://api.electricitymap.org/v3/carbon-intensity/latest?lat={self.lat}&lon={self.long}"
Expand All @@ -90,9 +89,10 @@ def get_current_carbon_intensity(self) -> float:
return resp.json()["carbonIntensity"]
except KeyError as e:
# Raise exception when carbonIntensity does not exist in response
raise CarbonIntensityNotFoundError(
raise ZeusCarbonIntensityNotFoundError(
f"Recent carbon intensity measurement not found at `({self.lat}, {self.long})` "
f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`"
f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`\n"
f"JSON Response: {resp.text}"
) from e
except requests.exceptions.RequestException as e:
logger.exception(
Expand Down

0 comments on commit 5066f68

Please sign in to comment.