def country_info(country_name): d = [] country = CountryInfo(country_name) d.append(["name", country.name().capitalize()]) d.append(["capital", country.capital().capitalize()]) d.append(["region", country.region().capitalize()]) d.append(["currency", country.currencies()]) d.append(["area", country.area()]) d.append(["population", country.population()]) d.append(["languages", country.languages()]) d.append(["borders", country.borders()]) d.append(["calling code", country.calling_codes()]) d.append(["lat/long", country.capital_latlng()]) d.append(["code", country.iso(2)]) return d
currencies_pool = rand.sample(currencies_pool, options[category_index] - 1) currencies_pool.append(country.currencies()[0]) rand.shuffle(currencies_pool) index = currencies_pool.index(country.currencies()[0]) else: index = country_pool.index(countries_question[i]) answer = choices[index] for x in range(options[category_index]): country_choice = CountryInfo(country_pool[x]) if category == "area": print('(' + choices[x] + ') ' + str(country_choice.area()) + " sq kilometers") elif category == "subregion": print('(' + choices[x] + ') ' + subregion_pool[x]) elif category == "calling code": print('(' + choices[x] + ') ' + str(country_choice.calling_codes()[0])) elif category == "population": print('(' + choices[x] + ') ' + str(country_choice.population())) elif category == "capital": print('(' + choices[x] + ') ' + country_choice.capital())
def get_area_by_country(country): country = CountryInfo(country) return country.area()
data1 = country.alt_spellings() print(data1) data2 = country.capital() print(data2) data3 = country.currencies() print(data3) data4 = country.languages() print(data4) data5 = country.timezones() print(data5) data6 = country.area() print(data6) data7 = country.borders() print(data7) data8 = country.calling_codes() print(data8) data9 = country.wiki() print(data9) data10 = country.info() for x, y in data10.items(): print(f'{x} --> {y}')
def getMobilityPlots(locations, data_type, shift_days, smoothing_range, mobility_type, pop_type, location_num): timeline = 'Days since X number of deaths/cases' x_num = 10 if len(locations) <= location_num: return None else: mob_loc_df = getGoogleMobilityReportsFromCountry( locations[location_num]) #print (mob_reports_df.head(10)) #mob_loc_df = mob_reports_df[mob_reports_df['sub_region_1'].isnull()] if data_type == 'Deaths': df = getNewDeaths() df_total = getTotalDeaths() elif data_type == 'Cases': df = getNewCases() df_total = getTotalCases() else: return None data = [] if not x_num: x_num = 0 location = locations[location_num] location_key = location.lower().replace(' ', '_') if pop_type == 'Per Million': country = CountryInfo(location) population = country.population() area = country.area() pop_density = float(population) / area print(population) print(area) print(pop_density) df[location_key] = 1000000.0 * df[location_key] / population else: population = 1.0 if timeline == 'Days since X number of deaths/cases': df_total_loc = df_total.fillna(0) df_total_loc['date'] = df_total_loc['date'] - datetime.timedelta( days=shift_days) df_total_loc = df_total_loc.drop( df_total_loc[(df_total_loc[location_key] < x_num)].index) df_total_loc = df_total_loc.reset_index() df_total_loc = df_total_loc.drop([location_key], axis=1) df[location_key] = df[location_key].fillna(0).rolling( smoothing_range).mean() df_loc = df[['date', location_key]].copy() df_loc['date'] = df_loc['date'] - datetime.timedelta( days=shift_days) df_loc = pd.merge(df_total_loc, df_loc, how='left', on='date') #mob_loc_df = mob_reports_df[mob_reports_df['country_region']==location] print(mob_loc_df.info()) #print (mob_loc_df['sub_region_1']) #mob_loc_df = mob_loc_df[mob_loc_df['sub_region_1'].isnull()] mob_loc_df['date'] = pd.to_datetime(mob_loc_df.date) mob_loc_df.sort_values(by=['date']) mob_loc_df[mobility_type] = mob_loc_df[mobility_type].fillna( 0).rolling(smoothing_range).mean() country = pycountry.countries.get(name=location).alpha_3 stringency_df = getStringencyDataFrame(country) stringency_df['date'] = pd.to_datetime(stringency_df.date) stringency_df.sort_values(by=['date']) df_loc = pd.merge(df_loc, mob_loc_df, how='left', on='date') df_loc = pd.merge(df_loc, stringency_df, how='left', on='date') if mobility_type != 'residential_percent_change_from_baseline': df_loc[mobility_type] = -df_loc[mobility_type] mobility_name = '% Time less at ' + mobility_dict[mobility_type] else: mobility_name = '% Time more at ' + mobility_dict[mobility_type] #print (df_loc[mobility_type]) data.append( go.Bar( x=df_loc.index, y=df_loc[location_key], marker=dict(color=colour_palette[location_num], ), opacity=0.7, text=location, #name=location + ' ' + data_type, yaxis='y2', showlegend=False)) data.append( go.Scatter(x=df_loc.index, y=df_loc['stringency'], mode='lines', marker=dict(color='rgb(0,0,0)', ), line=dict(color='rgb(0,0,0)', dash='dot'), opacity=1.0, text=location, name='Stringency Index', yaxis='y1', showlegend=True)) data.append( go.Scatter(x=df_loc.index, y=df_loc[mobility_type], mode='lines', marker=dict(color='rgb(0,0,0)', ), line=dict(color='rgb(0,0,0)', dash='solid'), opacity=1.0, text=location, name=mobility_name, yaxis='y1', showlegend=True)) else: return None if mobility_type == 'residential_percent_change_from_baseline': y_range = [0, 100] else: y_range = [0, 100] if pop_type == "Per Million": title_sub = ' (per million)' else: title_sub = '' figure = { 'data': data, 'layout': go.Layout( title={ 'text': location, 'y': 1.0, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, showlegend=True, #show_legend, legend=dict( orientation="h", x=0, y=1.1, font=dict(family="Arial", size=14, color="#000000"), ), font=dict(family='Arial', size=12, color='#000000'), hovermode='closest', margin=dict(t=50), xaxis=dict(tickfont=dict(family='Arial', size=14, color='#000000'), ), yaxis2=dict( #range=[0,1200], rangemode="tozero", tickfont=dict(family='Arial', size=14, color='#000000'), side='left', title=data_type + title_sub, showgrid=False), yaxis=dict( #autorange='reversed', range=y_range, rangemode='tozero', anchor='x', overlaying='y', tickfont=dict(family='Arial', size=14, color='#000000'), side='right', title='Mobility/Stringency', showgrid=False), height=400, autosize=True, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)') } return dcc.Graph(figure=figure, config={'displayModeBar': False}, id='reg-str-graph')
# coding=utf-8 from countryinfo import CountryInfo country = CountryInfo('Austria') print (country.population()) print (country.area())
def updateTotalDeathsTimeline(locations, timeline, data_type, x_num, smoothing_range, x_days, pop_type): if locations: if data_type == 'Deaths': df = getTotalDeaths() elif data_type == 'Cases': df = getTotalCases() else: return None df['sum_data'] = df.apply(lambda x: sumLocations(x, locations), axis=1) df = df.drop(df[(df['sum_data'] == 0)].index) data = [] count = 0 for location in locations: location_key = location.lower().replace(' ', '_') if pop_type == '% of Population': country = CountryInfo(location) population = country.population() area = country.area() pop_density = float(population) / area print(population) print(area) print(pop_density) df[location_key] = 100.0 * df[location_key] / population else: population = 1.0 if not x_num: x_num = 0 if timeline == 'Days since X number of deaths/cases': df_location = df.fillna(0) df_location = df_location.drop(df_location[( df_location[location_key] < x_num / population)].index) df_location = df_location.reset_index() data.append( go.Scatter(x=df_location.index, y=df_location[location_key], mode='lines', marker=dict(color=colour_palette[count], ), opacity=1.0, text=location, name=location)) prediction_df = df_location[['date', location_key]] prediction_df[ 'rolling_mean'] = 1.0 + df_location[location_key].fillna( 0).pct_change().rolling(smoothing_range).mean() prediction_df = prediction_df.iloc[[-1]] r_index = prediction_df.index.values[0] r_mean = prediction_df['rolling_mean'].values[0] r_num = prediction_df[location_key].values[0] for x in range(1, x_days): extra_df = pd.DataFrame( [[float(r_num) * r_mean**x, r_mean]], columns=[location_key, "rolling_mean"]) prediction_df = pd.concat([prediction_df, extra_df], ignore_index=True, axis=0) prediction_df.index = pd.RangeIndex(start=r_index, stop=r_index + x_days, step=1) data.append( go.Scatter(x=prediction_df.index, y=prediction_df[location_key], mode='lines', marker=dict(color=colour_palette[count], ), line=dict(color=colour_palette[count], dash='dot'), opacity=1.0, text=location, name=location, showlegend=False)) else: data.append( go.Scatter(x=df['date'], y=df[location_key].fillna(0), mode='lines', marker=dict(color=colour_palette[count], ), opacity=1.0, text=location, name=location)) prediction_df = df[['date', location_key]] prediction_df['rolling_mean'] = 1.0 + df[location_key].fillna( 0).pct_change().rolling(smoothing_range).mean() prediction_df = prediction_df.iloc[[-1]] r_date = prediction_df['date'].values[0] r_mean = prediction_df['rolling_mean'].values[0] r_num = prediction_df[location_key].values[0] for x in range(1, x_days): r_dt = convert_to_datetime( r_date ) #datetime.datetime.strptime(r_date, '%Y-%m-%d') extra_df = pd.DataFrame( [[ r_dt + datetime.timedelta(days=x), float(r_num) * r_mean**x, r_mean ]], columns=["date", location_key, "rolling_mean"]) prediction_df = pd.concat([prediction_df, extra_df], ignore_index=True, axis=0) data.append( go.Scatter(x=prediction_df['date'], y=prediction_df[location_key], mode='lines', marker=dict(color=colour_palette[count], ), line=dict(color=colour_palette[count], dash='dot'), opacity=1.0, text=location, name=location, showlegend=False)) count += 1 if pop_type == "% of Population": title_sub = ' (% of population)' else: title_sub = '' figure = { 'data': data, 'layout': go.Layout( title='Cumulative ' + data_type + title_sub, legend=dict(orientation="h", x=0, y=1.1), font=dict(family='Arial', size=15, color='#000000'), hovermode='closest', margin=dict(t=50), xaxis=dict(tickfont=dict(family='Arial', size=14, color='#000000'), ), yaxis=dict( #range=y_axis_range, tickfont=dict(family='Arial', size=14, color='#000000'), ), height=400, autosize=True, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)') } return dcc.Graph(figure=figure, config={'displayModeBar': False}, id='total-deaths-t-graph')
country_currency = country.currencies() print('\nCurrency:') for currency in country_currency: print(currency, end=', ') country_lang = country.languages() print('\n\nLanguages:') for languages in country_lang: print(languages, end=', ') country_timezone = country.timezones() print('\n\nTime-Zone:') for timezones in country_timezone: print(timezones, end=', ') country_area = country.area() print(f'\n\nCountry Area:\n{country_area}') country_borders = country.borders() print('\nBorders:') for border in country_borders: print(border, end=', ') calling_codes = country.calling_codes() print('\n\nCall Code:') for call_code in calling_codes: print(call_code) country_region = country.region() print(f'\nRegion:\n{country_region}')