def test_get_country_info_from_m49(self): assert Country.get_country_info_from_m49(4) == { '#country+alt+i_ar+name+v_unterm': 'أفغانستان', '#country+alt+i_en+name+v_unterm': 'Afghanistan', '#country+alt+i_es+name+v_unterm': 'Afganistán (el)', '#country+alt+i_fr+name+v_unterm': "Afghanistan (l') [masc.]", '#country+alt+i_ru+name+v_unterm': 'Афганистан', '#country+alt+i_zh+name+v_unterm': '阿富汗', '#country+alt+name+v_fts': '', '#country+alt+name+v_hrinfo_country': '', '#country+alt+name+v_iso': '', '#country+alt+name+v_m49': '', '#country+alt+name+v_reliefweb': '', '#country+alt+name+v_unterm': '', '#country+code+num+v_m49': '4', '#country+code+v_fts': '1', '#country+code+v_hrinfo_country': '181', '#country+code+v_iso2': 'AF', '#country+code+v_iso3': 'AFG', '#country+code+v_reliefweb': '13', '#country+name+preferred': 'Afghanistan', '#country+name+short+v_reliefweb': '', '#country+regex': 'afghan', '#geo+admin_level': '0', '#geo+lat': '33.83147477', '#geo+lon': '66.02621828', '#meta+id': '1', '#region+code+intermediate': '', '#region+code+main': '142', '#region+code+sub': '34', '#region+intermediate+name+preferred': '', '#region+main+name+preferred': 'Asia', '#region+name+preferred+sub': 'Southern Asia'} assert Country.get_country_info_from_m49(882) == { '#country+alt+i_ar+name+v_unterm': 'ساموا', '#country+alt+i_en+name+v_unterm': 'Samoa', '#country+alt+i_es+name+v_unterm': 'Samoa', '#country+alt+i_fr+name+v_unterm': 'Samoa (le)', '#country+alt+i_ru+name+v_unterm': 'Самоа', '#country+alt+i_zh+name+v_unterm': '萨摩亚', '#country+alt+name+v_fts': '', '#country+alt+name+v_hrinfo_country': '', '#country+alt+name+v_iso': '', '#country+alt+name+v_m49': '', '#country+alt+name+v_reliefweb': '', '#country+alt+name+v_unterm': '', '#country+code+num+v_m49': '882', '#country+code+v_fts': '193', '#country+code+v_hrinfo_country': '373', '#country+code+v_iso2': 'WS', '#country+code+v_iso3': 'WSM', '#country+code+v_reliefweb': '204', '#country+name+preferred': 'Samoa', '#country+name+short+v_reliefweb': '', '#country+regex': '^(?!.*amer.*)samoa|(\\bindep.*samoa)', '#geo+admin_level': '0', '#geo+lat': '-13.16992041', '#geo+lon': '-173.5139768', '#meta+id': '192', '#region+code+intermediate': '', '#region+code+main': '9', '#region+code+sub': '61', '#region+intermediate+name+preferred': '', '#region+main+name+preferred': 'Oceania', '#region+name+preferred+sub': 'Polynesia'} assert Country.get_country_info_from_m49(275) == { '#meta+id': '170', '#country+code+v_hrinfo_country': '351', '#country+code+v_reliefweb': '180', '#country+code+num+v_m49': '275', '#country+code+v_fts': '171', '#country+code+v_iso2': 'PS', '#country+code+v_iso3': 'PSE', '#country+name+preferred': 'State of Palestine', '#country+alt+name+v_m49': '', '#country+alt+name+v_iso': 'Palestine, State of', '#country+alt+name+v_unterm': '', '#country+alt+name+v_fts': 'occupied Palestinian territory', '#country+alt+name+v_hrinfo_country': 'occupied Palestinian territory', '#country+name+short+v_reliefweb': 'oPt', '#country+alt+name+v_reliefweb': 'occupied Palestinian territory', '#country+alt+i_en+name+v_unterm': 'Palestine', '#country+alt+i_fr+name+v_unterm': 'État de Palestine', '#country+alt+i_es+name+v_unterm': 'Estado de Palestina', '#country+alt+i_ru+name+v_unterm': 'Государство Палестина', '#country+alt+i_zh+name+v_unterm': '巴勒斯坦国', '#country+alt+i_ar+name+v_unterm': 'دولة فلسطين', '#geo+admin_level': '0', '#geo+lat': '31.99084142', '#geo+lon': '35.30744047', '#region+code+main': '142', '#region+main+name+preferred': 'Asia', '#region+code+sub': '145', '#region+name+preferred+sub': 'Western Asia', '#region+code+intermediate': '', '#region+intermediate+name+preferred': '', '#country+regex': 'palestin|\\bgaza|west.?bank', '#country+name+override': 'oPt'} assert Country.get_country_info_from_m49(9999) is None with pytest.raises(LocationError): Country.get_country_info_from_m49(9999, exception=LocationError)
def test_get_country_info_from_m49(self): assert Country.get_country_info_from_m49(4, use_live=False) == { '#country+alt+i_ar+name+v_unterm': 'أفغانستان', '#country+alt+i_en+name+v_unterm': 'Afghanistan', '#country+alt+i_es+name+v_unterm': 'Afganistán (el)', '#country+alt+i_fr+name+v_unterm': "Afghanistan (l') [masc.]", '#country+alt+i_ru+name+v_unterm': 'Афганистан', '#country+alt+i_zh+name+v_unterm': '阿富汗', '#country+alt+name+v_fts': '', '#country+alt+name+v_hrinfo_country': '', '#country+alt+name+v_iso': '', '#country+alt+name+v_m49': '', '#country+alt+name+v_reliefweb': '', '#country+alt+name+v_unterm': '', '#country+code+num+v_m49': '4', '#country+code+v_fts': '1', '#country+code+v_hrinfo_country': '181', '#country+code+v_iso2': 'AF', '#country+code+v_iso3': 'AFG', '#country+code+v_reliefweb': '13', '#country+name+preferred': 'Afghanistan', '#country+name+short+v_reliefweb': '', '#country+regex': 'afghan', '#geo+admin_level': '0', '#geo+lat': '33.83147477', '#geo+lon': '66.02621828', '#meta+id': '1', '#region+code+intermediate': '', '#region+code+main': '142', '#region+code+sub': '34', '#region+intermediate+name+preferred': '', '#region+main+name+preferred': 'Asia', '#region+name+preferred+sub': 'Southern Asia' } assert Country.get_country_info_from_m49(882, use_live=False) == { '#country+alt+i_ar+name+v_unterm': 'ساموا', '#country+alt+i_en+name+v_unterm': 'Samoa', '#country+alt+i_es+name+v_unterm': 'Samoa', '#country+alt+i_fr+name+v_unterm': 'Samoa (le)', '#country+alt+i_ru+name+v_unterm': 'Самоа', '#country+alt+i_zh+name+v_unterm': '萨摩亚', '#country+alt+name+v_fts': '', '#country+alt+name+v_hrinfo_country': '', '#country+alt+name+v_iso': '', '#country+alt+name+v_m49': '', '#country+alt+name+v_reliefweb': '', '#country+alt+name+v_unterm': '', '#country+code+num+v_m49': '882', '#country+code+v_fts': '193', '#country+code+v_hrinfo_country': '373', '#country+code+v_iso2': 'WS', '#country+code+v_iso3': 'WSM', '#country+code+v_reliefweb': '204', '#country+name+preferred': 'Samoa', '#country+name+short+v_reliefweb': '', '#country+regex': '^(?!.*amer.*)samoa|(\\bindep.*samoa)', '#geo+admin_level': '0', '#geo+lat': '-13.16992041', '#geo+lon': '-173.5139768', '#meta+id': '192', '#region+code+intermediate': '', '#region+code+main': '9', '#region+code+sub': '61', '#region+intermediate+name+preferred': '', '#region+main+name+preferred': 'Oceania', '#region+name+preferred+sub': 'Polynesia' } assert Country.get_country_info_from_m49(9999, use_live=False) is None with pytest.raises(LocationError): Country.get_country_info_from_m49(9999, use_live=False, exception=LocationError)
df = pd.read_csv("prepared_data/trimmed_75_2020.csv") print(df) regions = [] continents = [] latitudes = [] longitudes = [] for index, row in df.iterrows(): #print(row['Country or Area Code'], row['Country or Area']) region = "null" continent = "null" latitude = "null" longitude = "null" try: datadict = Country.get_country_info_from_m49( row['Country or Area Code']) region = datadict['#region+name+preferred+sub'] continent = datadict['#region+main+name+preferred'] latitude = datadict['#geo+lat'] longitude = datadict['#geo+lon'] except: region = (row['Country or Area'] + " is not a Country!") print("error at " + row['Country or Area']) regions.append(region) continents.append(continent) latitudes.append(latitude) longitudes.append(longitude) df["region"] = regions df["continent"] = continents df["latitude"] = latitudes