1010Record = 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
1718def 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
3031def 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