-
Notifications
You must be signed in to change notification settings - Fork 245
Add support for passing pathlib.Path objects as filenames #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
f159418
32654d3
2cea6da
3bfdb56
f91b750
367d6e9
9dc3fb7
b3c68ec
296be41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||||
| Utilities and common tasks for wrapping the GMT modules. | ||||||
| """ | ||||||
| import os | ||||||
| import pathlib | ||||||
| import shutil | ||||||
| import subprocess | ||||||
| import sys | ||||||
|
|
@@ -21,6 +22,7 @@ def data_kind(data, x=None, y=None, z=None): | |||||
| Possible types: | ||||||
|
|
||||||
| * a file name provided as 'data' | ||||||
| * a pathlib.Path provided as 'data' | ||||||
| * an xarray.DataArray provided as 'data' | ||||||
| * a matrix provided as 'data' | ||||||
| * 1D arrays x and y (and z, optionally) | ||||||
|
|
@@ -30,9 +32,9 @@ def data_kind(data, x=None, y=None, z=None): | |||||
|
|
||||||
| Parameters | ||||||
| ---------- | ||||||
| data : str or xarray.DataArray or {table-like} or None | ||||||
| Pass in either a file name to an ASCII data table, an | ||||||
| :class:`xarray.DataArray`, a 1D/2D | ||||||
| data : str or pathlib.Path or xarray.DataArray or {table-like} or None | ||||||
| Pass in either a file name or :class:`pathlib.Path` to an ASCII data | ||||||
| table, an :class:`xarray.DataArray`, a 1D/2D | ||||||
| {table-classes}. | ||||||
| x/y : 1d arrays or None | ||||||
| x and y columns as numpy arrays. | ||||||
|
|
@@ -50,12 +52,15 @@ def data_kind(data, x=None, y=None, z=None): | |||||
|
|
||||||
| >>> import numpy as np | ||||||
| >>> import xarray as xr | ||||||
| >>> import pathlib | ||||||
| >>> data_kind(data=None, x=np.array([1, 2, 3]), y=np.array([4, 5, 6])) | ||||||
| 'vectors' | ||||||
| >>> data_kind(data=np.arange(10).reshape((5, 2)), x=None, y=None) | ||||||
| 'matrix' | ||||||
| >>> data_kind(data="my-data-file.txt", x=None, y=None) | ||||||
| 'file' | ||||||
| >>> data_kind(data=pathlib.Path("my-data-file.txt"), x=None, y=None) | ||||||
| 'file' | ||||||
| >>> data_kind(data=xr.DataArray(np.random.rand(4, 3))) | ||||||
| 'grid' | ||||||
| """ | ||||||
|
|
@@ -66,7 +71,7 @@ def data_kind(data, x=None, y=None, z=None): | |||||
| if data is None and (x is None or y is None): | ||||||
| raise GMTInvalidInput("Must provided both x and y.") | ||||||
|
|
||||||
| if isinstance(data, str): | ||||||
| if isinstance(data, (str, pathlib.Path)): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will happen if people use
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The answer at https://stackoverflow.com/questions/58647584/how-to-test-if-object-is-a-pathlib-path/58966089#58966089 suggests using
Suggested change
Also, it might be a good idea to test that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gentle ping @aitorres to see if you're still available to finish up this PR! If not, we can get one of the maintainers to finish this up in the next week or so.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really sorry for the delay! I've just added and pushed the changes to use |
||||||
| kind = "file" | ||||||
| elif isinstance(data, xr.DataArray): | ||||||
| kind = "grid" | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.