duck.utils.object_mappingยถ

Module contains a function to map keys and values to an object.

Module Contentsยถ

Functionsยถ

map_data_to_object

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