From 261769df81c588edbbc51e9bf561150eaa7a62ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 24 Aug 2024 00:53:20 +0000 Subject: [PATCH 1/8] grass.script.db: Close opened file in db_connection --- python/grass/script/db.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/python/grass/script/db.py b/python/grass/script/db.py index 2725ff06ebe..43d9e990681 100644 --- a/python/grass/script/db.py +++ b/python/grass/script/db.py @@ -127,9 +127,8 @@ def db_connection(force=False, env=None): :return: parsed output of db.connect """ # noqa: E501 try: - nuldev = open(os.devnull, "w") - conn = parse_command("db.connect", flags="g", stderr=nuldev, env=env) - nuldev.close() + with open(os.devnull, "w") as nuldev: + conn = parse_command("db.connect", flags="g", stderr=nuldev, env=env) except CalledModuleError: conn = None From 1ca0c190b36f3c18f359b9d9ed4e930f7713e8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 24 Aug 2024 16:24:57 +0000 Subject: [PATCH 2/8] t.unregister: Use a context manager for opening files (SIM115) --- temporal/t.unregister/t.unregister.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/temporal/t.unregister/t.unregister.py b/temporal/t.unregister/t.unregister.py index e7e234320c0..cba596d22a1 100755 --- a/temporal/t.unregister/t.unregister.py +++ b/temporal/t.unregister/t.unregister.py @@ -108,17 +108,16 @@ def main(): # Read the map list from file if file: - fd = open(file, "r") - - line = True - while True: - line = fd.readline() - if not line: - break - - mapname = line.strip() - mapid = dummy.build_id(mapname, mapset) - maplist.append(mapid) + with open(file) as fd: + line = True + while True: + line = fd.readline() + if not line: + break + + mapname = line.strip() + mapid = dummy.build_id(mapname, mapset) + maplist.append(mapid) num_maps = len(maplist) update_dict = {} From 7a98608c77b2307dfd86b92d237bbc48d6930f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 24 Aug 2024 18:14:13 +0000 Subject: [PATCH 3/8] t.remove: Use a context manager for opening files (SIM115) --- temporal/t.remove/t.remove.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/temporal/t.remove/t.remove.py b/temporal/t.remove/t.remove.py index fd32f8972d5..9bf1203a8d1 100755 --- a/temporal/t.remove/t.remove.py +++ b/temporal/t.remove/t.remove.py @@ -100,19 +100,17 @@ def main(): else: dataset_list = tuple(datasets.split(",")) - # Read the dataset list from file if file: - fd = open(file, "r") - - line = True - while True: - line = fd.readline() - if not line: - break - - line_list = line.split("\n") - dataset_name = line_list[0] - dataset_list.append(dataset_name) + with open(file) as fd: + line = True + while True: + line = fd.readline() + if not line: + break + + line_list = line.split("\n") + dataset_name = line_list[0] + dataset_list.append(dataset_name) statement = "" From 79636894c7f2caed6c0b2fb0d5b707ac9a909247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 24 Aug 2024 18:28:56 +0000 Subject: [PATCH 4/8] t.rast.what: Use a context manager for opening files (SIM115) --- temporal/t.rast.what/t.rast.what.py | 288 ++++++++++++++-------------- 1 file changed, 139 insertions(+), 149 deletions(-) diff --git a/temporal/t.rast.what/t.rast.what.py b/temporal/t.rast.what/t.rast.what.py index 9fa4ad5d793..e35c6fa7aa1 100755 --- a/temporal/t.rast.what/t.rast.what.py +++ b/temporal/t.rast.what/t.rast.what.py @@ -373,78 +373,72 @@ def one_point_per_row_output( output is of type: x,y,start,end,value """ # open the output file for writing - out_file = open(output, "w") if output != "-" else sys.stdout - - if write_header is True: - out_str = "" - if vcat: - out_str += "cat{sep}" - if site_input: - out_str += "x{sep}y{sep}site{sep}start{sep}end{sep}value\n" - else: - out_str += "x{sep}y{sep}start{sep}end{sep}value\n" - out_file.write(out_str.format(sep=separator)) - - for count in range(len(output_files)): - file_name = output_files[count] - gs.verbose(_("Transforming r.what output file %s" % (file_name))) - map_list = output_time_list[count] - in_file = open(file_name, "r") - for line in in_file: - line = line.split(separator) + with open(output, "w") if output != "-" else sys.stdout as out_file: + if write_header is True: + out_str = "" if vcat: - cat = line[0] - x = line[1] - y = line[2] - values = line[4:] - if site_input: - site = line[3] - values = line[5:] - + out_str += "cat{sep}" + if site_input: + out_str += "x{sep}y{sep}site{sep}start{sep}end{sep}value\n" else: - x = line[0] - y = line[1] - if site_input: - site = line[2] - values = line[3:] + out_str += "x{sep}y{sep}start{sep}end{sep}value\n" + out_file.write(out_str.format(sep=separator)) + + for count in range(len(output_files)): + file_name = output_files[count] + gs.verbose(_("Transforming r.what output file %s" % (file_name))) + map_list = output_time_list[count] + with open(file_name) as in_file: + for line in in_file: + line = line.split(separator) + if vcat: + cat = line[0] + x = line[1] + y = line[2] + values = line[4:] + if site_input: + site = line[3] + values = line[5:] - for i in range(len(values)): - start, end = map_list[i].get_temporal_extent_as_tuple() - if vcat: - cat_str = "{ca}{sep}".format(ca=cat, sep=separator) - else: - cat_str = "" - if site_input: - coor_string = ( - "%(x)10.10f%(sep)s%(y)10.10f%(sep)s%(site_name)s%(sep)s" - % ( + else: + x = line[0] + y = line[1] + if site_input: + site = line[2] + values = line[3:] + + for i in range(len(values)): + start, end = map_list[i].get_temporal_extent_as_tuple() + if vcat: + cat_str = "{ca}{sep}".format(ca=cat, sep=separator) + else: + cat_str = "" + if site_input: + coor_string = ( + "%(x)10.10f%(sep)s%(y)10.10f%(sep)s%(site_name)s%(sep)s" + % ( + { + "x": float(x), + "y": float(y), + "site_name": str(site), + "sep": separator, + } + ) + ) + else: + coor_string = "%(x)10.10f%(sep)s%(y)10.10f%(sep)s" % ( + {"x": float(x), "y": float(y), "sep": separator} + ) + time_string = "%(start)s%(sep)s%(end)s%(sep)s%(val)s\n" % ( { - "x": float(x), - "y": float(y), - "site_name": str(site), + "start": str(start), + "end": str(end), + "val": (values[i].strip()), "sep": separator, } ) - ) - else: - coor_string = "%(x)10.10f%(sep)s%(y)10.10f%(sep)s" % ( - {"x": float(x), "y": float(y), "sep": separator} - ) - time_string = "%(start)s%(sep)s%(end)s%(sep)s%(val)s\n" % ( - { - "start": str(start), - "end": str(end), - "val": (values[i].strip()), - "sep": separator, - } - ) - out_file.write(cat_str + coor_string + time_string) - - in_file.close() - - if out_file is not sys.stdout: - out_file.close() + out_file.write(cat_str + coor_string + time_string) ############################################################################ @@ -460,84 +454,84 @@ def one_point_per_col_output( Each row represents a single raster map, hence a single time stamp """ # open the output file for writing - out_file = open(output, "w") if output != "-" else sys.stdout + # out_file = open(output, "w") if output != "-" else sys.stdout first = True - for count in range(len(output_files)): - file_name = output_files[count] - gs.verbose(_("Transforming r.what output file %s" % (file_name))) - map_list = output_time_list[count] - in_file = open(file_name, "r") - lines = in_file.readlines() + with open(output, "w") if output != "-" else sys.stdout as out_file: + for count in range(len(output_files)): + file_name = output_files[count] + gs.verbose(_("Transforming r.what output file %s" % (file_name))) + map_list = output_time_list[count] + with open(file_name) as in_file: + lines = in_file.readlines() - matrix = [] - for line in lines: - matrix.append(line.split(separator)) + matrix = [] + for line in lines: + matrix.append(line.split(separator)) - num_cols = len(matrix[0]) - - if first is True: - if write_header is True: - out_str = "start%(sep)send" % ({"sep": separator}) + num_cols = len(matrix[0]) - # Define different separator for coordinates and sites - if separator == ",": - coor_sep = ";" - else: - coor_sep = "," + if first is True: + if write_header is True: + out_str = "start%(sep)send" % ({"sep": separator}) - for row in matrix: - if vcat: - cat = row[0] - x = row[1] - y = row[2] - out_str += ( - "{sep}{cat}{csep}{x:10.10f}{csep}" - "{y:10.10f}".format( - cat=cat, - x=float(x), - y=float(y), - sep=separator, - csep=coor_sep, - ) - ) - if site_input: - site = row[3] - out_str += "{sep}{site}".format(sep=coor_sep, site=site) + # Define different separator for coordinates and sites + if separator == ",": + coor_sep = ";" else: - x = row[0] - y = row[1] - out_str += "{sep}{x:10.10f}{csep}{y:10.10f}".format( - x=float(x), y=float(y), sep=separator, csep=coor_sep - ) - if site_input: - site = row[2] - out_str += "{sep}{site}".format(sep=coor_sep, site=site) + coor_sep = "," + + for row in matrix: + if vcat: + cat = row[0] + x = row[1] + y = row[2] + out_str += ( + "{sep}{cat}{csep}{x:10.10f}{csep}" + "{y:10.10f}".format( + cat=cat, + x=float(x), + y=float(y), + sep=separator, + csep=coor_sep, + ) + ) + if site_input: + site = row[3] + out_str += "{sep}{site}".format(sep=coor_sep, site=site) + else: + x = row[0] + y = row[1] + out_str += "{sep}{x:10.10f}{csep}{y:10.10f}".format( + x=float(x), y=float(y), sep=separator, csep=coor_sep + ) + if site_input: + site = row[2] + out_str += "{sep}{site}".format(sep=coor_sep, site=site) - out_file.write(out_str + "\n") + out_file.write(out_str + "\n") - first = False + first = False - if vcat: - ncol = 4 - else: - ncol = 3 - for col in range(num_cols - ncol): - start, end = output_time_list[count][col].get_temporal_extent_as_tuple() - time_string = "%(start)s%(sep)s%(end)s" % ( - {"start": str(start), "end": str(end), "sep": separator} - ) - out_file.write(time_string) - for row in range(len(matrix)): - value = matrix[row][col + ncol] - out_file.write( - "%(sep)s%(value)s" % ({"sep": separator, "value": value.strip()}) + if vcat: + ncol = 4 + else: + ncol = 3 + for col in range(num_cols - ncol): + start, end = output_time_list[count][col].get_temporal_extent_as_tuple() + time_string = "%(start)s%(sep)s%(end)s" % ( + {"start": str(start), "end": str(end), "sep": separator} ) - out_file.write("\n") + out_file.write(time_string) + for row in range(len(matrix)): + value = matrix[row][col + ncol] + out_file.write( + "%(sep)s%(value)s" + % ({"sep": separator, "value": value.strip()}) + ) + out_file.write("\n") - in_file.close() - if out_file is not sys.stdout: - out_file.close() + in_file.close() ############################################################################ @@ -554,7 +548,6 @@ def one_point_per_timerow_output( 3730731.49590371|5642483.51236521|6|8|7|7 3581249.04638104|5634411.97526282|5|8|7|7 """ # noqa: E501 - out_file = open(output, "w") if output != "-" else sys.stdout matrix = [] header = "" @@ -564,7 +557,6 @@ def one_point_per_timerow_output( file_name = output_files[count] gs.verbose("Transforming r.what output file %s" % (file_name)) map_list = output_time_list[count] - in_file = open(file_name, "r") if write_header: if first is True: @@ -583,7 +575,8 @@ def one_point_per_timerow_output( ) header += time_string - lines = in_file.readlines() + with open(file_name) as in_file: + lines = in_file.readlines() for i in range(len(lines)): cols = lines[i].split(separator) @@ -603,26 +596,23 @@ def one_point_per_timerow_output( first = False - in_file.close() - - if write_header: - out_file.write(header + "\n") + with open(output, "w") if output != "-" else sys.stdout as out_file: + if write_header: + out_file.write(header + "\n") - gs.verbose(_("Writing the output file <%s>" % (output))) - for row in matrix: - first = True - for col in row: - value = col.strip() + gs.verbose(_("Writing the output file <%s>" % (output))) + for row in matrix: + first = True + for col in row: + value = col.strip() - if first is False: - out_file.write("%s" % (separator)) - out_file.write(value) + if first is False: + out_file.write("%s" % (separator)) + out_file.write(value) - first = False + first = False - out_file.write("\n") - if out_file is not sys.stdout: - out_file.close() + out_file.write("\n") ############################################################################ From a31c2977496aaa98738b293d8f752ca07e702fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sat, 24 Aug 2024 18:46:55 +0000 Subject: [PATCH 5/8] grass.temporal.aggregation: Use a context manager for opening files (SIM115) --- python/grass/temporal/aggregation.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/python/grass/temporal/aggregation.py b/python/grass/temporal/aggregation.py index 1ea60eaeea1..de330db318c 100644 --- a/python/grass/temporal/aggregation.py +++ b/python/grass/temporal/aggregation.py @@ -168,13 +168,11 @@ def aggregate_raster_maps( # Create the r.series input file filename = gs.tempfile(True) - file = open(filename, "w") + with open(filename, "w") as out_file: + for name in inputs: + string = "%s\n" % (name) + out_file.write(string) - for name in inputs: - string = "%s\n" % (name) - file.write(string) - - file.close() # Run r.series try: if len(inputs) > 1000: @@ -365,11 +363,10 @@ def aggregate_by_topology( if len(aggregation_list) > 1: # Create the r.series input file filename = gs.tempfile(True) - file = open(filename, "w") - for name in aggregation_list: - string = "%s\n" % (name) - file.write(string) - file.close() + with open(filename, "w") as out_file: + for name in aggregation_list: + string = "%s\n" % (name) + out_file.write(string) mod = copy.deepcopy(r_series) mod(file=filename, output=output_name) From bb2e9990623d7571b34790330a2be6e163c24469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:35:33 -0400 Subject: [PATCH 6/8] Apply suggestions from code review --- temporal/t.rast.what/t.rast.what.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/temporal/t.rast.what/t.rast.what.py b/temporal/t.rast.what/t.rast.what.py index e35c6fa7aa1..3373b334023 100755 --- a/temporal/t.rast.what/t.rast.what.py +++ b/temporal/t.rast.what/t.rast.what.py @@ -454,7 +454,6 @@ def one_point_per_col_output( Each row represents a single raster map, hence a single time stamp """ # open the output file for writing - # out_file = open(output, "w") if output != "-" else sys.stdout first = True with open(output, "w") if output != "-" else sys.stdout as out_file: @@ -531,7 +530,6 @@ def one_point_per_col_output( ) out_file.write("\n") - in_file.close() ############################################################################ From f7a60ec10ba267382a9bf839ed410f85f4d939cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:37:36 -0400 Subject: [PATCH 7/8] Update t.rast.what.py Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- temporal/t.rast.what/t.rast.what.py | 1 - 1 file changed, 1 deletion(-) diff --git a/temporal/t.rast.what/t.rast.what.py b/temporal/t.rast.what/t.rast.what.py index 3373b334023..a6fce8e7e51 100755 --- a/temporal/t.rast.what/t.rast.what.py +++ b/temporal/t.rast.what/t.rast.what.py @@ -531,7 +531,6 @@ def one_point_per_col_output( out_file.write("\n") - ############################################################################ From 71bc611cc4b9d5125f8fa9d2a0bc4357bedbd283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Thu, 17 Oct 2024 01:19:58 +0000 Subject: [PATCH 8/8] Move line out of context manager --- temporal/t.remove/t.remove.py | 2 +- temporal/t.unregister/t.unregister.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/temporal/t.remove/t.remove.py b/temporal/t.remove/t.remove.py index f547ce6fafa..a5bb3d85fd8 100755 --- a/temporal/t.remove/t.remove.py +++ b/temporal/t.remove/t.remove.py @@ -101,8 +101,8 @@ def main(): dataset_list = tuple(datasets.split(",")) if file: + line = True with open(file) as fd: - line = True while True: line = fd.readline() if not line: diff --git a/temporal/t.unregister/t.unregister.py b/temporal/t.unregister/t.unregister.py index f2635941187..b46c9fedd24 100755 --- a/temporal/t.unregister/t.unregister.py +++ b/temporal/t.unregister/t.unregister.py @@ -108,8 +108,8 @@ def main(): # Read the map list from file if file: + line = True with open(file) as fd: - line = True while True: line = fd.readline() if not line: