Skip to content

Commit 0c82b3c

Browse files
committed
data_kind: Refactor the if-else statements into if-return statements
1 parent 68a17a0 commit 0c82b3c

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

pygmt/helpers/utils.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,26 @@ def _check_encoding(
187187
return "ISOLatin1+"
188188

189189

190-
def data_kind(
190+
def data_kind( # noqa: PLR0911
191191
data: Any = None, required: bool = True
192192
) -> Literal[
193193
"arg", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors"
194194
]:
195195
r"""
196196
Check the kind of data that is provided to a module.
197197
198-
The ``data`` argument can be in any type, but only following types are supported:
198+
The ``data`` argument can be in any type. Following data kinds are recognized:
199199
200-
- a string or a :class:`pathlib.PurePath` object or a sequence of them, representing
201-
a file name or a list of file names
202-
- a 2-D or 3-D :class:`xarray.DataArray` object
203-
- a 2-D matrix
204-
- None, bool, int or float type representing an optional arguments
205-
- a geo-like Python object that implements ``__geo_interface__`` (e.g.,
206-
geopandas.GeoDataFrame or shapely.geometry)
200+
- ``"arg"``: data is ``None`` and ``required=False``, or bool, int, float,
201+
representing an optional argument, used for dealing with optional virtual files
202+
- ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
203+
representing one or more file names
204+
- ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
205+
(e.g., geopandas.GeoDataFrame or shapely.geometry)
206+
- ``"grid"``: a :class:`xarray.DataArray` object that is not 3-D
207+
- ``"image"``: a 3-D :class:`xarray.DataArray` object
208+
- ``"matrix"``: anything that is not None
209+
- ``"vectors"``: data is ``None`` and ``required=True``
207210
208211
Parameters
209212
----------
@@ -287,30 +290,36 @@ def data_kind(
287290
>>> data_kind(data=None)
288291
'vectors'
289292
"""
290-
kind: Literal[
291-
"arg", "file", "geojson", "grid", "image", "matrix", "stringio", "vectors"
292-
]
293+
# One file or a list/tuple of files.
293294
if isinstance(data, str | pathlib.PurePath) or (
294295
isinstance(data, list | tuple)
295296
and all(isinstance(_file, str | pathlib.PurePath) for _file in data)
296297
):
297-
# One or more files
298-
kind = "file"
299-
elif isinstance(data, bool | int | float) or (data is None and not required):
300-
kind = "arg"
301-
elif isinstance(data, io.StringIO):
302-
kind = "stringio"
303-
elif isinstance(data, xr.DataArray):
304-
kind = "image" if len(data.dims) == 3 else "grid"
305-
elif hasattr(data, "__geo_interface__"):
306-
# geo-like Python object that implements ``__geo_interface__``
307-
# (geopandas.GeoDataFrame or shapely.geometry)
308-
kind = "geojson"
309-
elif data is not None:
310-
kind = "matrix"
311-
else:
312-
kind = "vectors"
313-
return kind
298+
return "file"
299+
300+
# A StringIO object.
301+
if isinstance(data, io.StringIO):
302+
return "stringio"
303+
304+
# An option argument, mainly for dealing optional virtual files.
305+
if isinstance(data, bool | int | float) or (data is None and not required):
306+
return "arg"
307+
308+
# An xarray.DataArray object, representing a grid or an image.
309+
if isinstance(data, xr.DataArray):
310+
return "image" if len(data.dims) == 3 else "grid"
311+
312+
# Geo-like Python object that implements ``__geo_interface__`` (e.g.,
313+
# geopandas.GeoDataFrame or shapely.geometry).
314+
# Reference: https://gist.github.com/sgillies/2217756
315+
if hasattr(data, "__geo_interface__"):
316+
return "geojson"
317+
318+
# Any not-None is considered as a matrix.
319+
if data is not None:
320+
return "matrix"
321+
322+
return "vectors"
314323

315324

316325
def non_ascii_to_octal(

0 commit comments

Comments
 (0)