# Buggy code
def fetch_NO():
    url = "http://driftsdata.statnett.no/restapi/ProductionConsumption/GetLatestDetailedOverview"

    data = requests.get(url).json()
    countries = map(lambda x: x["value"], data["Headers"])
    i = countries.index(COUNTRY_CODE)

    obj = {
        "countryCode": COUNTRY_CODE,
        "datetime": arrow.get(data["MeasuredAt"] / 1000).datetime,  # time given in UTC
    }
    obj["consumption"] = {
        "unknown": float(
            data["ConsumptionData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        )
    }
    obj["exchange"] = {}
    obj["production"] = {
        "solar": 0,
        "unknown": float(
            data["ThermalData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        )
        + float(
            data["NotSpecifiedData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "wind": float(
            data["WindData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "nuclear": float(
            data["NuclearData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "hydro": float(
            data["HydroData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
    }

    return obj


# Traceback (IndexError)
Traceback (most recent call last):
File "feeder.py", line 51, in fetch_countries
obj = parser()
File "/home/parsers/PL.py", line 29, in fetch_PL
output_array = map(fetchValue, parameters)
File "/home/parsers/PL.py", line 21, in fetchValue
last = soup.find_all('point')[-1].find_all('quantity')[-1]
IndexError: list index out of range

# Fixed code
def fetch_NO(session=None):
    url = "http://driftsdata.statnett.no/restapi/ProductionConsumption/GetLatestDetailedOverview"

    data = (session or requests).get(url).json()
    countries = map(lambda x: x["value"], data["Headers"])
    i = countries.index(COUNTRY_CODE)

    obj = {
        "countryCode": COUNTRY_CODE,
        "datetime": arrow.get(data["MeasuredAt"] / 1000).datetime,  # time given in UTC
    }
    obj["consumption"] = {
        "unknown": float(
            data["ConsumptionData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        )
    }
    obj["exchange"] = {}
    obj["production"] = {
        "solar": 0,
        "unknown": float(
            data["ThermalData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        )
        + float(
            data["NotSpecifiedData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "wind": float(
            data["WindData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "nuclear": float(
            data["NuclearData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
        "hydro": float(
            data["HydroData"][i]["value"]
            .replace("\xa0", "")
            .replace(" ", "")
            .replace("-", "0")
        ),
    }

    return obj