itr
Iteration functions
itr
iterate
The elevaso_spine.itr.itr.iterate() function loops through collections (list, dict, tuple, set) and executes the provided function on non-collection data types. The elevaso_spine.itr.itr.iterate() function is used by elevaso_spine.fmt.sub.sub_value() to perform string replacements.
Another use case would be to convert data types, for example, if you want to change all integers into string with zero padding.
from elevaso_spine.itr import itr
def convert(val: int) -> str:
return val.zfill(4)
value = [
{
"name": "Test1",
"count": 1
},
{
"name": "Test2",
"count": 10
}
]
print(itr.iterate(val=value, custom_type_map={int: convert}))
The above could would iterate through the list of dictionary items and change the count to zero pad with 4 characters in total. The output would be:
[
{
"name": "Test1",
"count": "0001"
},
{
"name": "Test2",
"count": "0010",
}
]
- elevaso_spine.itr.itr.iterate(val: object, copy_val: bool = True, custom_type_map: dict = None) object[source]
Iterate a nested list, dict, tuple object to execute function on string values
- Args:
val (object): Object to iterate
copy_val (bool, Optional): True/False contents of dict/list should be copied prior to modifying, defaults to True
custom_type_map (dict, Optional): Optional mapping of type and functions, defaults to None
- Raises:
NotImplementedError: If value object type is not supported
Warning: If type of object does not support string substitution
- Returns:
object: Modified object after function calls