Skip to content

Commit d25a851

Browse files
authored
refactor: use PathLike and curdir concisely and consistently (#2589)
This refactor is mostly related to PathLike and curdir from the os module. - Import these items to use as (e.g.) PathLike or os.PathLike - Try to make docstrings related to these more consistent - Replace typing Optional[Union[...]] with Union[..., None]
1 parent 291fa8e commit d25a851

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+367
-352
lines changed

.docs/Notebooks/groundwater2023_watershed_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
#
2323

2424
import os
25-
import pathlib as pl
2625
import sys
26+
from pathlib import Path
2727

2828
import git
2929
import matplotlib as mpl
@@ -111,11 +111,11 @@ def set_idomain(grid, boundary):
111111
# Check if we are in the repository and define the data path.
112112

113113
try:
114-
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
114+
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
115115
except:
116116
root = None
117117

118-
data_path = root / "examples" / "data" if root else pl.Path.cwd()
118+
data_path = root / "examples" / "data" if root else Path.cwd()
119119
folder_name = "groundwater2023"
120120
fname = "geometries.yml"
121121
pooch.retrieve(

.docs/Notebooks/mf6_support_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
# - `sim_ws`: `os.curdir` ('.')
4747
# - `sim_tdis_file`: 'modflow6.tdis'
4848
#
49-
# The `sim_ws` parameter accepts `str` or `os.PathLike` arguments.
49+
# The `sim_ws` parameter accepts `str` or `PathLike` arguments.
5050

5151
# +
5252
import os
@@ -495,7 +495,7 @@
495495
model.dis.export(pth / "dis.nc")
496496

497497
# export the botm array to a shapefile
498-
model.dis.botm.export(str(pth / "botm.shp")) # supports os.PathLike or str
498+
model.dis.botm.export(pth / "botm.shp") # supports PathLike or str
499499
# -
500500

501501
# ## Loading an Existing MF6 Simulation

flopy/discretization/grid.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import warnings
55
from collections import defaultdict
6+
from os import PathLike
67

78
import numpy as np
89

@@ -70,7 +71,7 @@ class Grid:
7071
The value can be anything accepted by
7172
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
7273
such as an authority string (eg "EPSG:26916") or a WKT string.
73-
prjfile : str or pathlike, optional if `crs` is specified
74+
prjfile : str or PathLike, optional if `crs` is specified
7475
ESRI-style projection file with well-known text defining the CRS
7576
for the model grid (must be projected; geographic CRS are not supported).
7677
xoff : float
@@ -87,7 +88,7 @@ class Grid:
8788
.. deprecated:: 3.5
8889
The following keyword options will be removed for FloPy 3.6:
8990
90-
- ``prj`` (str or pathlike): use ``prjfile`` instead.
91+
- ``prj`` (str or PathLike): use ``prjfile`` instead.
9192
- ``epsg`` (int): use ``crs`` instead.
9293
- ``proj4`` (str): use ``crs`` instead.
9394
@@ -381,7 +382,7 @@ def prjfile(self, prjfile):
381382
if prjfile is None:
382383
self._prjfile = None
383384
return
384-
if not isinstance(prjfile, (str, os.PathLike)):
385+
if not isinstance(prjfile, (str, PathLike)):
385386
raise ValueError("prjfile property must be str, PathLike or None")
386387
self._prjfile = prjfile
387388
# If crs was previously unset, use .prj file input
@@ -997,7 +998,7 @@ def set_coord_info(
997998
The value can be anything accepted by
998999
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
9991000
such as an authority string (eg "EPSG:26916") or a WKT string.
1000-
prjfile : str or pathlike, optional
1001+
prjfile : str or PathLike, optional
10011002
ESRI-style projection file with well-known text defining the CRS
10021003
for the model grid (must be projected; geographic CRS are not
10031004
supported).

flopy/discretization/structuredgrid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import os.path
3+
from os import PathLike
34
from typing import Union
45

56
import numpy as np
@@ -102,7 +103,7 @@ class for a structured model grid
102103
The value can be anything accepted by
103104
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
104105
such as an authority string (eg "EPSG:26916") or a WKT string.
105-
prjfile : str or pathlike, optional if `crs` is specified
106+
prjfile : str or PathLike, optional if `crs` is specified
106107
ESRI-style projection file with well-known text defining the CRS
107108
for the model grid (must be projected; geographic CRS are not supported).
108109
xoff : float
@@ -119,7 +120,7 @@ class for a structured model grid
119120
.. deprecated:: 3.5
120121
The following keyword options will be removed for FloPy 3.6:
121122
122-
- ``prj`` (str or pathlike): use ``prjfile`` instead.
123+
- ``prj`` (str or PathLike): use ``prjfile`` instead.
123124
- ``epsg`` (int): use ``crs`` instead.
124125
- ``proj4`` (str): use ``crs`` instead.
125126
@@ -1779,7 +1780,7 @@ def from_binary_grid_file(cls, file_path, verbose=False):
17791780
)
17801781

17811782
@classmethod
1782-
def from_gridspec(cls, file_path: Union[str, os.PathLike], lenuni=0):
1783+
def from_gridspec(cls, file_path: Union[str, PathLike], lenuni=0):
17831784
"""
17841785
Instantiate a StructuredGrid from grid specification file.
17851786

flopy/discretization/unstructuredgrid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import os
3+
from os import PathLike
34
from typing import Union
45

56
import numpy as np
@@ -56,7 +57,7 @@ class UnstructuredGrid(Grid):
5657
The value can be anything accepted by
5758
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
5859
such as an authority string (eg "EPSG:26916") or a WKT string.
59-
prjfile : str or pathlike, optional if `crs` is specified
60+
prjfile : str or PathLike, optional if `crs` is specified
6061
ESRI-style projection file with well-known text defining the CRS
6162
for the model grid (must be projected; geographic CRS are not supported).
6263
beneath the top layer.
@@ -78,7 +79,7 @@ class UnstructuredGrid(Grid):
7879
.. deprecated:: 3.5
7980
The following keyword options will be removed for FloPy 3.6:
8081
81-
- ``prj`` (str or pathlike): use ``prjfile`` instead.
82+
- ``prj`` (str or PathLike): use ``prjfile`` instead.
8283
- ``epsg`` (int): use ``crs`` instead.
8384
- ``proj4`` (str): use ``crs`` instead.
8485
@@ -1118,7 +1119,7 @@ def from_binary_grid_file(cls, file_path, verbose=False):
11181119
)
11191120

11201121
@classmethod
1121-
def from_gridspec(cls, file_path: Union[str, os.PathLike]):
1122+
def from_gridspec(cls, file_path: Union[str, PathLike]):
11221123
"""
11231124
Create an UnstructuredGrid from a grid specification file.
11241125

flopy/discretization/vertexgrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class for a vertex model grid
3232
The value can be anything accepted by
3333
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
3434
such as an authority string (eg "EPSG:26916") or a WKT string.
35-
prjfile : str or pathlike, optional if `crs` is specified
35+
prjfile : str or PathLike, optional if `crs` is specified
3636
ESRI-style projection file with well-known text defining the CRS
3737
for the model grid (must be projected; geographic CRS are not supported).
3838
xoff : float
@@ -49,7 +49,7 @@ class for a vertex model grid
4949
.. deprecated:: 3.5
5050
The following keyword options will be removed for FloPy 3.6:
5151
52-
- ``prj`` (str or pathlike): use ``prjfile`` instead.
52+
- ``prj`` (str or PathLike): use ``prjfile`` instead.
5353
- ``epsg`` (int): use ``crs`` instead.
5454
- ``proj4`` (str): use ``crs`` instead.
5555

flopy/export/netcdf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import socket
66
import time
77
from datetime import datetime
8+
from os import PathLike
89
from pathlib import Path
910
from typing import Optional, Union
1011

@@ -150,7 +151,7 @@ class NetCdf:
150151

151152
def __init__(
152153
self,
153-
output_filename: Union[str, os.PathLike],
154+
output_filename: Union[str, PathLike],
154155
model,
155156
time_values=None,
156157
z_positive="up",
@@ -170,9 +171,9 @@ def __init__(
170171
self.logger = Logger(verbose)
171172
self.var_attr_dict = {}
172173
self.log = self.logger.log
173-
if os.path.exists(output_filename):
174+
if output_filename.exists():
174175
self.logger.warn(f"removing existing nc file: {output_filename}")
175-
os.remove(output_filename)
176+
output_filename.unlink()
176177
self.output_filename = output_filename
177178

178179
self.forgive = bool(forgive)

flopy/export/shapefile_utils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import shutil
1010
import sys
1111
import warnings
12+
from os import PathLike
1213
from pathlib import Path
1314
from typing import Optional, Union
1415
from warnings import warn
@@ -21,7 +22,7 @@
2122
from ..utils.crs import get_crs
2223

2324

24-
def write_gridlines_shapefile(filename: Union[str, os.PathLike], mg):
25+
def write_gridlines_shapefile(filename: Union[str, PathLike], mg):
2526
"""
2627
Write a polyline shapefile of the grid lines - a lightweight alternative
2728
to polygons.
@@ -59,12 +60,12 @@ def write_gridlines_shapefile(filename: Union[str, os.PathLike], mg):
5960

6061

6162
def write_grid_shapefile(
62-
path: Union[str, os.PathLike],
63+
path: Union[str, PathLike],
6364
mg,
6465
array_dict,
6566
nan_val=np.nan,
6667
crs=None,
67-
prjfile: Optional[Union[str, os.PathLike]] = None,
68+
prjfile: Union[str, PathLike, None] = None,
6869
verbose=False,
6970
**kwargs,
7071
):
@@ -87,7 +88,7 @@ def write_grid_shapefile(
8788
The value can be anything accepted by
8889
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
8990
such as an authority string (eg "EPSG:26916") or a WKT string.
90-
prjfile : str or pathlike, optional if `crs` is specified
91+
prjfile : str or PathLike, optional if `crs` is specified
9192
ESRI-style projection file with well-known text defining the CRS
9293
for the model grid (must be projected; geographic CRS are not supported).
9394
**kwargs : dict, optional
@@ -224,7 +225,7 @@ def write_grid_shapefile(
224225

225226

226227
def model_attributes_to_shapefile(
227-
path: Union[str, os.PathLike],
228+
path: Union[str, PathLike],
228229
ml,
229230
package_names=None,
230231
array_dict=None,
@@ -259,7 +260,7 @@ def model_attributes_to_shapefile(
259260
The value can be anything accepted by
260261
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
261262
such as an authority string (eg "EPSG:26916") or a WKT string.
262-
prjfile : str or pathlike, optional if `crs` is specified
263+
prjfile : str or PathLike, optional if `crs` is specified
263264
ESRI-style projection file with well-known text defining the CRS
264265
for the model grid (must be projected; geographic CRS are not supported).
265266
@@ -535,7 +536,7 @@ def get_pyshp_field_dtypes(code):
535536
return dtypes.get(code, object)
536537

537538

538-
def shp2recarray(shpname: Union[str, os.PathLike]):
539+
def shp2recarray(shpname: Union[str, PathLike]):
539540
"""Read a shapefile into a numpy recarray.
540541
541542
Parameters
@@ -566,10 +567,10 @@ def shp2recarray(shpname: Union[str, os.PathLike]):
566567
def recarray2shp(
567568
recarray,
568569
geoms,
569-
shpname: Union[str, os.PathLike] = "recarray.shp",
570+
shpname: Union[str, PathLike] = "recarray.shp",
570571
mg=None,
571572
crs=None,
572-
prjfile: Optional[Union[str, os.PathLike]] = None,
573+
prjfile: Union[str, PathLike, None] = None,
573574
verbose=False,
574575
**kwargs,
575576
):
@@ -599,7 +600,7 @@ def recarray2shp(
599600
The value can be anything accepted by
600601
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
601602
such as an authority string (eg "EPSG:26916") or a WKT string.
602-
prjfile : str or pathlike, optional if `crs` is specified
603+
prjfile : str or PathLike, optional if `crs` is specified
603604
ESRI-style projection file with well-known text defining the CRS
604605
for the model grid (must be projected; geographic CRS are not supported).
605606
**kwargs : dict, optional

0 commit comments

Comments
 (0)