preheat_open.query.build_instance_from_dict

preheat_open.query.build_instance_from_dict(cls, data, remove_keys=True)

Build an instance of a dataclass from a dictionary.

This function creates an instance of the specified class using values from the provided dictionary. It only uses keys that match the class constructor parameters and optionally removes used keys from the dictionary.

Parameters:
  • cls (Type[T]) – The class to build an instance of

  • data (dict) – The dictionary containing constructor parameters

  • remove_keys (bool) – Whether to remove used keys from the dictionary

Returns:

Tuple of the created instance and remaining dictionary

Return type:

tuple[T, dict]

Example:
>>> from dataclasses import dataclass
>>>
>>> @dataclass
... class Person:
...     name: str
...     age: int
>>>
>>> data = {"name": "Alice", "age": 30, "city": "NYC"}
>>> person, remaining = build_instance_from_dict(Person, data.copy())
>>> person.name
'Alice'
>>> person.age
30
>>> remaining
{'city': 'NYC'}