Exemplo n.º 1
0
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)
Exemplo n.º 2
0
    def test_country_name_to_country_alpha2(self):
        cn_a2_code = country_name_to_country_alpha2('USA')
        assert(cn_a2_code)
        assert(cn_a2_code == 'US')

        cn_a2_code = country_name_to_country_alpha2('United States')
        assert(cn_a2_code)
        assert(cn_a2_code == 'US')

        cn_a2_code = country_name_to_country_alpha2('South Korea')
        assert(cn_a2_code)
        assert(cn_a2_code == 'KR')
Exemplo n.º 3
0
 def country_code(name):
     try:
         code = pc.country_name_to_country_alpha2(name,
                                                  cn_name_format="default")
         return code
     except:
         return "N/A"
Exemplo n.º 4
0
 def addContinent(self, df):
     df['Continent'] = df.apply(
         lambda row: pc.convert_continent_code_to_continent_name(
             pc.country_alpha2_to_continent_code(
                 pc.country_name_to_country_alpha2(
                     row.country, cn_name_format="default"))),
         axis=1)
Exemplo n.º 5
0
def continents(elem):
    if elem == 'Mainland China':
        return 'China'
    if elem == 'US':
        return 'Nordamerika'
    if elem == 'UK':
        return 'Europa'
    if elem == 'Others':
        return 'Andere'
    try:
        country_code = pc.country_name_to_country_alpha2(
            elem, cn_name_format="default")
        continent_name = pc.country_alpha2_to_continent_code(country_code)
        if continent_name == "NA":
            return 'Nordamerika'
        if continent_name == 'EU':
            return "Europa"
        if continent_name == 'AS':
            return "Asien"
        if continent_name == 'AF':
            return "Afrika"
        if continent_name == 'OC':
            return "Ozeanien"
        return continent_name
    except:
        return elem
Exemplo n.º 6
0
def main():
  data = read_file('internet.csv')
  incomeperperson = pd.Series(get_variable(data, "incomeperperson"))
  internetuserate = pd.Series(get_variable(data, "internetuserate"))
  urbanrate = pd.Series(get_variable(data, "urbanrate"))

  incomeperperson_vs_internetuserate = internetuserate.corr(incomeperperson)
  urbanrate_vs_internetuserate = internetuserate.corr(urbanrate)
  urbanrate_vs_incomeperperson = incomeperperson.corr(urbanrate)

  print(incomeperperson_vs_internetuserate)
  print(urbanrate_vs_internetuserate)
  print(urbanrate_vs_incomeperperson)

  continents = {
    'NA': 'North America',
    'SA': 'South America', 
    'AS': 'Asia',
    'OC': 'Australia',
    'AF': 'Africa',
    'EU': 'Europe'
  }
  for point in data:
    country_code = pc.country_name_to_country_alpha2(point['country'], cn_name_format="default")
    continent_code = pc.country_alpha2_to_continent_code(country_code)
    point['continent'] = continents[continent_code]
  write_file(data)
Exemplo n.º 7
0
def get_continent(country):
    """
    DESCRIPTION: return continent code given country name.
        
    INPUT: country - (str) country name
        
    OUTPUT: continent_code - (str) continent code for country
    """

    try:
        #get country code
        country_code = pc.country_name_to_country_alpha2(
            country, cn_name_format="default")
        # get continent code
        continent_code = pc.country_alpha2_to_continent_code(country_code)

    except:

        # given that there are only two countries that cause an exception
        # it's easy to deal with them manually

        if country == 'The Bahamas':
            return 'NA'

        elif country == 'Myanmar (Burma)':
            return 'AS'

        # Redundant case
        else:
            print('Something Went Wrong')
            return -1

    return continent_code
Exemplo n.º 8
0
def get_country_continent_from_address(address_dict):
    """
	
	Takes address dictionary (from 'get_address_by_location'), returns country

	"""

    # dictionary of continent names:
    continent_name_dict = {
        "AF": "Africa",
        "AS": "Asia",
        "NA": "North America",
        "SA": "South America",
        "OC": "Oceania",
        "EU": "Europe"
    }

    # get info from address dict
    address = address_dict["address"]
    country = address["country"]

    # get continent
    country_code = pc.country_name_to_country_alpha2(country,
                                                     cn_name_format="default")

    continent_abbreviation = pc.country_alpha2_to_continent_code(country_code)
    continent_name = continent_name_dict[continent_abbreviation]

    return [country, continent_name]
Exemplo n.º 9
0
def execute_dtw(df_period_group, compare_group):
    dtw_results = []
    for country in df_period_group:
        try:
            country_code = pc.country_name_to_country_alpha2(
                country[0], cn_name_format="default")
        except Exception as e:
            print(country[0], "has error", e)
            continue
        try:
            continent_name = pc.country_alpha2_to_continent_code(country_code)
        except Exception as e:
            print(country[0], "has error", e)
            continue

        try:
            result = dtw.dtw(compare_group["StringencyIndex"],
                             country[1]["StringencyIndex"])
        except Exception as e:
            print(country[0], "has error", e)
            continue

        dtw_results.append([
            country[0], continent_name, result.distance,
            result.normalizedDistance
        ])
    dtw_df = pd.DataFrame(
        data=dtw_results,
        columns=["Country", "Continent", "Distance", "Normalized_distance"],
    )
    return dtw_df
Exemplo n.º 10
0
    def geolocator_process(loc_string):
        """
    user location will be determent by the geo-locator library. it receives the users location as written in the web
    converts it to  latitude and longitude then it will be called again to convert the latitude and longitude to
    a global unique name of country and continent.
        """
        country, continent = None, None  # initiate the returned variables
        loc = geolocator.geocode(loc_string)
        if loc:
            lat, lon = loc.latitude, loc.longitude
            time.sleep(config.SLEEP_TIME_FOR_LOCATIONS_API)
            try:
                new_loc = geolocator.reverse([lat, lon], language='en')
                country = new_loc.raw["address"]["country"]
                continent = config.continents_dict[
                    country_alpha2_to_continent_code(
                        country_name_to_country_alpha2(country))]

            except TypeError:
                logger.warning(
                    config.USER_PROBLEMATIC_COUNTRY.format(loc_string))

            except KeyError:
                if country in config.KNOWN_COUNTRIES:
                    country, continent = config.KNOWN_COUNTRIES[country]
            finally:
                time.sleep(config.SLEEP_TIME_FOR_LOCATIONS_API)
        return country, continent
Exemplo n.º 11
0
def country_to_continent(country_name):
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(
        country_continent_code)
    return country_continent_name
Exemplo n.º 12
0
def get_continent(country):
    try:
        country_code = pc.country_name_to_country_alpha2(
            country, cn_name_format='default')
        return pc.country_alpha2_to_continent_code(country_code)
    except (KeyError, TypeError):
        return country
 def get_continents(self, country_name):
     continents_table = {
         "AS": "Asia",
         "EU": "Europe",
         "NA": "North-America",
         "SA": "South-America",
         "AF": "Africa",
         "OC": "Oceania"
     }
     countries_not_found_table = {
         "England": "Europe",
         "Wales": "Europe",
         "Scotland": "Europe",
         "Northern Ireland": "Europe",
         "Kosovo": "Europe",
         "Chinese Taipei": "Asia",
         "Palestinian territories": "Asia",
         "East Timor": "Asia",
         "Netherlands Antilles": "South-America",
         "Saint Helena": "Africa"
     }
     try:
         iso2_cname = pycc.country_name_to_country_alpha2(country_name)
         continent_code = pycc.country_alpha2_to_continent_code(iso2_cname)
         return continents_table.get(continent_code)
     except KeyError:
         return countries_not_found_table.get(country_name)
Exemplo n.º 14
0
def get_continent(col):
    try:
        a =  country_name_to_country_alpha2(col)
        cn_a2_code.append(a)
        countryName.append(col)
        geolocate(a)
    except:
        cn = 'Unknown'
Exemplo n.º 15
0
def country_to_continent(country_name):
    "Convert country name string to continent"
    country_alpha2 = pcc.country_name_to_country_alpha2(country_name)
    country_continent_code = pcc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pcc.convert_continent_code_to_continent_name(
        country_continent_code)
    return country_continent_name
Exemplo n.º 16
0
def country_to_continent():
    country_name = input("country name = ")
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(
        country_continent_code)
    print(country_continent_name)
Exemplo n.º 17
0
def load_master_data():
    """
    Continent details for each city from weatherbit metadata is pulled using pycountry_convert API and stored in
    city_country_mdata table.

    :return void:
    """
    db_name = "postgres"
    db_password = "******"
    db_user = "******"
    db_host = "localhost"

    root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    filepath = os.path.join(root_dir, "data")

    data = pd.read_csv(os.path.join(filepath, "cities_all.csv"))
    data["city_country"] = data["city_name"].str.cat(data["country_full"],
                                                     sep=", ")
    data = data.dropna()

    try:
        conn = psycopg2.connect(dbname=db_name,
                                password=db_password,
                                user=db_user,
                                host=db_host)
        cur = conn.cursor()

        for item in data.itertuples():
            continent_flag = 0
            continent_name = ""

            if re.sub("[^a-zA-Z0-9, ]+", "", item[8]):
                try:
                    country_code = pc.country_name_to_country_alpha2(
                        item[5], cn_name_format="default")
                    continent_name = pc.country_alpha2_to_continent_code(
                        country_code)
                except:
                    continent_flag = 1

                if continent_flag == 0:
                    # query = """ INSERT INTO city_country_mdata (lat, lon, citycountry, continent) VALUES (%s,%s,%s,%s) """
                    record = (item[6], item[7],
                              re.sub("[^a-zA-Z0-9, ]+", "",
                                     item[8]), continent_name)
                    cur.execute(query, record)

        conn.commit()
        conn.close()

    except Exception as e:
        print("Exiting load_master_data due to exception: ", e.__class__)

    finally:
        if conn:
            cur.close()
            conn.close()
Exemplo n.º 18
0
def upload_csv(request):
    data = {'fields': PLAYER_ATTRIBUTES}
    if request.method == "POST":
        try:
            csv_file = request.FILES["csv_file"]
            # check for file format
            if not csv_file.name.endswith('.csv'):
                messages.error(request, 'File is not CSV type')
                return HttpResponseRedirect(reverse("upload-csv"))

            # do necessary operations to read the request file
            decoded_file = csv_file.read().decode('utf-8').splitlines()
            reader = csv.DictReader(decoded_file)

            for row in reader:
                type = row.get('type')[:2]
                name = row.get('name')
                if not type or not name:
                    continue  # skip if no type or name in the CSV row

                # below we get a player with the row type and the given name or we create one
                player, _ = eval(type.upper() +
                                 'Player.objects.get_or_create(name="{name}")'.
                                 format(name=name))

                # below we get team name if one is given and assign the player to that team.
                # we don't create a new team if it doesn't exist(discussable)
                team_name = row.get('team')
                if team_name:
                    team = Team.objects.filter(name=team_name).first()
                    if team:
                        player.team_id = team.id

                # parse remaining attributes, any that exist on the player model and are present in PLAYER_ATTRIBUTES,
                # will drop an error if the datatypes don't match(digit for a name, string for a number) etc.
                for attribute in PLAYER_ATTRIBUTES:
                    csv_attribute_value = row.get(attribute)
                    if csv_attribute_value:  # skip if no attribute in CSV row
                        setattr(
                            player, attribute,
                            csv_attribute_value)  # set attribute for player
                if len(player.country) != 2:
                    try:
                        player.country = country_name_to_country_alpha2(
                            player.country, cn_name_format="default")
                    except KeyError:
                        print(
                            "Invalid Country Name, setting to default. You may change the country in CSV and upload "
                            "the file again. It'll update the country name.")
                player.save()
                print(player)

        except Exception as e:
            print(e)
            messages.error(request, "Unable to upload file. " + repr(e))

    return render(request, "mysite/upload_csv.html", data)
Exemplo n.º 19
0
                    def country_to_continent(country_name):
                        country_continent_name = ""
                        try:
                            country_alpha2 = pc.country_name_to_country_alpha2(country_name)
                            country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
                            country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)
                        except:
                            pass

                        return country_continent_name
Exemplo n.º 20
0
def get_continent(col):
    try:
        cn_a2_code = country_name_to_country_alpha2(col)
    except:
        cn_a2_code = 'Unknown'
    try:
        cn_continent = country_alpha2_to_continent_code(cn_a2_code)
    except:
        cn_continent = 'Unknown'
    return (cn_a2_code, cn_continent)
Exemplo n.º 21
0
 def code2name(country, accronym):
     if country + '-' + accronym not in state_names:
         alpha2 = pcc.country_name_to_country_alpha2(
             country, cn_name_format="default")
         query = alpha2 + '-' + accronym
         result = pycountry.subdivisions.get(code=query).name
         state_names[country + '-' + accronym] = result
         return result
     else:
         return state_names[country + '-' + accronym]
Exemplo n.º 22
0
def country_to_continent(list_of_countries):
    import pycountry_convert as pc
    continents=[]
    for i in list_of_countries:
        try:
            country_code = pc.country_name_to_country_alpha2(i, cn_name_format="default")
            continents.append(pc.country_alpha2_to_continent_code(country_code))
        except:
            continents.append('Not found')
    return(continents)
def converting_countries_to_continents(countries_list):
    continent_list = []
    for index, cou in enumerate(countries_list):
        #print(index)
        if cou != "NA":
            if type(cou) != list and cou == "Republic of Korea":
                #print("korea found")
                continent_list.append("Asia")
            elif type(cou) != list and cou == "Côte D'Ivoire":
                continent_list.append("Africa")
            elif type(cou) != list:
                continent_list.append(
                    continents[country_alpha2_to_continent_code(
                        country_name_to_country_alpha2(cou))])
            elif type(cou) == list:
                #print(cou)
                temp = []
                for c in cou:
                    if c.strip() == "Republic of Korea":
                        #print("gere")
                        temp.append("Asia")
                    else:
                        temp.append(
                            continents[country_alpha2_to_continent_code(
                                country_name_to_country_alpha2(c.strip()))])
                continent_list.append(temp)
        else:
            continent_list.append("NA")

    return continent_list


# df['continents']=converting_countries_to_continents(df['location_countries'])
# print(df.head())
# print(df.tail())
# #print(df.loc[5893])
#
# df.to_csv(r'C:\Users\rsurulinathan\Downloads\continent_added_cts.tsv',sep='\t',index=False)

#https://stackoverflow.com/questions/23111990/pandas-dataframe-stored-list-as-string-how-to-convert-back-to-list
#https://stackoverflow.com/questions/45758646/pandas-convert-string-into-list-of-strings
#https://stackoverflow.com/questions/14714181/conditional-logic-on-pandas-dataframe
Exemplo n.º 24
0
def map_country_to_continent(String):
    """
    maps country name to continent name
    returns `None` if no match is found 
    """
    try:
        # if it is a country name, map to continent 
        country_code = pc.country_name_to_country_alpha2(String, cn_name_format="lower")
        continent_name = convert_country_alpha2_to_continent(country_code)
    except:
        continent_name = None
    return continent_name
Exemplo n.º 25
0
def kpi_continent_filer_options(continent):
    if continent == 'all':
        return country_options
    else:
        options = []
        countries = continent_filter[continent]
        for country in countries:
            options.append({
                'label': country,
                'value': country_name_to_country_alpha2(country)
            })
        return options
def get_continent(col):
    if col=='UK':
        return ('GB','EU') # To convert values for UK
    try:
        cn_a2_code =  country_name_to_country_alpha2(col)
    except:
        cn_a2_code = 'Unknown' 
    try:
        cn_continent = country_alpha2_to_continent_code(cn_a2_code)
    except:
        cn_continent = 'Unknown' 
    return (cn_a2_code, cn_continent)
Exemplo n.º 27
0
def get_data_from_repo(df):

    global africa_standardized_data
    df['Country/Region'] = df['Country/Region'].str.replace('*', '')
    name = []
    common_name = []
    official_name = []
    alpha_2 = []
    for i in list(pc.countries):
        name.append(i.name)
        alpha_2.append(i.alpha_3)
        if hasattr(i, "common_name"):
            common_name.append(i.common_name)
        else:
            common_name.append('No Common Name')

        if hasattr(i, "offical_name"):
            official_name.append(i.official_name)
        else:
            official_name.append('No Official Name')

        iso_alpha_dict = dict(zip(name, alpha_2))
        df['Country/Region'] = df['Country/Region'].replace(standard_names)
        df['iso_alpha'] = df['Country/Region'].replace(iso_alpha_dict)
        df['Validation'] = df['Country/Region'].apply(lambda x: 'Yes'
                                                      if x in name else 'No')
        standardized_data = df[df['Validation'] == 'Yes']

        continent_code_to_name = {
            'AS': 'ASIA',
            'NA': 'NORTH AMERICA',
            'OC': 'OCEANA',
            'EU': 'EUROPE',
            'AF': 'AFRICA',
            'SA': 'SOUTH AMERICA'
        }
    codes = []
    for (i, row) in standardized_data.iterrows():
        country_code = pc1.country_name_to_country_alpha2(
            row['Country/Region'], cn_name_format="default")
        continent_name = pc1.country_alpha2_to_continent_code(country_code)
        codes.append(continent_name)
    standardized_data['continent_codes'] = codes
    standardized_data['continent_codes'] = standardized_data[
        'continent_codes'].replace(continent_code_to_name)
    africa_standardized_data = standardized_data[
        standardized_data['continent_codes'] == 'AFRICA']
    africa_standardized_data.groupby([
        'Country/Region', 'Latitude', 'Longitude', 'Recovered', 'Deaths',
        'iso_alpha'
    ])['Confirmed'].value_counts().reset_index(name="")
    return africa_standardized_data
Exemplo n.º 28
0
def get_continent(country):
    try:
        iso2 = pc.country_name_to_country_alpha2(country)
        return pc.country_alpha2_to_continent_code(iso2)
    except KeyError:
        if country == "Vatican" or country == "Sint Maarten (Dutch part)" or country == "Kosovo" or country == "Faeroe Islands" or country == "Bonaire Sint Eustatius and Saba":
            return "EU"
        elif country == "Timor":
            return "AS"
        elif country == "Curacao":
            return "NA"
        elif country == "Democratic Republic of Congo" or country == "Cote d'Ivoire":
            return "AF"
def filter_continent(c, filter_continent_name="EU"):
    country_code = pc.country_name_to_country_alpha2(
        c["country"], cn_name_format="default"
    )
    try:
        continent_name = pc.country_alpha2_to_continent_code(country_code)
    except Exception as e:
        print(country_code, "has error", e)
        return False
    if continent_name == filter_continent_name:
        return True
    else:
        return False
Exemplo n.º 30
0
def continent_classification(unique_country_list):

    continent_dict = {}

    if (unique_country_list is None):
        print("Country list is empty")
        return (None)

    unique_country_list = list(set(unique_country_list))

    continent_code_to_continent = {
        'AF': 'Africa',
        'AS': 'Asia',
        'NA': 'North America',
        'SA': 'South America',
        'EU': 'Europe',
        'OC': 'Oceania'
    }
    for country in unique_country_list:

        try:
            country_code = pc.country_name_to_country_alpha2(
                country, cn_name_format="default")
            continent_name = continent_code_to_continent[
                pc.country_alpha2_to_continent_code(country_code)]
        except:
            print('Country unknown ERROR:', country)
            continue

        if continent_name not in continent_dict.keys():
            continent_dict[continent_name] = []

        continent_dict[continent_name].append(country)

    if ('The former Yugoslav Republic of Macedonia' in unique_country_list):
        continent_dict['Europe'].append(
            'The former Yugoslav Republic of Macedonia')

    for outlier in ['Hong Kong (S.A.R.)', 'Republic of Korea', 'timor-leste']:
        if (outlier in unique_country_list):
            continent_dict['Asia'].append(outlier)

    for outlier in ['Libyan Arab Jamahiriya', 'Congo, Republic of the...']:
        if (outlier in unique_country_list):
            continent_dict['Africa'].append(outlier)

    if ('Venezuela, Bolivarian Republic of...' in unique_country_list):
        continent_dict['South America'].append(
            'Venezuela, Bolivarian Republic of...')

    return (continent_dict)