Example #1
0
    def add_country_location(self, country):
        # type: (str) -> None
        """Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
        converted to an iso 3 code. If the country is already added, it is ignored.

        Args:
            country (str): Country to add

        Returns:
            None
        """
        iso3, match = Location.get_iso3_country_code(country)
        if iso3 is None:
            raise HDXError('Country: %s - cannot find iso3 code!' % country)
        hdx_code, match = Location.get_HDX_code_from_location(
            iso3, self.configuration)
        if hdx_code is None:
            raise HDXError(
                'Country: %s with iso3: %s could not be found in HDX list!' %
                (country, iso3))
        groups = self.data.get('groups', None)
        if groups:
            if hdx_code in [x['name'] for x in groups]:
                return
        else:
            groups = list()
        groups.append({'name': hdx_code})
        self.data['groups'] = groups
Example #2
0
 def test_get_iso3_country_code(self):
     assert Location.get_iso3_country_code('jpn') == ('jpn', True)
     assert Location.get_iso3_country_code('ZWE') == ('zwe', True)
     assert Location.get_iso3_country_code('Vut') == ('vut', True)
     assert Location.get_iso3_country_code('abc') == (None, False)
     assert Location.get_iso3_country_code('United Kingdom') == ('gbr', True)
     assert Location.get_iso3_country_code('united states') == ('usa', True)
     assert Location.get_iso3_country_code('UZBEKISTAN') == ('uzb', True)
     assert Location.get_iso3_country_code('Sierra') == ('sle', False)
     assert Location.get_iso3_country_code('Venezuela (Bolivarian Republic of)') == ('ven', False)
Example #3
0
    def add_continent_location(self, continent: str) -> None:
        """Add a continent. If a 2 letter continent code is not provided, tries to parse value and convert to 2 letter code.

        Args:
            continent (str): Continent to add

        Returns:
            None
        """
        self.add_country_locations(
            Location.get_countries_in_continent(continent))
Example #4
0
    def get_location(self) -> List[str]:
        """Return the dataset's location

        Returns:
            List[str]: List of locations or [] if there are none
        """
        countries = self.data.get('groups', None)
        if not countries:
            return list()
        return [
            Location.get_country_name_from_iso3(x['name']) for x in countries
        ]
Example #5
0
    def add_continent_location(self, continent: str) -> None:
        """Add all countries in a  continent. If a 2 letter continent code is not provided, value is parsed and if it
        is a valid continent name, converted to a 2 letter code. If any country is already added, it is ignored.

        Args:
            continent (str): Continent to add

        Returns:
            None
        """
        self.add_country_locations(
            Location.get_countries_in_continent(continent))
Example #6
0
 def test_get_countries_in_continent(self):
     assert len(Location.get_countries_in_continent('AF')) == 58
     assert len(Location.get_countries_in_continent('eu')) == 54
     assert len(Location.get_countries_in_continent('As')) == 52
     assert len(Location.get_countries_in_continent('ab')) == 0
     assert len(Location.get_countries_in_continent('North America')) == 42
     assert len(Location.get_countries_in_continent('North America')) == 42
Example #7
0
    def get_location(self):
        # type: () -> List[str]
        """Return the dataset's location

        Returns:
            List[str]: List of locations or [] if there are none
        """
        countries = self.data.get('groups', None)
        if not countries:
            return list()
        return [
            Location.get_location_from_HDX_code(x['name'], self.configuration)
            for x in countries
        ]
Example #8
0
    def add_continent_location(self, continent, locations=None):
        # type: (str, Optional[List[str]]) -> bool
        """Add all countries in a  continent. If a 2 letter continent code is not provided, value is parsed and if it
        is a valid continent name, converted to a 2 letter code. If any country is already added, it is ignored.

        Args:
            continent (str): Continent to add
            locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.

        Returns:
            bool: Returns True if all countries in continent added or False if any already present.
        """
        return self.add_country_locations(
            Location.get_countries_in_continent(continent),
            locations=locations)
Example #9
0
    def add_country_location(self, country: str) -> None:
        """Add a country. If iso 3 code not provided, tries to parse value and convert to iso 3 code.

        Args:
            country (str): Country to add

        Returns:
            None
        """
        iso3, match = Location.get_iso3_country_code(country)
        if iso3 is None:
            raise HDXError('Country: %s could not be found!')
        countries = self.data.get('groups', None)
        if countries:
            if country in [x['name'] for x in countries]:
                return
        else:
            countries = list()
        countries.append({'name': iso3})
        self.data['groups'] = countries
Example #10
0
    def add_country_location(self, country: str) -> None:
        """Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
        converted to an iso 3 code. If the country is already added, it is ignored.

        Args:
            country (str): Country to add

        Returns:
            None
        """
        iso3, match = Location.get_iso3_country_code(country)
        if iso3 is None:
            raise HDXError('Country: %s could not be found!')
        countries = self.data.get('groups', None)
        if countries:
            if country in [x['name'] for x in countries]:
                return
        else:
            countries = list()
        countries.append({'name': iso3})
        self.data['groups'] = countries
Example #11
0
    def add_country_location(self, country, exact=True, locations=None):
        # type: (str, Optional[bool],Optional[List[str]]) -> bool
        """Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
        converted to an iso 3 code. If the country is already added, it is ignored.

        Args:
            country (str): Country to add
            exact (Optional[bool]): True for exact matching or False to allow fuzzy matching. Defaults to True.
            locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.

        Returns:
            bool: True if country added or False if country already present
        """
        iso3, match = Location.get_iso3_country_code(country)
        if iso3 is None:
            raise HDXError('Country: %s - cannot find iso3 code!' % country)
        return self.add_other_location(
            iso3,
            exact=exact,
            alterror='Country: %s with iso3: %s could not be found in HDX list!'
            % (country, iso3),
            locations=locations)
Example #12
0
    def add_other_location(self, location):
        # type: (str) -> None
        """Add a location which is not a country or continent. Value is parsed and compared to existing locations in 
        HDX. If the location is already added, it is ignored.

        Args:
            location (str): Location to add

        Returns:
            None
        """
        hdx_code, match = Location.get_HDX_code_from_location(
            location, self.configuration)
        if hdx_code is None:
            raise HDXError('Location: %s - cannot find in HDX!' % location)
        groups = self.data.get('groups', None)
        if groups:
            if hdx_code in [x['name'] for x in groups]:
                return
        else:
            groups = list()
        groups.append({'name': hdx_code})
        self.data['groups'] = groups
Example #13
0
 def test_get_country_name_from_iso3(self):
     assert Location.get_country_name_from_iso3('jpn') == 'Japan'
     assert Location.get_country_name_from_iso3('awe') is None
     assert Location.get_country_name_from_iso3('Pol') == 'Poland'
     assert Location.get_country_name_from_iso3('SGP') == 'Singapore'
     assert Location.get_country_name_from_iso3('uy') is None