Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #767 from macieg/issue-766
Browse files Browse the repository at this point in the history
ISSUE-766 | Added new attribute method
  • Loading branch information
iandees authored Apr 19, 2020
2 parents 2f81c28 + 6d5747d commit 714c830
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
17 changes: 13 additions & 4 deletions openaddr/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions openaddr/tests/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 714c830

Please sign in to comment.