Skip to content

Commit e558834

Browse files
committed
Refactor virtualfile_from_data to remove if/elif/else block
Pure dictionary based lookup to convert data to a virtualfile.
1 parent ebd2264 commit e558834

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

pygmt/clib/session.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -1387,32 +1387,24 @@ def virtualfile_from_data(self, check_kind=None, data=None, x=None, y=None, z=No
13871387
"""
13881388
kind = data_kind(check_kind, data, x, y, z)
13891389

1390-
# Decide which virtualfile_from_ function to use
1391-
_virtualfile_from = {
1392-
"file": dummy_context,
1393-
"grid": self.virtualfile_from_grid,
1394-
# Note: virtualfile_from_matrix is not used because a matrix can be
1395-
# converted to vectors instead, and using vectors allows for better
1396-
# handling of string type inputs (e.g. for datetime data types)
1397-
"matrix": self.virtualfile_from_vectors,
1398-
"vectors": self.virtualfile_from_vectors,
1399-
}[kind]
1400-
1401-
# Ensure the data is an iterable (Python list or tuple)
14021390
if kind == "matrix": # turn 2D arrays into list of vectors
14031391
try:
14041392
# pandas.DataFrame and xarray.Dataset types
14051393
_data = [array for _, array in data.items()]
14061394
except AttributeError:
14071395
# Python lists, tuples, and numpy ndarray types
14081396
_data = np.atleast_2d(np.asanyarray(data).T)
1409-
elif kind == "vectors":
1410-
_data = (x, y, z)
1411-
else:
1412-
_data = (data,)
14131397

1414-
# Finally create the virtualfile from the data, to be passed into GMT
1415-
file_context = _virtualfile_from(*_data)
1398+
# Based on the data kind, create the virtualfile to be passed into GMT
1399+
file_context = {
1400+
"file": dummy_context(data),
1401+
"grid": self.virtualfile_from_grid(data),
1402+
# Note: virtualfile_from_matrix is not used because a matrix can be
1403+
# converted to vectors instead, and using vectors allows for better
1404+
# handling of string type inputs (e.g. for datetime data types)
1405+
"matrix": self.virtualfile_from_vectors(*_data),
1406+
"vectors": self.virtualfile_from_vectors(x, y, z),
1407+
}[kind]
14161408

14171409
return file_context
14181410

0 commit comments

Comments
 (0)