Skip to content

Commit

Permalink
temporal: SIM115: Use context manager for file I/O in t.rast (#5105)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arohanajit authored Feb 22, 2025
1 parent cbd53a5 commit 664e689
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,6 @@ ignore = [
"scripts/v.*/v.*.py" = ["SIM115"]
"scripts/wxpyimgview/wxpyimgview_gui.py" = ["SIM115"]
"temporal/t.rast.algebra/testsu*/*_algebra_arithmetic.py" = ["FLY002"]
"temporal/t.rast.colors/t.rast.colors.py" = ["SIM115"]
"temporal/t.rast.series/t.rast.series.py" = ["SIM115"]
"temporal/t.rast.what/t.rast.what.py" = ["SIM115"]
"temporal/t.register/testsu*/*_raster_different_local.py" = ["FLY002"]
"temporal/t.register/testsu*/*_raster_mapmetadata.py" = ["FLY002"]
"temporal/t.register/testsuite/test_t_register_raster.py" = ["FLY002"]
Expand Down
15 changes: 5 additions & 10 deletions temporal/t.rast.colors/t.rast.colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@

import grass.script as gs
from grass.exceptions import CalledModuleError
from pathlib import Path

############################################################################

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

if rows:
# Create the r.colors input file
filename = gs.tempfile(True)
file = open(filename, "w")

for row in rows:
string = "%s\n" % (row["id"])
file.write(string)

file.close()

flags_ = ""
if remove:
flags_ += "r"
Expand All @@ -172,6 +163,10 @@ def main():
if equi:
flags_ += "e"

# Create the r.colors input file
filename = gs.tempfile(True)
Path(filename).write_text("\n".join(str(row["id"]) for row in rows))

try:
gs.run_command(
"r.colors",
Expand Down
9 changes: 2 additions & 7 deletions temporal/t.rast.series/t.rast.series.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@

import grass.script as gs
from grass.exceptions import CalledModuleError
from pathlib import Path

############################################################################

Expand Down Expand Up @@ -136,13 +137,7 @@ def main():
if rows:
# Create the r.series input file
filename = gs.tempfile(True)
file = open(filename, "w")

for row in rows:
string = "%s\n" % (row["id"])
file.write(string)

file.close()
Path(filename).write_text("\n".join(str(row["id"]) for row in rows))

flag = ""
if len(rows) > max_files_open:
Expand Down

0 comments on commit 664e689

Please sign in to comment.