Skip to content

Commit 664e689

Browse files
authored
temporal: SIM115: Use context manager for file I/O in t.rast (#5105)
* added context managers * update pyproject.toml * add join to reduce code * added join to reduce code t.rast.series * ruff auto fix * rearranged with block * reverted back to gs.tmpfile
1 parent cbd53a5 commit 664e689

File tree

3 files changed

+7
-20
lines changed

3 files changed

+7
-20
lines changed

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,6 @@ ignore = [
376376
"scripts/v.*/v.*.py" = ["SIM115"]
377377
"scripts/wxpyimgview/wxpyimgview_gui.py" = ["SIM115"]
378378
"temporal/t.rast.algebra/testsu*/*_algebra_arithmetic.py" = ["FLY002"]
379-
"temporal/t.rast.colors/t.rast.colors.py" = ["SIM115"]
380-
"temporal/t.rast.series/t.rast.series.py" = ["SIM115"]
381-
"temporal/t.rast.what/t.rast.what.py" = ["SIM115"]
382379
"temporal/t.register/testsu*/*_raster_different_local.py" = ["FLY002"]
383380
"temporal/t.register/testsu*/*_raster_mapmetadata.py" = ["FLY002"]
384381
"temporal/t.register/testsuite/test_t_register_raster.py" = ["FLY002"]

temporal/t.rast.colors/t.rast.colors.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104

105105
import grass.script as gs
106106
from grass.exceptions import CalledModuleError
107+
from pathlib import Path
107108

108109
############################################################################
109110

@@ -146,16 +147,6 @@ def main():
146147
rows = sp.get_registered_maps("id", None, None, None)
147148

148149
if rows:
149-
# Create the r.colors input file
150-
filename = gs.tempfile(True)
151-
file = open(filename, "w")
152-
153-
for row in rows:
154-
string = "%s\n" % (row["id"])
155-
file.write(string)
156-
157-
file.close()
158-
159150
flags_ = ""
160151
if remove:
161152
flags_ += "r"
@@ -172,6 +163,10 @@ def main():
172163
if equi:
173164
flags_ += "e"
174165

166+
# Create the r.colors input file
167+
filename = gs.tempfile(True)
168+
Path(filename).write_text("\n".join(str(row["id"]) for row in rows))
169+
175170
try:
176171
gs.run_command(
177172
"r.colors",

temporal/t.rast.series/t.rast.series.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
import grass.script as gs
9696
from grass.exceptions import CalledModuleError
97+
from pathlib import Path
9798

9899
############################################################################
99100

@@ -136,13 +137,7 @@ def main():
136137
if rows:
137138
# Create the r.series input file
138139
filename = gs.tempfile(True)
139-
file = open(filename, "w")
140-
141-
for row in rows:
142-
string = "%s\n" % (row["id"])
143-
file.write(string)
144-
145-
file.close()
140+
Path(filename).write_text("\n".join(str(row["id"]) for row in rows))
146141

147142
flag = ""
148143
if len(rows) > max_files_open:

0 commit comments

Comments
 (0)