diff --git a/openaddr/conform.py b/openaddr/conform.py index 1af28c02..56600e79 100644 --- a/openaddr/conform.py +++ b/openaddr/conform.py @@ -966,14 +966,12 @@ def row_function(sd, row, key, fxn): row = row_fxn_chain(sd, row, key, fxn) elif function == "first_non_empty": row = row_fxn_first_non_empty(sd, row, key, fxn) + elif function == "get": + row = row_fxn_get(sd, row, key, fxn) return row - -### Row-level conform code. Inputs and outputs are individual rows in a CSV file. -### The input row may or may not be modified in place. The output row is always returned. - def row_transform_and_convert(sd, row): "Apply the full conform transform and extract operations to a row" @@ -1005,6 +1003,11 @@ def row_transform_and_convert(sd, row): row5 = row_calculate_hash(cache_fingerprint, row4) return row5 + + +### Row-level conform code. Inputs and outputs are individual rows in a CSV file. +### The input row may or may not be modified in place. The output row is always returned. + def fxn_smash_case(fxn): if "field" in fxn: fxn["field"] = fxn["field"].lower() @@ -1198,6 +1201,12 @@ def row_fxn_first_non_empty(sd, row, key, fxn): return row +def row_fxn_get(sd, row, key, fxn): + "Get single value from a field with many values" + row[var_types[key]] = row[fxn['field']][fxn['index']] + + return row + def row_canonicalize_unit_and_number(sd, row): "Canonicalize address unit and number" row["UNIT"] = (row["UNIT"] or '').strip() diff --git a/openaddr/tests/conform.py b/openaddr/tests/conform.py index 42586909..797e540f 100644 --- a/openaddr/tests/conform.py +++ b/openaddr/tests/conform.py @@ -21,6 +21,7 @@ row_fxn_postfixed_unit, row_fxn_remove_prefix, row_fxn_remove_postfix, row_fxn_chain, row_fxn_first_non_empty, + row_fxn_get, row_canonicalize_unit_and_number, conform_smash_case, conform_cli, convert_regexp_replace, conform_license, conform_attribution, conform_sharealike, normalize_ogr_filename_case, @@ -1377,6 +1378,23 @@ def test_row_first_non_empty(self): d = row_fxn_first_non_empty(c, d, "street", c["conform"]["street"]) self.assertEqual(e, d) + def test_row_fxn_get(self): + "New fxn get" + c = { + "conform": { + "region": { + "function": "get", + "field": "jednostkaAdministracyjna", + "index": 1 + } + }} + d = {"jednostkaAdministracyjna": ["Polska", "kujawsko-pomorskie", "brodnicki", "Bartnicza"]} + e = copy.deepcopy(d) + + e.update({"OA:region": "kujawsko-pomorskie"}) + d = row_fxn_get(c, d, "region", c["conform"]["region"]) + self.assertEqual(e, d) + class TestConformCli (unittest.TestCase): "Test the command line interface creates valid output files from test input" def setUp(self):