Skip to content

Commit 2c68702

Browse files
chg ! tests
1 parent 4b4475a commit 2c68702

File tree

4 files changed

+466
-213
lines changed

4 files changed

+466
-213
lines changed

src/country_workspace/contrib/hope/push.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from country_workspace.exceptions import RemoteError
1414
from country_workspace.models import AsyncJob
1515
from country_workspace.workspaces.models import CountryHousehold, CountryIndividual
16+
from country_workspace.utils.fields import map_fields
1617

1718

1819
@dataclass
@@ -225,20 +226,3 @@ def steps() -> Iterator[Callable[[], None]]:
225226
if processor.total["errors"]:
226227
return processor.total
227228
return processor.total
228-
229-
230-
def map_fields(fields: dict[str, str]) -> dict[str, str]:
231-
"""
232-
Map keys in a dictionary to alternative names based on a predefined mapping.
233-
234-
Args:
235-
fields (dict[str, str]): A dictionary containing field names as keys and their values.
236-
237-
Returns:
238-
dict[str, str]: A new dictionary with keys mapped according to the predefined mapping.
239-
240-
"""
241-
to_map = {
242-
"gender": "sex",
243-
}
244-
return {to_map.get(k, k): v for k, v in fields.items()}

src/country_workspace/utils/fields.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
Record = Mapping[str, Any]
1111

1212

13-
TO_REMOVE = "_h_c", "_h_f", "_i_c", "_i_f"
14-
TO_UPPERCASE = "relationship", "gender", "disability", "residence_status"
13+
TO_REMOVE_VALUES = "_h_c", "_h_f", "_i_c", "_i_f"
14+
TO_UPPERCASE_FIELDS = "relationship", "gender", "disability", "residence_status"
15+
TO_MAP_FIELDS = {"gender": "sex"}
1516

1617

1718
def clean_field_name(v: str) -> str:
@@ -24,7 +25,7 @@ def clean_field_name(v: str) -> str:
2425
str: The cleaned field name.
2526
2627
"""
27-
return reduce(lambda name, substr: name.replace(substr, ""), TO_REMOVE, v.lower())
28+
return reduce(lambda name, substr: name.replace(substr, ""), TO_REMOVE_VALUES, v.lower())
2829

2930

3031
def clean_field_names(record: Record) -> Record:
@@ -52,4 +53,18 @@ def uppercase_field_value(k: str, v: Any) -> str:
5253
str: The uppercase value if applicable or the original value.
5354
5455
"""
55-
return v.upper() if isinstance(v, str) and any(k.startswith(prefix) for prefix in TO_UPPERCASE) else v
56+
return v.upper() if isinstance(v, str) and any(k.startswith(prefix) for prefix in TO_UPPERCASE_FIELDS) else v
57+
58+
59+
def map_fields(fields: dict[str, str]) -> dict[str, str]:
60+
"""
61+
Map keys in a dictionary to alternative names based on a predefined mapping.
62+
63+
Args:
64+
fields (dict[str, str]): A dictionary containing field names as keys and their values.
65+
66+
Returns:
67+
dict[str, str]: A new dictionary with keys mapped according to the predefined mapping.
68+
69+
"""
70+
return {TO_MAP_FIELDS.get(k, k): v for k, v in fields.items()}

0 commit comments

Comments
 (0)