dlite icon indicating copy to clipboard operation
dlite copied to clipboard

Instantiate properties from dict

Open jesper-friis opened this issue 3 years ago • 0 comments

Make it possible to instantiate properties from dict. An important use case is to make it easier to work with dicts, dataclasses and pydantic datamodels. We should make it easy to convert to/from nested dicts and instances with ref-type properties.

This could be exposed via:

  • A dlite.Instance.set_dict(dict_obj) method that sets properties of an instance from a dict.
  • A dlite.Instance.get_dict() method that returns the properties as a dict.
  • Instantiation of Metadata with ref-type properties from nested dicts, like
    Entity = dlite.get_instance("http://onto-ns.com/meta/0.1/Entity")
    Entity(dimensions={...}, properties={"ref_prop": {...}, "other_prop": True, ...})
    

Use this to improve the support for dataclasses and pydantic datamodels in utils.py (maybe a separate issue).


At C-level we could support this by adding a functions:

int dlite_swig_set_dict(DLiteInstance *inst, obj_t *dict_obj);

This function could be exposed directly as the set_dict() method, but also used by dlite_swig_set_scalar() to support metadata instantiation with ref-type properties from nested dicts.

The get_dict() method could probably be implemented directly in Python.

jesper-friis avatar Mar 25 '23 12:03 jesper-friis