Skip to content

Commit 9605e1e

Browse files
Add caching for Nordic country energy mix data
- Load and cache Nordic country energy mix data in _load_static_data() - Add get_nordic_country_energy_mix_data() method to retrieve cached data - This addresses the caching performance request in PR #1039
1 parent a5ff78f commit 9605e1e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

codecarbon/input.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def _load_static_data() -> None:
6161
_CACHE["cpu_power"] = pd.read_csv(path)
6262

6363

64+
# Nordic country energy mix - used for emissions calculations
65+
path = _get_resource_path("data/private_infra/nordic_country_energy_mix.json")
66+
with open(path) as f:
67+
_CACHE["nordic_country_energy_mix"] = json.load(f)
68+
6469
# Load static data at module import
6570
_load_static_data()
6671

@@ -189,6 +194,13 @@ def get_cpu_power_data(self) -> pd.DataFrame:
189194
"""
190195
return _CACHE["cpu_power"]
191196

197+
def get_nordic_country_energy_mix_data(self) -> Dict:
198+
"""
199+
Returns Nordic Country Energy Mix Data.
200+
Data is cached on first access per country.
201+
"""
202+
return _CACHE["nordic_country_energy_mix"]
203+
192204

193205
class DataSourceException(Exception):
194206
pass

0 commit comments

Comments
 (0)