Example #1
0
 def calculate_regional_average(cls, val_type, datadict, iso3):
     countryinfo = Country.get_country_info_from_iso3(iso3)
     level = 3
     while level != 0:
         region_level = cls.region_levels[level]
         region_prefix = region_level
         column = Column.parse('#region+code+%s' % region_prefix)
         regioncode = countryinfo[column.get_display_tag(
             sort_attributes=True)]
         if regioncode:
             regioncode = int(regioncode)
             column = Column.parse('#region+%s+name+preferred' %
                                   region_prefix)
             regionname = countryinfo[column.get_display_tag(
                 sort_attributes=True)]
             countries_in_region = Country.get_countries_in_region(
                 regioncode)
             avg = cls.calculate_average(datadict, countries_in_region)
             if avg:
                 logger.warning('%s: %s - Using %s (%s) average' %
                                (iso3, val_type, regionname, region_level))
                 return avg, regioncode
         level -= 1
     logger.warning('%s: %s - Using global average' % (iso3, val_type))
     return cls.calculate_average(datadict), '001'
 def test_get_countries_in_region(self):
     assert Country.get_countries_in_region('Eastern Asia') == ['CHN', 'HKG', 'JPN', 'KOR', 'MAC',
                                                                                'MNG', 'PRK', 'TWN']
     assert len(Country.get_countries_in_region('Africa')) == 60
     assert Country.get_countries_in_region(13) == ['BLZ', 'CRI', 'GTM', 'HND', 'MEX', 'NIC', 'PAN',
                                                                    'SLV']
     assert Country.get_countries_in_region('Channel Islands') == ['GGY', 'JEY']
     assert len(Country.get_countries_in_region('NOTEXIST')) == 0
     with pytest.raises(LocationError):
         Country.get_countries_in_region('NOTEXIST', exception=LocationError)
def check_regions(iso_list: list) -> list:
    """ check which regions are available in dataset or if it has data for all continents -> World
    Args:
        iso_list: list of available countries as iso-codes
    Returns:
        A set with regions available in the dataset
    """
    regions = set()
    if iso_list:
        for cc in REGION_CODE.keys():
            [
                regions.add(REGION_CODE[cc]) for iso in iso_list
                if iso in Country.get_countries_in_region(cc, use_live=False)
            ]
        if regions == set(REGION_CODE.values()):
            return ["Welt"]
        if not regions:
            logging.info(
                "geographischer Geltungsbereich -> keine Regionen erkannt")
            return ["N/A"]
    else:
        return ["N/A"]
    return list(sorted(regions))
Example #4
0
from hdx.hdx_configuration import Configuration
from hdx.data.dataset import Dataset
from hdx.location.country import Country
import os

# Setup hdx access
conf = Configuration.create(hdx_site='prod', user_agent='A_Quick_Example', hdx_read_only=True, project_config_dict = {})
# Search for datasets with the keyword we want
datasets = Dataset.search_in_hdx('Requirements and Funding Data', rows=800)
# Get a list of the names of the actual csv-files
resources = Dataset.get_all_resources(datasets)
# Get all the three-digit country codes
africaCodes = [x.lower() for x in Country.get_countries_in_region('Africa')]

# Delete current files before getting new versions
filelist = [ f for f in os.listdir("csv") if f.endswith(".CSV") ]
for f in filelist:
    os.remove(os.path.join("csv", f))

# Download all the files that match the naming pattern of the files we want
for resource in resources:
    for countryCode in africaCodes:
        if "fts_requirements_funding_" + countryCode + ".csv" == resource['name']:
            resource.download('csv')