Skip to content

Commit e911306

Browse files
JessicaS11Illviljanheadtr1ckpre-commit-ci[bot]dcherian
authored
list available backends and basic descriptors (#7000)
Co-authored-by: Illviljan <[email protected]> Co-authored-by: Mick <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Deepak Cherian <[email protected]> Co-authored-by: dcherian <[email protected]>
1 parent 15c6836 commit e911306

File tree

6 files changed

+90
-27
lines changed

6 files changed

+90
-27
lines changed

doc/api.rst

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -592,60 +592,60 @@ Dataset methods
592592
.. autosummary::
593593
:toctree: generated/
594594

595-
open_dataset
596595
load_dataset
596+
open_dataset
597597
open_mfdataset
598598
open_rasterio
599599
open_zarr
600-
Dataset.to_netcdf
601-
Dataset.to_pandas
602-
Dataset.as_numpy
603-
Dataset.to_zarr
604600
save_mfdataset
601+
Dataset.as_numpy
602+
Dataset.from_dataframe
603+
Dataset.from_dict
605604
Dataset.to_array
606605
Dataset.to_dataframe
607606
Dataset.to_dask_dataframe
608607
Dataset.to_dict
609-
Dataset.from_dataframe
610-
Dataset.from_dict
608+
Dataset.to_netcdf
609+
Dataset.to_pandas
610+
Dataset.to_zarr
611+
Dataset.chunk
611612
Dataset.close
612613
Dataset.compute
613-
Dataset.persist
614-
Dataset.load
615-
Dataset.chunk
616-
Dataset.unify_chunks
617614
Dataset.filter_by_attrs
618615
Dataset.info
616+
Dataset.load
617+
Dataset.persist
618+
Dataset.unify_chunks
619619

620620
DataArray methods
621621
-----------------
622622

623623
.. autosummary::
624624
:toctree: generated/
625625

626-
open_dataarray
627626
load_dataarray
627+
open_dataarray
628+
DataArray.as_numpy
629+
DataArray.from_cdms2
630+
DataArray.from_dict
631+
DataArray.from_iris
632+
DataArray.from_series
633+
DataArray.to_cdms2
634+
DataArray.to_dataframe
628635
DataArray.to_dataset
636+
DataArray.to_dict
637+
DataArray.to_index
638+
DataArray.to_iris
639+
DataArray.to_masked_array
629640
DataArray.to_netcdf
641+
DataArray.to_numpy
630642
DataArray.to_pandas
631643
DataArray.to_series
632-
DataArray.to_dataframe
633-
DataArray.to_numpy
634-
DataArray.as_numpy
635-
DataArray.to_index
636-
DataArray.to_masked_array
637-
DataArray.to_cdms2
638-
DataArray.to_iris
639-
DataArray.from_iris
640-
DataArray.to_dict
641-
DataArray.from_series
642-
DataArray.from_cdms2
643-
DataArray.from_dict
644+
DataArray.chunk
644645
DataArray.close
645646
DataArray.compute
646647
DataArray.persist
647648
DataArray.load
648-
DataArray.chunk
649649
DataArray.unify_chunks
650650

651651
Coordinates objects
@@ -1093,6 +1093,7 @@ Advanced API
10931093
Dataset.set_close
10941094
backends.BackendArray
10951095
backends.BackendEntrypoint
1096+
backends.list_engines
10961097

10971098
Default, pandas-backed indexes built-in Xarray:
10981099

doc/internals/how-to-add-new-backend.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ If you also want to support lazy loading and dask see :ref:`RST lazy_loading`.
1616
Note that the new interface for backends is available from Xarray
1717
version >= 0.18 onwards.
1818

19+
You can see what backends are currently available in your working environment
20+
with :py:class:`~xarray.backends.list_engines()`.
21+
1922
.. _RST backend_entrypoint:
2023

2124
BackendEntrypoint subclassing
@@ -26,7 +29,9 @@ it should implement the following attributes and methods:
2629

2730
- the ``open_dataset`` method (mandatory)
2831
- the ``open_dataset_parameters`` attribute (optional)
29-
- the ``guess_can_open`` method (optional).
32+
- the ``guess_can_open`` method (optional)
33+
- the ``description`` attribute (optional)
34+
- the ``url`` attribute (optional).
3035

3136
This is what a ``BackendEntrypoint`` subclass should look like:
3237

@@ -55,6 +60,10 @@ This is what a ``BackendEntrypoint`` subclass should look like:
5560
return False
5661
return ext in {".my_format", ".my_fmt"}
5762
63+
description = "Use .my_format files in Xarray"
64+
65+
url = "https://link_to/your_backend/documentation"
66+
5867
``BackendEntrypoint`` subclass methods and attributes are detailed in the following.
5968

6069
.. _RST open_dataset:
@@ -168,6 +177,17 @@ that always returns ``False``.
168177
Backend ``guess_can_open`` takes as input the ``filename_or_obj`` parameter of
169178
Xarray :py:meth:`~xarray.open_dataset`, and returns a boolean.
170179

180+
.. _RST properties:
181+
182+
description and url
183+
^^^^^^^^^^^^^^^^^^^^
184+
185+
``description`` is used to provide a short text description of the backend.
186+
``url`` is used to include a link to the backend's documentation or code.
187+
188+
These attributes are surfaced when a user prints :py:class:`~xarray.backends.BackendEntrypoint`.
189+
If ``description`` or ``url`` are not defined, an empty string is returned.
190+
171191
.. _RST decoders:
172192

173193
Decoders

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Bug fixes
4747
Documentation
4848
~~~~~~~~~~~~~
4949

50+
- Improves overall documentation around available backends, including adding docstrings for :py:func:`xarray.backends.list_engines`
51+
Add :py:meth:`__str__` to surface the new :py:class:`BackendEntrypoint` ``description``
52+
and ``url`` attributes. (:issue:`6577`, :pull:`7000`)
53+
By `Jessica Scheick <https://github.com/jessicas11>`_.
54+
5055

5156
Internal Changes
5257
~~~~~~~~~~~~~~~~

xarray/backends/common.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,34 @@ class BackendEntrypoint:
372372
- ``guess_can_open`` method: it shall return ``True`` if the backend is able to open
373373
``filename_or_obj``, ``False`` otherwise. The implementation of this
374374
method is not mandatory.
375+
376+
Attributes
377+
----------
378+
379+
open_dataset_parameters : tuple, default None
380+
A list of ``open_dataset`` method parameters.
381+
The setting of this attribute is not mandatory.
382+
description : str
383+
A short string describing the engine.
384+
The setting of this attribute is not mandatory.
385+
url : str
386+
A string with the URL to the backend's documentation.
387+
The setting of this attribute is not mandatory.
375388
"""
376389

377390
available: ClassVar[bool] = True
378391

379392
open_dataset_parameters: tuple | None = None
380-
"""list of ``open_dataset`` method parameters"""
393+
description: str = ""
394+
url: str = ""
395+
396+
def __repr__(self) -> str:
397+
txt = f"<{type(self).__name__}>"
398+
if self.description:
399+
txt += f"\n {self.description}"
400+
if self.url:
401+
txt += f"\n Learn more at {self.url}"
402+
return txt
381403

382404
def open_dataset(
383405
self,

xarray/backends/plugins.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@ def build_engines(entrypoints) -> dict[str, BackendEntrypoint]:
105105

106106
@functools.lru_cache(maxsize=1)
107107
def list_engines() -> dict[str, BackendEntrypoint]:
108+
"""
109+
Return a dictionary of available engines and their BackendEntrypoint objects.
110+
111+
Returns
112+
-------
113+
dictionary
114+
115+
Notes
116+
-----
117+
This function lives in the backends namespace (``engs=xr.backends.list_engines()``).
118+
If available, more information is available about each backend via ``engs["eng_name"]``.
119+
108120
# New selection mechanism introduced with Python 3.10. See GH6514.
121+
"""
109122
if sys.version_info >= (3, 10):
110123
entrypoints = entry_points(group="xarray.backends")
111124
else:

xarray/core/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
if TYPE_CHECKING:
2121
from numpy.typing import ArrayLike
2222

23+
from ..backends.common import BackendEntrypoint
2324
from .common import AbstractArray, DataWithCoords
2425
from .dataarray import DataArray
2526
from .dataset import Dataset
@@ -85,6 +86,7 @@ def dtype(self) -> np.dtype:
8586
DTypeLikeSave: Any = None
8687

8788

89+
T_Backend = TypeVar("T_Backend", bound="BackendEntrypoint")
8890
T_Dataset = TypeVar("T_Dataset", bound="Dataset")
8991
T_DataArray = TypeVar("T_DataArray", bound="DataArray")
9092
T_Variable = TypeVar("T_Variable", bound="Variable")

0 commit comments

Comments
 (0)