def test_country_code(self): """Does a country like Mexico or United States get a code assigned?""" country_name = 'United States' # population = int(float(pop_dict['Value'])) code = get_country_code(country_name) #Assert methods verifies result received matches expected one self.assertEqual(code, 'usa')
def test_get_country_code(self): code = get_country_code("Andorra") self.assertEqual(code, "ad")
import json import pygal from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS from country_codes import get_country_code filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country = pop_dict['Country Name'] code = get_country_code(country) population = int(float(pop_dict['Value'])) if code: cc_populations[code] = population cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm_style = RS('#336699', base_style=LCS) wm = pygal.maps.world.World(style=wm_style)
from country_codes import get_country_code from pygal.style import RotateStyle filename = 'pop_growth.csv' with open(filename) as f: reader = csv.reader(f) header_row = next(reader) # Printing the header row, in order to accurately parse the file. for index, column_header in enumerate(header_row): print(index, column_header) # Creating a dictionary of country codes as keys, pop growth is the value. pop_growth_2017 = {} for row in reader: '''Loop through each row and convert the country into a country code and the the row for 2017 into a key''' country_code = get_country_code(row[0]) if country_code == None: continue else: try: pop_growth_2017[country_code] = float(row[61]) except ValueError: continue # Format and render file. wm_style = RotateStyle('#336699') wm = pygal.maps.world.World(style=wm_style) wm.title = 'Population Growth in 2017 by Country' wm.add('2017', pop_growth_2017) wm.render_to_file('pop_growth.svg')
def test_country_name_none(self): """Do names like 'Utopia' return None?""" country_code = get_country_code('Utopia') self.assertEqual(country_code, None)
from pygal.style import LightColorizedStyle, RotateStyle import country_codes filename = "population_data.json" with open(filename) as file: population_data = json.load(file) # Build a dictionary of population data. cpop_lo, cpop_med, cpop_hi = {}, {}, {} for data in population_data: if data['Year'] == "2010": country_name = data['Country Name'] population = int(float(data['Value'])) country_code = country_codes.get_country_code(country_name) if country_code: if population < 10000000: cpop_lo[country_code] = population elif population < 1000000000: cpop_med[country_code] = population else: cpop_hi[country_code] = population print(len(cpop_lo), len(cpop_med), len(cpop_hi)) wmap_style = RotateStyle('#336699', base_style=LightColorizedStyle) wmap = pygal.maps.world.World(style=wmap_style) wmap.title = 'World Population in 2010, by Country' wmap.add('0-10m', cpop_lo) wmap.add('10m-1bn', cpop_med) wmap.add('>1bn', cpop_hi)
def test_if_none(self): answer = get_country_code('xyz') self.assertEqual(answer, None)
def test_get_country_code(self): country_code = get_country_code('United States') self.assertEqual(country_code,'us')
def funcname(self): """测试get_country_code""" country_name = get_country_code('United States') self.assertEqual(country_name, 'US')
def test_country_name_in_countries(self): """Do country names like 'Andorra' work?""" country_code = get_country_code('Andorra') self.assertEqual(country_code, 'ad')
def test_country_name_not_in_countries(self): """Do country names like 'Venezuela, RB' work?""" country_code = get_country_code('Venezuela, RB') self.assertEqual(country_code, 've')
def test_CC(self): """能正确返回这些国别码吗?""" for country,cc in zip(self.country_list, self.cc_list): CC = get_country_code(country) self.assertEqual(CC, cc)
from country_codes import get_country_code filename = "population_data.json" with open(filename) as f: # 打开文件 pop_data = json.load(f) # 读取文件 # cc_populations = {} # 定义空字典以存储国家人口键值对 for pop_dic in pop_data: # 遍历列表中的字典 if pop_dic['Year'] == '2010': # 判断字典中的年份是否为2010 country_name = pop_dic['Country Name'] # 获取字典值复制变量 population = int(float(pop_dic['Value'])) # 获取字典值复制变量 # print(country_name + ": " + str(population)) # 打印拼接变量 code = get_country_code(country_name) # 调用函数传入参数赋值变量 if code: # 判断是否为True # print(code + ": " + str(population)) # 打印国家代码和人口数量 cc_populations[code] = population # 禁用打印,将国别码和人口数量分别作为键和值填充字典 # else: # 否则 # print('Error - ' + country_name) # 打印错误信息和传入参数国家名称 # 根据人口数量分为三个组 cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} # 定义三个分组字典 for cc, pop in cc_populations.items(): if pop < 10000000: # 小于1000万 cc_pops_1[cc] = pop # 写入字典1 elif pop < 1000000000: # 小于10亿 cc_pops_2[cc] = pop # 写入字典2 else: # 大于10亿 cc_pops_3[cc] = pop # 写入字典3
from country_codes import get_country_code import json from pygal.maps.world import COUNTRIES filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) pop_data2010 = [] for dictionary in pop_data: if dictionary['Year'] == '2010': pop_data2010.append(dictionary) #print(len(pop_data2010)) #print(pop_data2010) pop_temp = [] for countries in pop_data2010: code = get_country_code(countries['Country Name']) if code == None: print(countries['Country Name']) print(COUNTRIES)
def test_get_country_codes(self): country_code_name = get_country_code("Poland") self.assertEqual(country_code_name, "pl")
def test_missing_cc(self): """Respond to country codes that are not found in pygal/COUNTRIES""" cc = get_country_code('Bolivia') self.assertEqual(cc, 'bol')
# Open file and extract data filename = "life_female.csv" with open(filename) as f: lifedata = csv.reader(f) # Get to the proper column line on the csv for n in range(1, 6): next(lifedata) # Make a new dictionary. Translate the country names # to country_codes, and put them in as keys and the # life expectancy data as values (taken from the 2014 data set) lrates = {} for rowdata in list(lifedata): ccode = get_country_code(rowdata[0]) try: lrates[ccode] = float(rowdata[58]) except ValueError: continue # Plot new graph and output to file wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.force_uri_protocol = 'http' wm.title = 'Female Life Expectancy in 2014, by Country' wm.add('Years', lrates) wm.render_to_file('female_mortality.svg')
def test_cc(self): """Do the correct country codes get returned?""" cc = get_country_code('Japan') self.assertEqual(cc, 'jp')
} def print_trends(arg): trends = get_trends(arg) n = 1 for trend, value in trends.items(): print(f"{n}. {trend} - {value[0]} hits") if value[1].date( ) == datetime.today().date() else None n += 1 if __name__ == '__main__': inp = input("Enter a 2-letter country code to start search: ") code_length = 2 code_list = get_country_code(code_length) while True: if len(inp) == code_length and inp.upper() not in code_list: print(inp) inp = input("Please enter a valid country code: ") elif len(inp) != code_length: inp = input("Please enter a 2-letter country code.\n" "A list of alpha-2 codes can be found here: \n" "https://www.iban.com/country-codes\n" "Country code: ") else: break print_trends(inp.upper())
main_dit = {} names, data = [], [] for row in reader: table_nam = row[2] print(table_nam) data_info = row[8] main_dit[table_nam] = data_info new_dit = {} for key, value in main_dit.items(): code = get_country_code(key) if code: new_dit[code] = value print(key + " ---- " + value) print(new_dit) upper_middle_income, lower_middle_income, high_income, low_income = {}, {}, {}, {} for key, value in new_dit.items(): if value == 'Upper middle income': upper_middle_income[key] = value elif value == 'Lower middle income': lower_middle_income[key] = value elif value == 'High income':
def test_for_poland(self): answer = get_country_code('Poland') self.assertEqual(str(answer), 'pl')
def test_country_codes_singer(self): country_name = 'China' code = get_country_code(country_name) self.assertEqual(code, 'cn')
def test_special_country_name(self): """Do names like 'Hong Kong SAR, China' work?""" country_code = get_country_code('Hong Kong SAR, China') self.assertEqual(country_code, 'hk')
def test_country_codes_inner(self): country_name = 'Iran, Islamic Republic of' code = get_country_code(country_name) self.assertEqual(code, 'ir')
def test_default_country_name(self): """Do names like 'Belgium' work?""" country_code = get_country_code('Belgium') self.assertEqual(country_code, 'be')
def test_country_codes_two_words(self): country_name = 'United Kingdom' code = get_country_code(country_name) self.assertEqual(code, 'gb')
import json from pygal_maps_world.maps import World from pygal.style import LightColorizedStyle as LCS,RotateStyle as RS from country_codes import get_country_code #将数据加载到一个列表中 file_name = "Document/population_data.json" with open(file_name) as f: pop_date = json.load(f) #打印每个国家2010年人口数量,创建一个包含人口数量的字典 cc_populations = {} for pop_dict in pop_date: if pop_dict["Year"] == "2010": country_name = pop_dict["Country Name"] #Python不能直接将包含小数点的字符串'1127437398.85751' 转换为整数 #为消除这种错误,我们先将字符串转换为浮点数,再将浮点数转换为整数 population = int(float(pop_dict["Value"])) code = get_country_code(country_name) if code: cc_populations[code] = population else: print("ERROR - " + country_name) #根据人口数量将所有国家分为三组 cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop
def test_case_country(self): """Do upper case or lower case countries work?""" code = get_country_code('UNITED STATES') self.assertEqual(code, 'us')
filename = 'population_data.json' filepath = os.path.dirname(__file__) + '/json/' + filename openfile = open(filepath) # with open(os.path.join(sys.path[0], filename), "r") as f: with openfile as f: pop_data = json.load(f) # Build a dictionary of population data. cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # Group the countries into 3 population levels. cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop # See how many countries are in each level. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))
def test_country_codes(self): """Do countries like 'Andorra' work?""" cc = get_country_code('Andorra') self.assertEqual(cc, 'ad')
from pygal.style import LightColorizedStyle from country_codes import get_country_code # Load the data into a list. filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) # Print the 2010 population for each country. # Build a dictionary of population data. cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: # print(code + ": " + str(population)) cc_populations[code] = population else: print('ERROR - ' + country_name) # Group the countries into 3 population levels. cc_pops_1 = {} cc_pops_2 = {} cc_pops_3 = {} for cc, pop in cc_populations.items(): if pop < 100000000: cc_pops_1[cc] = pop elif pop < 1000000000:
#!/usr/bin/python # -*- coding: UTF-8 -*- import pygal_maps_world.maps import json #此处模块自己编写的,见country_codes.py from country_codes import get_country_code cc_populations = {} with open('country_population.csv', 'r') as f: for line in f.readlines(): datas = line.split(',') countryCode = get_country_code(datas[0].strip('"')) squan = datas[-3].strip().strip('"') if (squan.isdigit()): quantity = int(squan) cc_populations[countryCode] = quantity else: continue cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop wm = pygal_maps_world.maps.World()