Skip to content

Allow .attrs to use dict-likes #5655

Open
@Illviljan

Description

@Illviljan

Is your feature request related to a problem? Please describe.
Reading attributes from h5py-files is rather slow. So instead of retrieving it immediately I wanted to create a lazy dict-class that only retrieves the attribute values when necessary. But this is difficult to achieve since xarray keeps forcing the attrs to dicts in a lot of places.

Describe the solution you'd like

  • Replace in
    self._attrs = dict(value)
    and
    self._attrs = dict(value)
    with a asdict(value) function that checks if the input is a valid dict-like, if not convert to dict. Things that might be good to check:
    • MutableMapping
    • hasattr(dict_like, "copy")
    • isinstance(dict_like, dict) == True
  • Remove unneccessary conversions to dict. For example
    return dict(variable_attrs[0])
    should not be necessary as attrs from variables/dataarrays/datasets have already been forced to dicts when they were initialized.

Describe alternatives you've considered

  • One could lazify with dicts as well, for example by replacing the value with a function. This however won't look good in reprs, that's why having a convienence class is nice.
  • dict(LazyDict) always forces to dict, it does not let it pass through unchanged even if isinstance(LazyDict, dict) == True.

Interesting reading:
https://stackoverflow.com/questions/16669367/setup-dictionary-lazily
https://stackoverflow.com/questions/3387691/how-to-perfectly-override-a-dict

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-metadataRelating to the handling of metadata (i.e. attrs and encoding)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions