Exemplo n.º 1
0
    def get_private_infra_emissions(self, energy: Energy,
                                    geo: GeoMetadata) -> float:
        """
        Computes emissions for private infra
        :param energy: Mean power consumption of the process (kWh)
        :param geo: Country and region metadata
        :return: CO2 emissions in kg
        """
        if self._co2_signal_api_token:
            try:
                return co2_signal.get_emissions(energy, geo,
                                                self._co2_signal_api_token)
            except Exception as e:
                logger.error("co2_signal.get_emissions: " + str(e) +
                             " >>> Using CodeCarbon's data.")

        compute_with_regional_data: bool = (geo.region is not None) and (
            geo.country_iso_code.upper() in ["USA", "CAN"])

        if compute_with_regional_data:
            try:
                return self.get_region_emissions(energy, geo)
            except Exception as e:
                logger.error(e, exc_info=True)
                logger.warning("Regional emissions retrieval failed." +
                               " Falling back on country emissions.")
        return self.get_country_emissions(energy, geo)
Exemplo n.º 2
0
    def get_private_infra_emissions(self, energy: Energy,
                                    geo: GeoMetadata) -> float:
        """
        Computes emissions for private infra
        :param energy: Mean power consumption of the process (kWh)
        :param geo: Country and region metadata
        :return: CO2 emissions in kg
        """
        if co2_signal.is_available():
            try:
                return co2_signal.get_emissions(energy, geo)
            except Exception as e:
                logger.error(e)

        compute_with_regional_data: bool = (geo.region is not None) and (
            geo.country_iso_code.upper() in ["USA", "CAN"])

        if compute_with_regional_data:
            try:
                return self.get_region_emissions(energy, geo)
            except Exception as e:
                logger.error(e)
                logger.warning(
                    "CODECARBON : Regional emissions retrieval failed. Falling back on country emissions."
                )
        return self.get_country_emissions(energy, geo)
Exemplo n.º 3
0
 def test_get_emissions_RUNS(self):
     responses.add(
         responses.GET,
         co2_signal.URL,
         json={
             "status": "ok",
             "countryCode": "FR",
             "data": {
                 "carbonIntensity": 58.765447504659946,
                 "fossilFuelPercentage": 8.464454194205029,
             },
             "units": {"carbonIntensity": "gCO2eq/kWh"},
         },
         status=200,
     )
     result = co2_signal.get_emissions(self._energy, self._geo)
     assert round(result, 5) == 0.58765
Exemplo n.º 4
0
 def test_get_emissions_TIMEOUT(self):
     with self.assertRaises(requests.exceptions.ConnectionError):
         co2_signal.get_emissions(self._energy, self._geo, timeout=0.000001)
Exemplo n.º 5
0
 def test_get_emissions_TIMEOUT(self):
     with self.assertRaises(
         (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout)
     ):
         co2_signal.get_emissions(self._energy, self._geo)