def parse_multiple_countries(data_list, nominal_start=None, nominal_end=None): """ @param nominal_start: what start year did the query ask for (not necessarily get) @param nominal_end: what end year did the query ask for (not necessarily get) @return: list of countries """ #get_country_id = lambda item : item['country']['id'] # When it's a list of indicator the World bank returns ISO2 codes labeled 'id' get_country_id_iso2 = lambda item: item['country']['id'] countries = [] #data_list.sort(key = get_country_id) while len(data_list) > 0: current_id_iso2 = get_country_id_iso2(data_list[0]) country_data = filter( lambda item: get_country_id_iso2(item) == current_id_iso2, data_list) # we now basically don't know the country's "real" (iso3) id country = Country("") country.code_iso2 = current_id_iso2 indicator = parse_single_country(country_data, nominal_start, nominal_end) country.set_indicator(indicator) countries.append(country) # remove parsed data from the list #TODO: data_list.remove(i) data_list = [item for item in data_list if item not in country_data] return countries
def test_add_indicators_to_countries(self): c01=Country("country1") c01.code_iso2="c1" c02=Country("country2") c02.code_iso2="c2" countries = [c01,c02] ind1=Indicator("ind1", [1,2], [10,20]) ind2=Indicator("ind2", [1,2], [20,30]) c1 = Country("") c1.code_iso2="c1" c1.set_indicator(ind1) c11 = Country("") c11.code_iso2="c1" c11.set_indicator(ind2) ind3=Indicator("ind1", [1,2], [230,240]) ind4=Indicator("ind2", [1,2], [330,340]) c2 = Country("") c2.code_iso2="c2" c2.set_indicator(ind3) c22 = Country("") c22.code_iso2="c2" c22.set_indicator(ind4) countries1 = [c1, c2] countries2 = [c11, c22] country_indicators = [countries1, countries2] parser.add_indicators_to_countries(countries, country_indicators) self.assertEqual(len(countries), 2) self.assertEqual(len(countries[0].get_indicator("ind1").values), 2)
def parse_multiple_countries_alone(data_list): """ Parse a query for countries without indicators @return: list of countries """ get_country_id = lambda item: item['id'] get_country_id_iso2 = lambda item: item['iso2Code'] countries = [] data_list.sort(key=get_country_id) for item in data_list: current_id = get_country_id(item) current_id_iso2 = get_country_id_iso2(item) country = Country(current_id) country.code_iso2 = current_id_iso2 country.name = item['name'] countries.append(country) return countries
def parse_multiple_countries_alone(data_list): """ Parse a query for countries without indicators @return: list of countries """ get_country_id = lambda item : item['id'] get_country_id_iso2 = lambda item : item['iso2Code'] countries = [] data_list.sort(key = get_country_id) for item in data_list: current_id = get_country_id(item) current_id_iso2 = get_country_id_iso2(item) country = Country(current_id) country.code_iso2 = current_id_iso2 country.name = item['name'] countries.append(country) return countries
def parse_multiple_countries(data_list, nominal_start=None, nominal_end=None): """ @param nominal_start: what start year did the query ask for (not necessarily get) @param nominal_end: what end year did the query ask for (not necessarily get) @return: list of countries """ #get_country_id = lambda item : item['country']['id'] # When it's a list of indicator the World bank returns ISO2 codes labeled 'id' get_country_id_iso2 = lambda item : item['country']['id'] countries = [] #data_list.sort(key = get_country_id) while len(data_list)>0: current_id_iso2 = get_country_id_iso2(data_list[0]) country_data = filter(lambda item : get_country_id_iso2(item) == current_id_iso2, data_list) # we now basically don't know the country's "real" (iso3) id country = Country("") country.code_iso2=current_id_iso2 indicator = parse_single_country(country_data, nominal_start, nominal_end) country.set_indicator(indicator) countries.append(country) # remove parsed data from the list #TODO: data_list.remove(i) data_list = [item for item in data_list if item not in country_data] return countries