def sector_percentages(xml, resource=no_resource): ret = [] for ele in xml.xpath("./sector"): sp = SectorPercentage() field_functions = { 'sector' : partial(from_codelist, cl.Sector, "@code"), 'vocabulary' : partial(from_codelist, cl.Vocabulary, "@vocabulary"), } for field, function in field_functions.items(): try: setattr(sp, field, function(ele, resource)) except (MissingValue, ValueError), exe: iati_identifier = xval(xml, "/iati-activity/iati-identifier/text()", 'no_identifier') log.warn( _("uFailed to import a valid {0} in activity {1}, error was: {2}".format( field, iati_identifier, exe), logger='activity_importer', dataset=resource.dataset_id, resource=resource.url), exc_info=exe ) if ele.xpath("@percentage"): try: sp.percentage = int(xval(ele, "@percentage")) except ValueError: sp.percentage = None if ele.xpath("text()"): sp.text = xval(ele, "text()") if any(getattr(sp, attr) for attr in "sector vocabulary percentage".split()): ret.append(sp)
def sector_percentages(xml, resource=no_resource): ret = [] for ele in xml.xpath("./sector"): sp = SectorPercentage() field_functions = { 'sector': partial(from_codelist, cl.Sector, "@code"), 'vocabulary': partial(from_codelist, cl.Vocabulary, "@vocabulary"), } for field, function in field_functions.items(): try: setattr(sp, field, function(ele, resource)) except (MissingValue, ValueError), exe: iati_identifier = xval( xml, "/iati-activity/iati-identifier/text()", 'no_identifier') log.warn(_( "uFailed to import a valid {0} in activity {1}, error was: {2}" .format(field, iati_identifier, exe), logger='activity_importer', dataset=resource.dataset_id, resource=resource.url), exc_info=exe) if ele.xpath("@percentage"): try: sp.percentage = int(xval(ele, "@percentage")) except ValueError: sp.percentage = None if ele.xpath("text()"): sp.text = xval(ele, "text()") if any( getattr(sp, attr) for attr in "sector vocabulary percentage".split()): ret.append(sp)
def sector_percentages(xml): ret = [] for ele in xml: sp = SectorPercentage() if ele.xpath("@code") and xval(ele, "@code") in cl.Sector.values(): sp.sector = cl.Sector.from_string(xval(ele, "@code")) if ele.xpath("@vocabulary"): sp.vocabulary = cl.Vocabulary.from_string(xval(ele, "@vocabulary")) if ele.xpath("@percentage"): sp.percentage = int(xval(ele, "@percentage")) if any(getattr(sp, attr) for attr in "sector vocabulary percentage".split()): ret.append(sp) return ret