duck.utils.object_mapping¶
Module contains a function to map keys and values to an object.
Module Contents¶
Functions¶
Map key-value pairs from a dictionary to an object’s attributes. |
API¶
- duck.utils.object_mapping.map_data_to_object(obj, data: dict)[source]¶
Map key-value pairs from a dictionary to an object’s attributes.
This function takes a dictionary and maps its key-value pairs as attributes of the given object. Each key in the dictionary becomes an attribute of the object with the corresponding value.
- Parameters:
obj – The object to which attributes will be added.
data – A dictionary containing key-value pairs to be mapped to the object.
- Raises:
TypeError – If the provided data is not a dictionary.
Example:
class Example: pass e = Example() map_data_to_object(e, {'name': 'Alice', 'age': 30}) print(e.name) # Output: Alice print(e.age) # Output: 30