def fetch_island_data(zone_key, session): if zone_key == "ES-IB-FO": formentera_data = Formentera(session, verify=False).get_all() if not formentera_data: raise ParserException(zone_key, "Formentera doesn't respond") else: return formentera_data elif zone_key == "ES-IB-IZ": ibiza_data = Ibiza(session, verify=False).get_all() if not ibiza_data: raise ParserException(zone_key, "Party is over, Ibiza doesn't respond") else: return ibiza_data elif zone_key == "ES-IB-MA": mallorca_data = Mallorca(session, verify=False).get_all() if not mallorca_data: raise ParserException(zone_key, "Mallorca doesn't respond") else: return mallorca_data elif zone_key == "ES-IB-ME": menorca_data = Menorca(session, verify=False).get_all() if not menorca_data: raise ParserException(zone_key, "Menorca doesn't respond") else: return menorca_data elif zone_key == "ES-IB": balearic_islands = BalearicIslands(session, verify=False).get_all() if not balearic_islands: raise ParserException(zone_key, "Balearic Islands doesn't respond") else: return balearic_islands else: raise ParserException( zone_key, "Can't read this country code {0}".format(zone_key))
def fetch_exchange(zone_key1, zone_key2, session=None, target_datetime=None, logger=None) -> list: if target_datetime: raise NotImplementedError( "This parser is not yet able to parse past dates") sorted_zone_keys = "->".join(sorted([zone_key1, zone_key2])) ses = session or Session() if sorted_zone_keys == "ES->ES-IB": responses = BalearicIslands(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB", "No responses") elif (sorted_zone_keys == "ES->ES-IB-MA" or sorted_zone_keys == "ES-IB-MA->ES-IB-ME" or sorted_zone_keys == "ES-IB-IZ->ES-IB-MA"): responses = Mallorca(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB-MA", "No responses") elif sorted_zone_keys == "ES-IB-FO->ES-IB-IZ": responses = Formentera(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB-FO", "No responses") else: raise NotImplementedError("This exchange pair is not implemented") exchanges = [] for response in responses: if sorted_zone_keys == "ES-IB-MA->ES-IB-ME": net_flow = -1 * response.link["ma_me"] elif sorted_zone_keys == "ES-IB-IZ->ES-IB-MA": net_flow = response.link["ma_ib"] elif sorted_zone_keys == "ES-IB-FO->ES-IB-IZ": net_flow = -1 * response.link["ib_fo"] else: net_flow = response.link["pe_ma"] exchange = { "sortedZoneKeys": sorted_zone_keys, "datetime": get(response.timestamp).datetime, "netFlow": net_flow, "source": "demanda.ree.es", } exchanges.append(exchange) return exchanges
def fetch_exchange(zone_key1, zone_key2, session=None, target_datetime=None, logger=None) -> list: if target_datetime: raise NotImplementedError( 'This parser is not yet able to parse past dates') sorted_zone_keys = '->'.join(sorted([zone_key1, zone_key2])) ses = session or Session() if sorted_zone_keys == 'ES->ES-IB': responses = BalearicIslands(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB", "No responses") elif sorted_zone_keys == 'ES->ES-IB-MA' or sorted_zone_keys == 'ES-IB-MA->ES-IB-ME' or sorted_zone_keys == 'ES-IB-IZ->ES-IB-MA': responses = Mallorca(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB-MA", "No responses") elif sorted_zone_keys == 'ES-IB-FO->ES-IB-IZ': responses = Formentera(ses, verify=False).get_all() if not responses: raise ParserException("ES-IB-FO", "No responses") else: raise NotImplementedError('This exchange pair is not implemented') exchanges = [] for response in responses: if sorted_zone_keys == 'ES-IB-MA->ES-IB-ME': net_flow = -1 * response.link['ma_me'] elif sorted_zone_keys == 'ES-IB-IZ->ES-IB-MA': net_flow = response.link['ma_ib'] elif sorted_zone_keys == 'ES-IB-FO->ES-IB-IZ': net_flow = -1 * response.link['ib_fo'] else: net_flow = response.link['pe_ma'] exchange = { 'sortedZoneKeys': sorted_zone_keys, 'datetime': get(response.timestamp).datetime, 'netFlow': net_flow, 'source': 'demanda.ree.es', } exchanges.append(exchange) return exchanges
class TestMallorca(unittest.TestCase): def setUp(self): self.instance = Mallorca() self.date = get('2022-06-20').format('YYYY-MM-DD') def test_instance(self): self.assertIsInstance(self.instance, Mallorca) def test_get(self): response = self.instance.get(self.date) self.assertIsInstance(response, Response) self.assertIsNotNone(response.timestamp) self.assertEqual(response.demand, 480.2) self.assertEqual(71.3, response.carbon) self.assertEqual(50.9, response.waste) self.assertEqual(42.7, response.link['pe_ma']) self.assertEqual(-2.7, response.link['ma_me']) self.assertEqual(-86.9, response.link['ma_ib']) def test_get_all(self): responses = self.instance.get_all() self.assertIsNotNone(responses)
def setUp(self): self.instance = Mallorca() self.date = get('2022-06-20').format('YYYY-MM-DD')