-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
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 and
xarray/xarray/core/variable.py
Line 865 in dddac11
self._attrs = dict(value) with aLine 798 in dddac11
self._attrs = dict(value) 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:MutableMappinghasattr(dict_like, "copy")isinstance(dict_like, dict) == True
- Remove unneccessary conversions to dict. For example should not be necessary as attrs from variables/dataarrays/datasets have already been forced to dicts when they were initialized.
Line 523 in dddac11
return dict(variable_attrs[0])
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 ifisinstance(LazyDict, dict) == True.
Interesting reading:
https://stackoverflow.com/questions/16669367/setup-dictionary-lazily
https://stackoverflow.com/questions/3387691/how-to-perfectly-override-a-dict