From 664e6891f5b59e6b518232bc33f49418e9958085 Mon Sep 17 00:00:00 2001 From: Arohan Ajit Date: Sat, 22 Feb 2025 12:20:43 -0500 Subject: [PATCH] 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 --- pyproject.toml | 3 --- temporal/t.rast.colors/t.rast.colors.py | 15 +++++---------- temporal/t.rast.series/t.rast.series.py | 9 ++------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 11155e233e3..d653ef994d9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/temporal/t.rast.colors/t.rast.colors.py b/temporal/t.rast.colors/t.rast.colors.py index 185ba5a5a29..3cc064dff3a 100755 --- a/temporal/t.rast.colors/t.rast.colors.py +++ b/temporal/t.rast.colors/t.rast.colors.py @@ -104,6 +104,7 @@ import grass.script as gs from grass.exceptions import CalledModuleError +from pathlib import Path ############################################################################ @@ -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" @@ -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", diff --git a/temporal/t.rast.series/t.rast.series.py b/temporal/t.rast.series/t.rast.series.py index 57907f463e4..cb0f0059e8c 100755 --- a/temporal/t.rast.series/t.rast.series.py +++ b/temporal/t.rast.series/t.rast.series.py @@ -94,6 +94,7 @@ import grass.script as gs from grass.exceptions import CalledModuleError +from pathlib import Path ############################################################################ @@ -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: