def find_region_from_ISO3(i3, reglist): result = utility.get_default_error() for reg in reglist: if (reg['Country_ISO3'] != utility.get_default_error()) and (reg['Country_ISO3'].upper().strip() == i3.upper().strip()): result = reg['Country_Region'] break return result
def ISO_from_name(cname, cslist, tag): res = utility.get_default_error() found = False cs = cslist[0] ss = cslist[1] sname = (utility.to_str(cname)).upper().strip() for c in cs: name = (utility.to_str(c['Name'])).upper().strip() if (sname == name): found = True res = c[tag] break else: found = False if (not found): for s in ss: names = ((utility.to_str(s['Country'])).upper().strip()).split(delimiter) for name in names: mname = str(name).upper().strip() if (sname == mname): res = s[tag] found = True #print "MINOR CONCERN - ", sname, " was found in the country synonym list." break if found == True: break if res == utility.get_default_error(): print "MAJOR ERROR - ", sname, " was not found in the ISO country or synonym lists" return res
def combine(c, gds, country_profiles, mds, sts, stats_attribs, md_attribs): newrow = OrderedDict() country_name = c['Country_Name'] iso2 = c['Country_ISO2'] iso3 = c['Country_ISO3'] region = c['Country_Region'] this_cp = utility.get_tuple_by_X(iso3, "Country_ISO3", country_profiles) this_md = utility.get_tuple_by_X(iso3, "Country_ISO3", mds) this_sts = utility.get_tuple_by_X(iso3, "Country_ISO3", sts) this_goods = goods.get_good_tuples_for_country(gds, country_name) cpkeys = this_cp.keys() skeys = this_sts.keys() mkeys = this_md.keys() advancement = (this_cp['advancement_level'] if (len(cpkeys) != 0) else "") description = (this_cp['description'] if (len(cpkeys) != 0) else "") sections = (this_cp['sections'] if (len(cpkeys) != 0) else []) sources = (this_cp['sources'] if (len(cpkeys) != 0) else []) newrow['Name'] = country_name newrow['Region'] = region newrow['ISO2'] = iso2 newrow['ISO3'] = iso3 newrow['Advancement_Level'] = advancement newrow['Description'] = description newrow['Goods'] = this_goods newrow['Country_Statistics'] = [] sfound = False if (stats.found_stats_from(iso3, "Country_ISO3", sts) == utility.get_default_error()) else True if not sfound: print " No Country Statistics for ", country_name if (skeys != 0) and (sfound): nray = [] for count in range(4, len(stats_attribs)): nr = OrderedDict() curkey = stats_attribs[count] try: nr[curkey] = this_sts[curkey] except KeyError: nr[curkey] = "" nray.append(nr) newrow['Country_Statistics'] = nray newrow['Master_Data'] = [] mfound = False if (master_data.found_master_data_from( iso3, "Country_ISO3", mds) == utility.get_default_error()) else True if not mfound: print " No Master Data for ", country_name if (mkeys != 0) and (mfound): mray = [] for newcount in range(5, len(md_attribs)): nr = OrderedDict() curkey = md_attribs[newcount] try: nr[curkey] = this_md[curkey] except KeyError: nr[curkey] = "" mray.append(nr) newrow['Master_Data'] = mray newrow['Sections'] = sections newrow['Sources'] = sources return newrow
def include_extra(masterdata, yr): md_list = [] if (yr != get_current_year()) : print "SWITCHING years from ", get_current_year(), " to ", yr, ".\n" set_regions(yr) set_current_year(yr) for mdrec in masterdata: md = OrderedDict() md['Country_Name'] = mdrec['Country_Name'] md['Country_ISO2'] = ISO_countries.ISO2_from_name(md['Country_Name'], csl) md['Country_ISO3'] = ISO_countries.ISO3_from_name(md['Country_Name'], csl) if ( md['Country_ISO2'] == utility.get_default_error() ): print "There is no ISO2 for ", md['Country_Name'] if ( md['Country_ISO3'] == utility.get_default_error() ): print "There is no ISO3 for ", md['Country_Name'] md['Country_Region'] = regions.find_region_from_ISO3(md['Country_ISO3'], regs) md[CWS+'_Age_Range'] = mdrec[CWS+'_Age_Range'] md[CWS+'_Total_Percentage_of_Working_Children'] = mdrec[CWS+'_Total_Percentage_of_Working_Children'] md[CWS+'_Total_Working_Population'] = mdrec[CWS+'_Total_Working_Population'] md[CWS+'_Agriculture'] = mdrec[CWS+'_Agriculture'] md[CWS+'_Services'] = mdrec[CWS+'_Services'] md[CWS+'_Industry'] = mdrec[CWS+'_Industry'] md[ESAS+'_Year'] = mdrec[ESAS+'_Year'] md[ESAS+'_Age_Range'] = mdrec[ESAS+'_Age_Range'] md[ESAS+'_Percentage'] = mdrec[ESAS+'_Percentage'] md[CWAS+'_Year'] = mdrec[CWAS+'_Year'] md[CWAS+'_Age_Range'] = mdrec[CWAS+'_Age_Range'] md[CWAS+'_Total'] = mdrec[CWAS+'_Total'] md[UPCR+'_Year'] = mdrec[UPCR+'_Year'] md[UPCR+'_Rate'] = mdrec[UPCR+'_Rate'] md_list.append(md) return md_list
def include_extra(goods_list, yer): result = [] if yer != get_base_year(): set_sectors(yer) set_regions(yer) set_base_year(yer) for good in goods_list: g = OrderedDict() g['Country_Name'] = good['Country_Name'] g['Country_ISO2'] = ISO_countries.ISO2_from_name( g['Country_Name'], csl) g['Country_ISO3'] = ISO_countries.ISO3_from_name( g['Country_Name'], csl) if (g['Country_ISO2'] == utility.get_default_error()): print " No ISO2 was found for ", g['Country_Name'] if (g['Country_ISO3'] == utility.get_default_error()): print " No ISO3 was found for ", g['Country_Name'] g['Country_Region'] = regions.find_region_from_ISO3( g['Country_ISO3'], regs) g['Good_Name'] = good['Good_Name'] g['Good_Sector'] = sectors.find_sector_from_name(g['Good_Name'], secs) g['Child_Labor'] = ("Yes" if (good['Child_Labor'] == present) else "No") g['Forced_Labor'] = ("Yes" if (good['Forced_Labor'] == present) else "No") g['Forced_Child_Labor'] = ("Yes" if (good['Forced_Child_Labor'] == present) else "No") result.append(g) return result
def find_region_from_ISO3(i3, reglist): result = utility.get_default_error() for reg in reglist: if (reg['Country_ISO3'] != utility.get_default_error()) and ( reg['Country_ISO3'].upper().strip() == i3.upper().strip()): result = reg['Country_Region'] break return result
def combine(c, gds, country_profiles, mds, sts, stats_attribs, md_attribs): newrow = OrderedDict() country_name = c['Country_Name'] iso2 = c['Country_ISO2'] iso3 = c['Country_ISO3'] region = c['Country_Region'] this_cp = utility.get_tuple_by_X(iso3, "Country_ISO3", country_profiles) this_md = utility.get_tuple_by_X(iso3, "Country_ISO3", mds) this_sts = utility.get_tuple_by_X(iso3, "Country_ISO3", sts) this_goods = goods.get_good_tuples_for_country(gds, country_name) cpkeys = this_cp.keys() skeys = this_sts.keys() mkeys = this_md.keys() advancement = ( this_cp['advancement_level'] if (len(cpkeys) != 0) else "" ) description = ( this_cp['description'] if (len(cpkeys) != 0) else "" ) sections = (this_cp['sections'] if (len(cpkeys) != 0) else []) sources = ( this_cp['sources'] if (len(cpkeys) != 0) else []) newrow['Name'] = country_name newrow['Region'] = region newrow['ISO2'] = iso2 newrow['ISO3'] = iso3 newrow['Advancement_Level'] = advancement newrow['Description'] = description newrow['Goods'] = this_goods newrow['Country_Statistics'] = [] sfound = False if ( stats.found_stats_from(iso3, "Country_ISO3", sts) == utility.get_default_error()) else True if not sfound: print " No Country Statistics for ", country_name if (skeys != 0) and (sfound) : nray = [] for count in range(4,len(stats_attribs)): nr = OrderedDict() curkey = stats_attribs[count] try: nr[curkey] = this_sts[curkey] except KeyError: nr[curkey] = "" nray.append(nr) newrow['Country_Statistics'] = nray newrow['Master_Data'] = [] mfound = False if (master_data.found_master_data_from(iso3, "Country_ISO3", mds) == utility.get_default_error()) else True if not mfound: print " No Master Data for ", country_name if (mkeys != 0) and (mfound): mray = [] for newcount in range(5,len(md_attribs)): nr = OrderedDict() curkey = md_attribs[newcount] try: nr[curkey] = this_md[curkey] except KeyError: nr[curkey] = "" mray.append(nr) newrow['Master_Data'] = mray newrow['Sections'] = sections newrow['Sources'] = sources return newrow
def get_master_data_from_iso3(iso3, mlist): md = utility.get_default_error() index = found_master_data_from(iso3, "Country_ISO3", mlist) if index != utility.get_default_error(): print "found" md = mlist[index] else: print "not found" return md
def get_stats_from_iso3(iso3, mlist): stats = utility.get_default_error() index = found_stats_from(iso3, "Country_ISO3", mlist) if index != utility.get_default_error(): print "found" stats = mlist[index] else: print "not found" return stats
def get_stats_from_iso3(iso3, mlist): stats = utility.get_default_error() index = found_stats_from(iso3, "Country_ISO3", mlist) if index != utility.get_default_error() : print "found" stats = mlist[index] else: print "not found" return stats
def ISO3_from_ISO2(iso2, cslist): result = utility.get_default_error() cs = cslist[0] for row in cs: if str(row["ISO2"]) == str(iso2): result = row["ISO3"] return result
def name_from_ISO3(iso3, cslist): result = utility.get_default_error() cs = cslist[0] for row in cs: if str(row["ISO3"]) == str(iso3): result = row["Name"] return result
def found_stats_from(value, tag, mlist): found = utility.get_default_error() for count in range(0, len(mlist)): stat = mlist[count] if (stat[tag].strip().upper() == value.strip().upper()): found = count break return found
def found_master_data_from(value, tag, mlist): found = utility.get_default_error() for count in range(0, len(mlist)): md = mlist[count] if (md[tag].strip().upper() == value.strip().upper()): found = count break return found
def find_sector_from_name(goodname, slist): result = utility.get_default_error() ig = utility.canonical_good(goodname).upper().strip() for s in slist: cg = utility.canonical_good(s['Good_Name']).upper().strip() if (utility.goods_equal(ig, cg)): result = s['Good_Sector'] break return result
def found_stats_from(value, tag, mlist): found = utility.get_default_error() for count in range(0, len(mlist)): stat = mlist[count] if (stat[tag].strip().upper() == value.strip().upper()) : found = count break return found
def find_X_from_name(name, reglist, tag): result = utility.get_default_error() cname = name.upper().strip() for reg in reglist: try: pname = reg['Country_Name'].encode('utf-8').upper().strip() except UnicodeDecodeError: pname = reg['Country_Name'].upper().strip() if (pname == cname): result = reg[tag] break return result
def find_goods_in_sector(sector, slist): result = utility.get_default_error() searchstring = sector.upper().strip() runner = [] for sec in slist: if (searchstring == (sec['Good_Sector'].upper().strip())): cg = utility.canonical_good(sec['Good_Name']).strip() if (cg not in runner): runner.append(sec['Good_Name'].strip()) if (len(runner) > 0): result = sorted(runner) return result
def find_X_from_Y(tag1, tag2, reglist, request): result = utility.get_default_error() req = request.upper().strip() for reg in reglist: try: pvar = reg[tag2].encode('utf-8').upper().strip() except UnicodeDecodeError: pvar = reg[tag2].upper().strip() if (pvar == req): result = reg[tag1] break return result
def expand_syns(syns): result = utility.get_default_error() working_array = [] for s in syns: b = OrderedDict() b['ISO2'] = s['ISO2'] b['ISO3'] = s['ISO3'] strg = s['Country'] b['Country'] = utility.to_list(strg,";","name") working_array.append(b) result = working_array return result
def include_extra(masterdata, yr): md_list = [] if (yr != get_current_year()): print "SWITCHING years from ", get_current_year(), " to ", yr, ".\n" set_regions(yr) set_current_year(yr) for mdrec in masterdata: md = OrderedDict() md['Country_Name'] = mdrec['Country_Name'] md['Country_ISO2'] = ISO_countries.ISO2_from_name( md['Country_Name'], csl) md['Country_ISO3'] = ISO_countries.ISO3_from_name( md['Country_Name'], csl) if (md['Country_ISO2'] == utility.get_default_error()): print "There is no ISO2 for ", md['Country_Name'] if (md['Country_ISO3'] == utility.get_default_error()): print "There is no ISO3 for ", md['Country_Name'] md['Country_Region'] = regions.find_region_from_ISO3( md['Country_ISO3'], regs) md[CWS + '_Age_Range'] = mdrec[CWS + '_Age_Range'] md[CWS + '_Total_Percentage_of_Working_Children'] = mdrec[ CWS + '_Total_Percentage_of_Working_Children'] md[CWS + '_Total_Working_Population'] = mdrec[CWS + '_Total_Working_Population'] md[CWS + '_Agriculture'] = mdrec[CWS + '_Agriculture'] md[CWS + '_Services'] = mdrec[CWS + '_Services'] md[CWS + '_Industry'] = mdrec[CWS + '_Industry'] md[ESAS + '_Year'] = mdrec[ESAS + '_Year'] md[ESAS + '_Age_Range'] = mdrec[ESAS + '_Age_Range'] md[ESAS + '_Percentage'] = mdrec[ESAS + '_Percentage'] md[CWAS + '_Year'] = mdrec[CWAS + '_Year'] md[CWAS + '_Age_Range'] = mdrec[CWAS + '_Age_Range'] md[CWAS + '_Total'] = mdrec[CWAS + '_Total'] md[UPCR + '_Year'] = mdrec[UPCR + '_Year'] md[UPCR + '_Rate'] = mdrec[UPCR + '_Rate'] md_list.append(md) return md_list
def add_to_country_profiles(profiles): results = [] cpkeys = profiles[0].keys() for profile in profiles: newrec = OrderedDict() cn = profile['country'].encode('utf8') newrec['Country_ISO3'] = ISO_countries.ISO3_from_name(cn, csl) if ( newrec['Country_ISO3'] == utility.get_default_error() ): print "Could not find ISO3 for ", cn for n in range(0, len(cpkeys)): cur = cpkeys[n] newrec[cur] = profile[cur] results.append(newrec) return results
def add_to_country_profiles(profiles): results = [] cpkeys = profiles[0].keys() for profile in profiles: newrec = OrderedDict() cn = profile['country'].encode('utf8') newrec['Country_ISO3'] = ISO_countries.ISO3_from_name(cn, csl) if (newrec['Country_ISO3'] == utility.get_default_error()): print "Could not find ISO3 for ", cn for n in range(0, len(cpkeys)): cur = cpkeys[n] newrec[cur] = profile[cur] results.append(newrec) return results
def include_extra(goods_list, yer): result = [] if yer != get_base_year(): set_sectors(yer) set_regions(yer) set_base_year(yer) for good in goods_list: g = OrderedDict() g['Country_Name'] = good['Country_Name'] g['Country_ISO2'] = ISO_countries.ISO2_from_name(g['Country_Name'], csl) g['Country_ISO3'] = ISO_countries.ISO3_from_name(g['Country_Name'], csl) if (g['Country_ISO2'] == utility.get_default_error()): print " No ISO2 was found for ", g['Country_Name'] if (g['Country_ISO3'] == utility.get_default_error()): print " No ISO3 was found for ", g['Country_Name'] g['Country_Region'] = regions.find_region_from_ISO3(g['Country_ISO3'], regs) g['Good_Name'] = good['Good_Name'] g['Good_Sector'] = sectors.find_sector_from_name(g['Good_Name'], secs) g['Child_Labor'] = ( "Yes" if (good['Child_Labor'] == present) else "No" ) g['Forced_Labor'] = ( "Yes" if (good['Forced_Labor'] == present) else "No" ) g['Forced_Child_Labor'] = ( "Yes" if (good['Forced_Child_Labor'] == present) else "No" ) result.append(g) return result
def ISO3_from_name(cname, cslist): result = utility.get_default_error() iso3 = ISO_from_name(cname, cslist, "ISO3") if iso3 != utility.get_default_error(): result = iso3 return result
def ISO2_from_name(cname, cslist): res = utility.get_default_error() iso2 = ISO_from_name(cname, cslist, "ISO2") if iso2 != utility.get_default_error(): res = iso2 return res