Skip to content

Commit

Permalink
Merge pull request #38 from rbrtmrtn/constant-attribute-fn
Browse files Browse the repository at this point in the history
Add constant attribute function and tests
  • Loading branch information
iandees authored Apr 29, 2023
2 parents abcf2f2 + 7756727 commit 4f363cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions openaddr/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,8 @@ def row_function(sc, row, key, fxn):
row = row_fxn_chain(sc, row, key, fxn)
elif function == "first_non_empty":
row = row_fxn_first_non_empty(sc, row, key, fxn)
elif function == "constant":
row = row_fxn_constant(sc, row, key, key, fxn)

return row

Expand Down Expand Up @@ -1031,6 +1033,14 @@ def row_fxn_first_non_empty(sc, row, key, fxn):

return row

def row_fxn_constant(sc, row, key, fxn):
"Set an attribute to a constant value"
value = fxn['value']

row['oa:{}'.format(key)] = value

return row

def row_canonicalize_unit_and_number(sc, row):
"Canonicalize address unit and number"
row["UNIT"] = (row["UNIT"] or '').strip()
Expand Down
31 changes: 30 additions & 1 deletion openaddr/tests/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
row_fxn_prefixed_number, row_fxn_postfixed_street,
row_fxn_postfixed_unit,
row_fxn_remove_prefix, row_fxn_remove_postfix, row_fxn_chain,
row_fxn_first_non_empty,
row_fxn_first_non_empty, row_fxn_constant,
row_canonicalize_unit_and_number, conform_smash_case, conform_cli,
convert_regexp_replace, normalize_ogr_filename_case,
is_in, geojson_source_to_csv, check_source_tests
Expand Down Expand Up @@ -1621,6 +1621,35 @@ 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_constant(self):
"constant - replacing empty string"
c = { "conform": {
"region": {
"function": "constant",
"value": "PA"
}
} }
d = { "STATE": "" }
e = copy.deepcopy(d)
e.update({ "oa:region": "PA" })

d = row_fxn_constant(c, d, "region", c["conform"]["region"])
self.assertEqual(e, d)

"constant - replacing non-empty, non-standard string"
c = { "conform": {
"region": {
"function": "constant",
"value": "PA"
}
} }
d = { "STATE": "Penna" }
e = copy.deepcopy(d)
e.update({ "oa:region": "PA" })

d = row_fxn_constant(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 4f363cc

Please sign in to comment.