def test_country_alpha3_to_country_alpha2(self): cn_a2_code = country_alpha3_to_country_alpha2('USA') assert (cn_a2_code) assert (cn_a2_code == 'US') cn_a2_code = country_alpha3_to_country_alpha2('RUS') assert (cn_a2_code) assert (cn_a2_code == 'RU')
def gather(self, data_name): """Importing and cleaning OECD. returns istat as pandas.dataframe""" # Import, Rename, adjust values in million oecd = pd.read_csv(f'data/OECD_{data_name}.csv') oecd.rename(columns={ 'LOCATION': 'Country', 'TIME': 'Year', 'Value': data_name }, inplace=True) oecd['Country'] = oecd['Country'].apply(lambda x: x.split(' ')[ 0] if re.match(r'[France|Germany].*', x) else x) oecd[data_name] = oecd[data_name].apply( lambda value: float(value * 1000000)) # Kick out every country with the wrong code len (they're not in europe anyways) & not in europe oecd = oecd[oecd['Country'].apply(lambda x: len(x) == 3)] oecd = oecd[oecd['Country'].apply( lambda x: pc.country_alpha2_to_continent_code( country_2_code=pc.country_alpha3_to_country_alpha2(x )) == 'EU')] # Turn country codes to full country names oecd['Country'] = oecd['Country'].apply( lambda x: pc.map_country_alpha3_to_country_name()[x]) oecd.set_index(['Country', 'Year'], inplace=True) oecd.sort_index(inplace=True) oecd = pd.DataFrame(oecd[data_name]) self.data.append(oecd)
def alpha3_to_continent(iso_alpha3): return { 'EU': 'Europa', 'NA': 'North America', 'SA': 'South America', 'AS': 'Asia', 'OC': 'Oceania', 'AF': 'Africa', }[country_alpha2_to_continent_code( country_alpha3_to_country_alpha2(iso_alpha3))]
def main(): cn_name_format = COUNTRY_NAME_FORMAT_UPPER for key, value in sorted(map_countries(cn_name_format).items()): pprint({key: value}) try: cn_continent = country_alpha2_to_continent_code('AA') pprint(cn_continent) except KeyError as kerr: print("Key error: {0}".format(kerr)) except: print("Unexpected error:", sys.exc_info()[0]) try: cn_continent = country_alpha2_to_continent_code(None) pprint(cn_continent) except KeyError as kerr: print("Key error: {0}".format(kerr)) except: print("Unexpected error:", sys.exc_info()[0]) cn_continent = country_alpha2_to_continent_code('US') pprint(cn_continent) cn_continent = country_alpha2_to_continent_code('AU') pprint(cn_continent) cn_continent = country_alpha2_to_continent_code('NZ') pprint(cn_continent) cn_continent = country_alpha2_to_continent_code('JP') pprint(cn_continent) cn_name = country_alpha2_to_country_name('JP', cn_name_format) pprint(cn_name) cn_a2_code = country_name_to_country_alpha2('USA', cn_name_format) pprint(cn_a2_code) cn_a2_code = country_name_to_country_alpha2('United States', cn_name_format) pprint(cn_a2_code) cn_a2_code = country_name_to_country_alpha2('South Korea', cn_name_format) pprint(cn_a2_code) cn_name = country_alpha2_to_country_name(cn_a2_code, cn_name_format) pprint(cn_name) cn_name = country_alpha2_to_country_name('RU', cn_name_format) pprint(cn_name) cn_a2_code = country_name_to_country_alpha2(cn_name, cn_name_format) pprint(cn_a2_code) cn_a2_code = country_alpha3_to_country_alpha2('RUS') pprint(cn_a2_code) cn_name = country_alpha2_to_country_name('GB', cn_name_format) pprint(cn_name) cn_name = country_alpha2_to_country_name('US', cn_name_format) pprint(cn_name)
def createCitiesPickleFile(): worldCitiesfile = pd.read_excel('../Docs/worldcities.xlsx', encoding='utf-8') worldCitiesDict = {} worldIso2Dict = {} worldIso3Dict = {} worldCountriesDict = {} index = 0 cpt = 0 continents = { 'NA': 'North America', 'SA': 'South America', 'AS': 'Asia', 'OC': 'Australia', 'AF': 'Africa', 'EU': 'Europe' } unsupportedCountries = [] for city in worldCitiesfile['city']: try: iso2CountryCode = pycountry_convert.country_alpha3_to_country_alpha2( worldCitiesfile['iso3'][index]) continentCode = pycountry_convert.country_alpha2_to_continent_code( iso2CountryCode) country = worldCitiesfile['country'][index].lower() worldCountriesDict[country] = continents[continentCode] worldCitiesDict[city.lower()] = country worldIso2Dict[country] = iso2CountryCode worldIso3Dict[country] = worldCitiesfile['iso3'][index] worldIso2Dict[iso2CountryCode.lower( )] = country # a problem when i try to transform the iso2 code to lower case worldIso3Dict[worldCitiesfile['iso3'][index].lower()] = country except: unsupportedCountries.append(worldCitiesfile['iso3'][index].lower()) cpt += 1 pass index += 1 print(cpt) unsupportedCountries = set(unsupportedCountries) print("number of unsupported countries", unsupportedCountries) print("number of countries", worldCountriesDict.__len__()) print("number of iso2", worldIso2Dict.__len__()) print("number of iso3", worldIso3Dict.__len__()) print("number of cities", worldCitiesDict.__len__()) import pickle with open('../Docs/locations.pickle', 'wb') as file: pickle.dump(worldCitiesDict, file) pickle.dump(worldIso2Dict, file) pickle.dump(worldIso3Dict, file) pickle.dump(worldCountriesDict, file) print("succes !")
def country_code(country_code): if not country_code: return "" country = country_code # If it's no 3 letter country code it is just passed if len(country_code) == 3: print("3") country = country_alpha3_to_country_alpha2( country_alpha2_to_country_name(country_code)) elif len(country_code) == 2: # In case it is just a 2 letter code. print("2") country = country_alpha2_to_country_name(country_code) return country
def update_map(picked_country, map_figure): if picked_country: # highlight flows from that country only country_name = country_df.iloc[picked_country]['country'] filtered_country_flow = flow_df[flow_df['country_from'] == country_name] map_trace = make_edge_trace(filtered_country_flow) map_trace.append(make_point_trace(country_df)) map_figure['data'] = map_trace # highlight country and destinations country_to = filtered_country_flow['country_to'].tolist() index_list = country_df[country_df['country'].isin(country_to)].index.tolist() index_list.append(picked_country) map_figure['data'][-1]['selectedpoints'] = index_list # rotate map to center country_code_iso2 = pc.country_alpha3_to_country_alpha2(country_name) lat = centroid_df.loc[centroid_df['country']==country_code_iso2, 'latitude'].iloc[0] lon = centroid_df.loc[centroid_df['country']==country_code_iso2, 'longitude'].iloc[0] map_figure['layout']['geo']['projection']['rotation']['lat'] = lat map_figure['layout']['geo']['projection']['rotation']['lon'] = lon return map_figure
def update_info(picked_country): if not picked_country: return [dash_dangerously_set_inner_html.DangerouslySetInnerHTML("""<center><strong ><font color="white" font-family="Source Sans Pro" size=2 font-weight='bold'> Currently showing countries with more than 10 connections <br> Pick a country on the map or on the bar for to show all connections of that country. </font></strong></center>""")] else: country = country_df.iloc[picked_country]['country'] filtered_country_flow = flow_df[flow_df['country_from'] == country] n_country_to = len(filtered_country_flow) country_code_iso2 = pc.country_alpha3_to_country_alpha2(country) country_name = pc.country_alpha2_to_country_name(country_code_iso2) user_count = country_df.iloc[picked_country]['user_count'] line_1 = f"""{country_name} has {user_count} active user(s).""" html_raw_1 = f'''<center><strong ><font color="white" font-family="Source Sans Pro" size=2 font-weight="bold">{line_1}</font></strong></center>''' line_2 = f"""{country_name} has connections with {n_country_to} other countries""" html_raw_2 = f'''<center><strong ><font color="white" font-family="Source Sans Pro" size=2 font-weight="bold">{line_2}</font></strong></center>''' line_3 = """To clear selections, refresh the webpage. """ html_raw_3 = f'''<center><strong ><font color="gray" font-family="Source Sans Pro" size=1 font-weight="100">{line_3}</font></strong></center>''' # todo: make it pretty if time allows... return [dash_dangerously_set_inner_html.DangerouslySetInnerHTML(html_raw_1), dash_dangerously_set_inner_html.DangerouslySetInnerHTML(html_raw_2), dash_dangerously_set_inner_html.DangerouslySetInnerHTML(html_raw_3)]
def get_continent(country_code): ''' :param country_code: ISO 3166-1 alpha-3 country code of a country :return: the name of the continent to which the country belongs, 'Unknown' if the country code is not valid ''' continent_names = { 'NA': 'North America', 'SA': 'South America', 'AS': 'Asia', 'OC': 'Oceania', 'AF': 'Africa', 'AN': 'Antarctica', 'EU': 'Europe' } try: continent_code = pc.country_alpha2_to_continent_code( pc.country_alpha3_to_country_alpha2(country_code)) continent = continent_names[continent_code] except: continent = 'Unknown' return continent
"URU": "URY", "VAN": "VUT", "VIE": "VNM", "ZAM": "ZMB", "ZIM": "ZWE", } countrynames = [] continentcodes = [] with open(sys.argv[1]) as f: for line in f: line = line.strip() if line == "UNK": countrynames.append("Kosovo") continentcodes.append(continent_dico["EU"]) else: try: countrynames.append(country_alpha2_to_country_name( country_alpha3_to_country_alpha2(line))) except: line = alpha3_ico[line] countrynames.append(country_alpha2_to_country_name( country_alpha3_to_country_alpha2(line))) continentcodes.append(continent_dico[country_alpha2_to_continent_code( country_alpha3_to_country_alpha2(line))]) np.savetxt("continents.csv", continentcodes, delimiter=',', fmt="%s") np.savetxt("countrynames.csv", countrynames, delimiter=',', fmt="%s")
def medal_table(season, year): if (int(year) > 2018): return None print(year) print(getcwd()) url = "https://www.topendsports.com/events/" + season.lower( ) + "/medal-tally/" + str(year) + ".htm" filename = "responses/medal_tables/" + str(year) + "-" + season + ".html" if (not path.exists(filename)): get_response(url, filename) with open(filename, 'rb') as f: soup = BeautifulSoup(f.read(), 'html.parser') medals = soup.find('table', class_='list') medals['id'] = 'medalTable' for thtag in medals.find_all('th'): thtag['class'] = 'th-sm' rank = medals.find('th') if rank.text != "Ranking": rank.string = "Ranking" new_tag = soup.new_tag('thead') trow = medals.find('tr') trow.wrap(new_tag) for tr in medals.find_all('tr'): tr['class'] = 'inBody' tr_one = medals.find('tr') del tr_one['class'] for trtag in medals.find_all('tr'): index = 0 for tdtag in trtag.find_all('td'): if index == 0: if "=" in tdtag.text: tdtag.string = tdtag.text[1:] if index == 1: tdtag['class'] = 'sorting_1' if tdtag.text == 'Great Britain': tdtag.string = 'United Kingdom' if len(tdtag.text) == 3: if tdtag.text == "CRO": tdtag.string = "HRV" elif tdtag.text == "SUI": tdtag.string = "CHE" elif tdtag.text == "IRI": tdtag.string = "IRN" elif tdtag.text == "GRE": tdtag.string = "GRC" elif tdtag.text == "DEN": tdtag.string = "DNK" elif tdtag.text == "RSA": tdtag.string = "ZAF" elif tdtag.text == "SLO": tdtag.string = "SVN" elif tdtag.text == "INA": tdtag.string = "IDN" elif tdtag.text == "VIE": tdtag.string = "Vietnam" elif tdtag.text == "TPE": tdtag.string = "TWN" elif tdtag.text == "BAH": tdtag.string = "BHS" elif tdtag.text == "IOA": tdtag.string = "Individual Olympic Athletes" elif tdtag.text == "FIJ": tdtag.string = "FJI" elif tdtag.text == "KOS": tdtag.string = "Kosovo" elif tdtag.text == "PUR": tdtag.string = "PRI" elif tdtag.text == "SIN": tdtag.string = "SGP" elif tdtag.text == "MAS": tdtag.string = "MYS" elif tdtag.text == "ALG": tdtag.string = "DZA" elif tdtag.text == "BUL": tdtag.string = "BGR" elif tdtag.text == "MGL": tdtag.string = "MNG" elif tdtag.text == "GRN": tdtag.string = "GRD" elif tdtag.text == "NIG": tdtag.string = "NER" elif tdtag.text == "PHI": tdtag.string = "PHL" elif tdtag.text == "NGR": tdtag.string = "NGA" elif tdtag.text == "POR": tdtag.string = "PRT" elif tdtag.text == "UAE": tdtag.string = "ARE" if (tdtag.text != "Individual Olympic Athletes") & ( tdtag.text != "Kosovo") & (tdtag.text != "Vietnam"): two = concon.country_alpha3_to_country_alpha2( tdtag.text) tdtag.string = concon.country_alpha2_to_country_name( two) index = index + 1 print(medals.find_all('td', class_="sorting_1")) for countrytag in medals.find_all('td', class_="sorting_1"): c_link = soup.new_tag("a", href='/countries/' + countrytag.text) c_link["class"] = "btn-link" if len(countrytag.text) > 0: countrytag.string.wrap(c_link) # print(medals) print(type(medals)) print(type(str(medals))) medals = str(medals).replace("list", "table") return medals
def continent_from_iso_country_code(alpha3_country_code): alpha2_country_code = pc.country_alpha3_to_country_alpha2(alpha3_country_code) continent_code = pc.country_alpha2_to_continent_code(alpha2_country_code) return pc.convert_continent_code_to_continent_name(continent_code)