Allow .attrs to use dict-likes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
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 https://github.com/pydata/xarray/blob/dddac11b01330791ffab4dfc72d226e71821973e/xarray/core/variable.py#L865 and https://github.com/pydata/xarray/blob/dddac11b01330791ffab4dfc72d226e71821973e/xarray/core/dataset.py#L798 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:MutableMappinghasattr(dict_like, "copy")isinstance(dict_like, dict) == True
- Remove unneccessary conversions to dict. For example https://github.com/pydata/xarray/blob/dddac11b01330791ffab4dfc72d226e71821973e/xarray/core/merge.py#L523 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 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with xarray/core/variable.py around line 865, xarray/core/dataset.py around line 798, and xarray/core/merge.py around line 523. Review how attrs are converted and determine the dict-like behavior described in the issue; done means supported lazy dict-like attrs are preserved where appropriate and unnecessary conversions are removed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100