Skip to content

Commit b1363c0

Browse files
committed
Fix PTH123 vilations
1 parent b2a207d commit b1363c0

File tree

13 files changed

+35
-48
lines changed

13 files changed

+35
-48
lines changed

examples/tutorials/basics/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
fig.coast(land="black", water="skyblue")
7373

7474
# Create space-delimited file
75-
with open("examples.txt", "w") as f:
75+
with Path("examples.txt").open(mode="w") as f:
7676
f.write("114 0.5 0 22p,Helvetica-Bold,white CM BORNEO\n")
7777
f.write("119 3.25 0 12p,Helvetica-Bold,black CM CELEBES SEA\n")
7878
f.write("112 -4.6 0 12p,Helvetica-Bold,black CM JAVA SEA\n")

pygmt/figure.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,7 @@ def _preview(self, fmt, dpi, as_bytes=False, **kwargs):
497497
fname = Path(self._preview_dir.name) / f"{self._name}.{fmt}"
498498
self.savefig(fname, dpi=dpi, **kwargs)
499499
if as_bytes:
500-
with open(fname, "rb") as image:
501-
preview = image.read()
502-
return preview
500+
return fname.read_bytes()
503501
return fname
504502

505503
def _repr_png_(self):

pygmt/helpers/tempfile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ def read(self, keep_tabs=False):
8888
content : str
8989
Content of the temporary file as a Unicode string.
9090
"""
91-
with open(self.name, encoding="utf8") as tmpfile:
92-
content = tmpfile.read()
93-
if not keep_tabs:
94-
content = content.replace("\t", " ")
95-
return content
91+
content = Path(self.name).read_text(encoding="utf8")
92+
if not keep_tabs:
93+
content = content.replace("\t", " ")
94+
return content
9695

9796
def loadtxt(self, **kwargs):
9897
"""

pygmt/src/plot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
plot - Plot in two dimensions.
33
"""
44

5+
from pathlib import Path
6+
57
from pygmt.clib import Session
68
from pygmt.exceptions import GMTInvalidInput
79
from pygmt.helpers import (
@@ -220,7 +222,7 @@ def plot( # noqa: PLR0912
220222
elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"):
221223
# checking that the data is a file path to set default style
222224
try:
223-
with open(which(data), encoding="utf8") as file:
225+
with Path(which(data)).open(encoding="utf8") as file:
224226
line = file.readline()
225227
if "@GMULTIPOINT" in line or "@GPOINT" in line:
226228
# if the file is gmt style and geometry is set to Point

pygmt/src/plot3d.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
plot3d - Plot in three dimensions.
33
"""
44

5+
from pathlib import Path
6+
57
from pygmt.clib import Session
68
from pygmt.exceptions import GMTInvalidInput
79
from pygmt.helpers import (
@@ -195,7 +197,7 @@ def plot3d( # noqa: PLR0912
195197
elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"):
196198
# checking that the data is a file path to set default style
197199
try:
198-
with open(which(data), encoding="utf8") as file:
200+
with Path(which(data)).open(encoding="utf8") as file:
199201
line = file.readline()
200202
if "@GMULTIPOINT" in line or "@GPOINT" in line:
201203
# if the file is gmt style and geometry is set to Point

pygmt/tests/test_helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ def test_gmttempfile_read():
132132
Make sure GMTTempFile.read() works.
133133
"""
134134
with GMTTempFile() as tmpfile:
135-
with open(tmpfile.name, "w", encoding="utf8") as ftmp:
136-
ftmp.write("in.dat: N = 2\t<1/3>\t<2/4>\n")
135+
Path(tmpfile.name).write_text("in.dat: N = 2\t<1/3>\t<2/4>\n")
137136
assert tmpfile.read() == "in.dat: N = 2 <1/3> <2/4>\n"
138137
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"
139138

pygmt/tests/test_legend.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Test Figure.legend.
33
"""
44

5+
from pathlib import Path
6+
57
import pytest
68
from pygmt import Figure
79
from pygmt.exceptions import GMTInvalidInput
@@ -95,8 +97,7 @@ def test_legend_specfile():
9597
"""
9698

9799
with GMTTempFile() as specfile:
98-
with open(specfile.name, "w", encoding="utf8") as file:
99-
file.write(specfile_contents)
100+
Path(specfile.name).write_text(specfile_contents)
100101
fig = Figure()
101102
fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True)
102103
fig.legend(specfile.name, position="JTM+jCM+w5i")

pygmt/tests/test_meca.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Test Figure.meca.
33
"""
44

5+
from pathlib import Path
6+
57
import numpy as np
68
import pandas as pd
79
import pytest
@@ -72,13 +74,8 @@ def test_meca_spec_single_focalmecha_file():
7274
fig = Figure()
7375
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
7476
with GMTTempFile() as temp:
75-
with open(temp.name, mode="w", encoding="utf8") as temp_file:
76-
temp_file.write("0 5 0 0 90 0 5")
77-
fig.meca(
78-
spec=temp.name,
79-
convention="aki",
80-
scale="2.5c",
81-
)
77+
Path(temp.name).write_text("0 5 0 0 90 0 5")
78+
fig.meca(spec=temp.name, convention="aki", scale="2.5c")
8279
return fig
8380

8481

pygmt/tests/test_plot.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ def test_plot_ogrgmt_file_multipoint_default_style(func):
487487
# FEATURE_DATA
488488
1 2
489489
"""
490-
with open(tmpfile.name, "w", encoding="utf8") as file:
491-
file.write(gmt_file)
490+
Path(tmpfile.name).write_text(gmt_file)
492491
fig = Figure()
493492
fig.plot(
494493
data=func(tmpfile.name), region=[0, 2, 1, 3], projection="X2c", frame=True
@@ -507,8 +506,7 @@ def test_plot_ogrgmt_file_multipoint_non_default_style():
507506
# FEATURE_DATA
508507
1 2
509508
"""
510-
with open(tmpfile.name, "w", encoding="utf8") as file:
511-
file.write(gmt_file)
509+
Path(tmpfile.name).write_text(gmt_file)
512510
fig = Figure()
513511
fig.plot(
514512
data=tmpfile.name,

pygmt/tests/test_plot3d.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,7 @@ def test_plot3d_ogrgmt_file_multipoint_default_style(func):
444444
>
445445
1 1 2
446446
1.5 1.5 1"""
447-
with open(tmpfile.name, "w", encoding="utf8") as file:
448-
file.write(gmt_file)
447+
Path(tmpfile.name).write_text(gmt_file)
449448
fig = Figure()
450449
fig.plot3d(
451450
data=func(tmpfile.name),
@@ -470,8 +469,7 @@ def test_plot3d_ogrgmt_file_multipoint_non_default_style():
470469
>
471470
1 1 2
472471
1.5 1.5 1"""
473-
with open(tmpfile.name, "w", encoding="utf8") as file:
474-
file.write(gmt_file)
472+
Path(tmpfile.name).write_text(gmt_file)
475473
fig = Figure()
476474
fig.plot3d(
477475
data=tmpfile.name,

0 commit comments

Comments
 (0)