From 870343fe17389f3d40d0c6fa81a906d07805ac65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edouard=20Choini=C3=A8re?= <27212526+echoix@users.noreply.github.com> Date: Sun, 20 Oct 2024 17:06:56 -0400 Subject: [PATCH] style: Fix if-else-block-instead-of-if-exp (SIM108) (part 1) (#4561) Ruff rule: https://docs.astral.sh/ruff/rules/if-else-block-instead-of-if-exp Since the amount of changes for SIM108 are quite large, (more than 330), I've decided to split the fixes in more than 1 PR. I kept `scripts/*`, `python/*` and `gui/*` in other PRs. To avoid conflicts, I didn't commit the modified `pyproject.toml`, nor extra typing annotations that I used to help guiding and validating these fixes. * style: Fix if-else-block-instead-of-if-exp (SIM108) in utils/ * style: Fix if-else-block-instead-of-if-exp (SIM108) in lib/init/grass.py * style: Fix if-else-block-instead-of-if-exp (SIM108) in doc/ * style: Fix if-else-block-instead-of-if-exp (SIM108) in man/build_html.py * style: Fix if-else-block-instead-of-if-exp (SIM108) in temporal/ * style: Manual fixes of if-else-block-instead-of-if-exp (SIM108) in temporal/ --- doc/python/raster_example_ctypes.py | 5 +-- doc/python/vector_example_ctypes.py | 5 +-- lib/init/grass.py | 33 ++++----------- man/build_html.py | 5 +-- temporal/t.info/t.info.py | 6 +-- temporal/t.list/t.list.py | 5 +-- temporal/t.rast.accdetect/t.rast.accdetect.py | 41 ++++--------------- .../t.rast.accumulate/t.rast.accumulate.py | 38 ++++------------- temporal/t.rast.aggregate/t.rast.aggregate.py | 5 +-- temporal/t.rast.to.rast3/t.rast.to.rast3.py | 5 +-- temporal/t.rast.what/t.rast.what.py | 20 ++------- temporal/t.rename/t.rename.py | 11 +---- temporal/t.topology/t.topology.py | 5 +-- temporal/t.unregister/t.unregister.py | 7 +--- .../t.vect.observe.strds.py | 7 +--- .../t.vect.what.strds/t.vect.what.strds.py | 9 ++-- utils/g.html2man/ggroff.py | 5 +-- utils/mkrest.py | 5 +-- utils/ppmrotate.py | 7 +--- utils/update_version.py | 5 +-- 20 files changed, 48 insertions(+), 181 deletions(-) diff --git a/doc/python/raster_example_ctypes.py b/doc/python/raster_example_ctypes.py index 67f7a2cf4cb..54de0aab30b 100644 --- a/doc/python/raster_example_ctypes.py +++ b/doc/python/raster_example_ctypes.py @@ -40,10 +40,7 @@ sys.exit("You must be in GRASS GIS to run this program") # parse command line arguments, prompt user for a raster map name if one wasn't given -if len(sys.argv) == 2: - input = sys.argv[1] -else: - input = input("Name of raster map? ") +input = sys.argv[1] if len(sys.argv) == 2 else input("Name of raster map? ") # initialize GRASS library G_gisinit("") diff --git a/doc/python/vector_example_ctypes.py b/doc/python/vector_example_ctypes.py index ae3877224d0..27053d653c9 100644 --- a/doc/python/vector_example_ctypes.py +++ b/doc/python/vector_example_ctypes.py @@ -33,10 +33,7 @@ if "GISBASE" not in os.environ: sys.exit("You must be in GRASS GIS to run this program.") -if len(sys.argv) == 2: - input = sys.argv[1] -else: - input = input("Name of vector map? ") +input = sys.argv[1] if len(sys.argv) == 2 else input("Name of vector map? ") # initialize GRASS library G_gisinit("") diff --git a/lib/init/grass.py b/lib/init/grass.py index d2d8301c52b..505d48dadae 100755 --- a/lib/init/grass.py +++ b/lib/init/grass.py @@ -125,7 +125,7 @@ def clean_env(): write_gisrc(env_new, gisrc) -def is_debug(): +def is_debug() -> bool: """Returns True if we are in debug mode For debug messages use ``debug()``. @@ -133,13 +133,8 @@ def is_debug(): global _DEBUG if _DEBUG is not None: return _DEBUG - _DEBUG = os.getenv("GRASS_DEBUG") # translate to bool (no or empty variable means false) - if _DEBUG: - _DEBUG = True - else: - _DEBUG = False - return _DEBUG + return bool(os.getenv("GRASS_DEBUG")) def debug(msg): @@ -553,10 +548,7 @@ def write_gisrc(kv, filename, append=False): def add_mapset_to_gisrc(gisrc, grassdb, location, mapset): - if os.access(gisrc, os.R_OK): - kv = read_gisrc(gisrc) - else: - kv = {} + kv = read_gisrc(gisrc) if os.access(gisrc, os.R_OK) else {} kv["GISDBASE"] = grassdb kv["LOCATION_NAME"] = location kv["MAPSET"] = mapset @@ -564,10 +556,7 @@ def add_mapset_to_gisrc(gisrc, grassdb, location, mapset): def add_last_mapset_to_gisrc(gisrc, last_mapset_path): - if os.access(gisrc, os.R_OK): - kv = read_gisrc(gisrc) - else: - kv = {} + kv = read_gisrc(gisrc) if os.access(gisrc, os.R_OK) else {} kv["LAST_MAPSET_PATH"] = last_mapset_path write_gisrc(kv, gisrc) @@ -1333,11 +1322,8 @@ def get_shell(): sh = os.path.basename(sh) else: # If SHELL is not set, see if there is Bash and use it. - if shutil.which("bash"): - sh = "bash" - else: - # Fallback to sh if there is no Bash on path. - sh = "sh" + # Fallback to sh if there is no Bash on path. + sh = "bash" if shutil.which("bash") else "sh" # Ensure the variable is set. os.environ["SHELL"] = sh @@ -1651,11 +1637,8 @@ def sh_like_startup(location, location_name, grass_env_file, sh): else: f.write("test -r ~/.alias && . ~/.alias\n") - if os.getenv("ISISROOT"): - # GRASS GIS and ISIS blend - grass_name = "ISIS-GRASS" - else: - grass_name = "GRASS" + # GRASS GIS and ISIS blend + grass_name = "GRASS" if not os.getenv("ISISROOT") else "ISIS-GRASS" if sh == "zsh": f.write("setopt PROMPT_SUBST\n") diff --git a/man/build_html.py b/man/build_html.py index 0ef860d42f3..a8cc4c057a5 100644 --- a/man/build_html.py +++ b/man/build_html.py @@ -464,10 +464,7 @@ def write_html_cmd_overview(f): def write_html_footer(f, index_url, year=None): - if year is None: - cur_year = default_year - else: - cur_year = year + cur_year = default_year if year is None else year f.write( footer_tmpl.substitute( grass_version=grass_version, index_url=index_url, year=cur_year diff --git a/temporal/t.info/t.info.py b/temporal/t.info/t.info.py index 575446030a1..c04fe0c0a5f 100755 --- a/temporal/t.info/t.info.py +++ b/temporal/t.info/t.info.py @@ -103,11 +103,7 @@ def main(): if not system and not name: gs.fatal(_("Please specify %s=") % ("name")) - if name.find("@") >= 0: - id_ = name - else: - id_ = name + "@" + gs.gisenv()["MAPSET"] - + id_ = name if name.find("@") >= 0 else name + "@" + gs.gisenv()["MAPSET"] dataset = tgis.dataset_factory(type_, id_) if not dataset.is_in_db(dbif): diff --git a/temporal/t.list/t.list.py b/temporal/t.list/t.list.py index 52285d04b83..c41bcae4e22 100755 --- a/temporal/t.list/t.list.py +++ b/temporal/t.list/t.list.py @@ -123,10 +123,7 @@ def main(): outfile = open(outpath, "w") for ttype in temporal_type.split(","): - if ttype == "absolute": - time = "absolute time" - else: - time = "relative time" + time = "absolute time" if ttype == "absolute" else "relative time" stds_list = tgis.get_dataset_list(type, ttype, columns, where, order, dbif=dbif) diff --git a/temporal/t.rast.accdetect/t.rast.accdetect.py b/temporal/t.rast.accdetect/t.rast.accdetect.py index 1588b44d4b9..17433e06f3f 100644 --- a/temporal/t.rast.accdetect/t.rast.accdetect.py +++ b/temporal/t.rast.accdetect/t.rast.accdetect.py @@ -173,11 +173,7 @@ def main(): mapset = tgis.get_current_mapset() - if input.find("@") >= 0: - id = input - else: - id = input + "@" + mapset - + id = input if input.find("@") >= 0 else input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if not input_strds.is_in_db(): @@ -261,10 +257,7 @@ def main(): # The minimum threshold space time raster dataset minimum_strds = None if minimum: - if minimum.find("@") >= 0: - minimum_id = minimum - else: - minimum_id = minimum + "@" + mapset + minimum_id = minimum if minimum.find("@") >= 0 else minimum + "@" + mapset minimum_strds = tgis.SpaceTimeRasterDataset(minimum_id) if not minimum_strds.is_in_db(): @@ -282,10 +275,7 @@ def main(): # The maximum threshold space time raster dataset maximum_strds = None if maximum: - if maximum.find("@") >= 0: - maximum_id = maximum - else: - maximum_id = maximum + "@" + mapset + maximum_id = maximum if maximum.find("@") >= 0 else maximum + "@" + mapset maximum_strds = tgis.SpaceTimeRasterDataset(maximum_id) if not maximum_strds.is_in_db(): @@ -304,16 +294,10 @@ def main(): if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) - if stop: - stop = tgis.string_to_datetime(stop) - else: - stop = input_strds_end + stop = tgis.string_to_datetime(stop) if stop else input_strds_end else: start = int(start) - if stop: - stop = int(stop) - else: - stop = input_strds_end + stop = int(stop) if stop else input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) @@ -365,10 +349,7 @@ def main(): if indicator: num_maps = len(input_maps) for i in range(num_maps): - if reverse: - map = input_maps[num_maps - i - 1] - else: - map = input_maps[i] + map = input_maps[num_maps - i - 1] if reverse else input_maps[i] if ( input_strds.get_temporal_type() == "absolute" @@ -637,19 +618,13 @@ def compute_occurrence( # Aggregate num_maps = len(input_maps) for i in range(num_maps): - if reverse: - map = input_maps[num_maps - i - 1] - else: - map = input_maps[i] + map = input_maps[num_maps - i - 1] if reverse else input_maps[i] # Compute the days since start input_start, input_end = map.get_temporal_extent_as_tuple() td = input_start - start - if map.is_time_absolute(): - days = tgis.time_delta_to_relative_time(td) - else: - days = td + days = tgis.time_delta_to_relative_time(td) if map.is_time_absolute() else td if input_strds.get_temporal_type() == "absolute" and tsuffix == "gran": suffix = tgis.create_suffix_from_datetime( diff --git a/temporal/t.rast.accumulate/t.rast.accumulate.py b/temporal/t.rast.accumulate/t.rast.accumulate.py index 6fe067e02bc..d254d21c110 100644 --- a/temporal/t.rast.accumulate/t.rast.accumulate.py +++ b/temporal/t.rast.accumulate/t.rast.accumulate.py @@ -192,11 +192,7 @@ def main(): mapset = tgis.get_current_mapset() - if input.find("@") >= 0: - id = input - else: - id = input + "@" + mapset - + id = input if input.find("@") >= 0 else input + "@" + mapset input_strds = tgis.SpaceTimeRasterDataset(id) if not input_strds.is_in_db(): @@ -205,10 +201,7 @@ def main(): input_strds.select(dbif) - if output.find("@") >= 0: - out_id = output - else: - out_id = output + "@" + mapset + out_id = output if output.find("@") >= 0 else output + "@" + mapset # The output space time raster dataset output_strds = tgis.SpaceTimeRasterDataset(out_id) @@ -244,11 +237,7 @@ def main(): # The lower threshold space time raster dataset if lower: - - if lower.find("@") >= 0: - lower_id = lower - else: - lower_id = lower + "@" + mapset + lower_id = lower if lower.find("@") >= 0 else lower + "@" + mapset lower_strds = tgis.SpaceTimeRasterDataset(lower_id) if not lower_strds.is_in_db(): @@ -271,11 +260,7 @@ def main(): _("The upper option works only in conjunction with the lower option") ) - if upper.find("@") >= 0: - upper_id = upper - else: - upper_id = upper + "@" + mapset - + upper_id = upper if upper.find("@") >= 0 else upper + "@" + mapset upper_strds = tgis.SpaceTimeRasterDataset(upper_id) if not upper_strds.is_in_db(): dbif.close() @@ -293,17 +278,11 @@ def main(): if input_strds.is_time_absolute(): start = tgis.string_to_datetime(start) - if stop: - stop = tgis.string_to_datetime(stop) - else: - stop = input_strds_end + stop = tgis.string_to_datetime(stop) if stop else input_strds_end start = tgis.adjust_datetime_to_granularity(start, granularity) else: start = int(start) - if stop: - stop = int(stop) - else: - stop = input_strds_end + stop = int(stop) if stop else input_strds_end if input_strds.is_time_absolute(): end = tgis.increment_datetime_by_string(start, cycle) @@ -371,10 +350,7 @@ def main(): num_maps = len(gran_list) for i in range(num_maps): - if reverse: - map = gran_list[num_maps - i - 1] - else: - map = gran_list[i] + map = gran_list[num_maps - i - 1] if reverse else gran_list[i] # Select input maps based on temporal topology relations input_maps = [] if map.get_equal(): diff --git a/temporal/t.rast.aggregate/t.rast.aggregate.py b/temporal/t.rast.aggregate/t.rast.aggregate.py index 609d6d19d38..0675407794a 100755 --- a/temporal/t.rast.aggregate/t.rast.aggregate.py +++ b/temporal/t.rast.aggregate/t.rast.aggregate.py @@ -218,10 +218,7 @@ def main(): dbif, gs.overwrite(), ) - if register_null: - register_null = False - else: - register_null = True + register_null = not register_null tgis.register_map_object_list( "rast", diff --git a/temporal/t.rast.to.rast3/t.rast.to.rast3.py b/temporal/t.rast.to.rast3/t.rast.to.rast3.py index 7776d7426ee..118e19549c6 100755 --- a/temporal/t.rast.to.rast3/t.rast.to.rast3.py +++ b/temporal/t.rast.to.rast3/t.rast.to.rast3.py @@ -183,10 +183,7 @@ def main(): gs.warning(_("%s failed to set units.") % "r3.support") # Register the space time voxel cube in the temporal GIS - if output.find("@") >= 0: - id = output - else: - id = output + "@" + mapset + id = output if output.find("@") >= 0 else output + "@" + mapset start, end = sp.get_temporal_extent_as_tuple() r3ds = tgis.Raster3DDataset(id) diff --git a/temporal/t.rast.what/t.rast.what.py b/temporal/t.rast.what/t.rast.what.py index b3cec73a538..4d13c6a924c 100755 --- a/temporal/t.rast.what/t.rast.what.py +++ b/temporal/t.rast.what/t.rast.what.py @@ -410,10 +410,7 @@ def one_point_per_row_output( 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 = "" + cat_str = "{ca}{sep}".format(ca=cat, sep=separator) if vcat else "" if site_input: coor_string = ( "%(x)10.10f%(sep)s%(y)10.10f%(sep)s%(site_name)s%(sep)s" @@ -480,10 +477,7 @@ def one_point_per_col_output( out_str = "start%(sep)send" % ({"sep": separator}) # Define different separator for coordinates and sites - if separator == ",": - coor_sep = ";" - else: - coor_sep = "," + coor_sep = ";" if separator == "," else "," for row in matrix: if vcat: @@ -517,10 +511,7 @@ def one_point_per_col_output( first = False - if vcat: - ncol = 4 - else: - ncol = 3 + ncol = 4 if vcat else 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" % ( @@ -567,10 +558,7 @@ def one_point_per_timerow_output( if write_header: if first is True: - if vcat: - header = "cat{sep}".format(sep=separator) - else: - header = "" + header = "cat{sep}".format(sep=separator) if vcat else "" if site_input: header += "x%(sep)sy%(sep)ssite" % ({"sep": separator}) else: diff --git a/temporal/t.rename/t.rename.py b/temporal/t.rename/t.rename.py index a6aee79b790..a1f1a164fee 100755 --- a/temporal/t.rename/t.rename.py +++ b/temporal/t.rename/t.rename.py @@ -59,15 +59,8 @@ def main(): # Get the current mapset to create the id of the space time dataset mapset = gs.gisenv()["MAPSET"] - if input.find("@") >= 0: - old_id = input - else: - old_id = input + "@" + mapset - - if output.find("@") >= 0: - new_id = output - else: - new_id = output + "@" + mapset + old_id = input if input.find("@") >= 0 else input + "@" + mapset + new_id = output if output.find("@") >= 0 else output + "@" + mapset # Do not overwrite yourself if new_id == old_id: diff --git a/temporal/t.topology/t.topology.py b/temporal/t.topology/t.topology.py index 4356596f319..e9da9b369a7 100755 --- a/temporal/t.topology/t.topology.py +++ b/temporal/t.topology/t.topology.py @@ -74,10 +74,7 @@ def main(): spatial = None if spatio_temporal_relations: - if sp.get_type() == "strds": - spatial = "2D" - else: - spatial = "3D" + spatial = "2D" if sp.get_type() == "strds" else "3D" if temporal_relations or spatio_temporal_relations: sp.print_spatio_temporal_relationships(maps=maps, spatial=spatial) diff --git a/temporal/t.unregister/t.unregister.py b/temporal/t.unregister/t.unregister.py index b39b7b9819f..cacfe579a7b 100755 --- a/temporal/t.unregister/t.unregister.py +++ b/temporal/t.unregister/t.unregister.py @@ -93,12 +93,7 @@ def main(): # Map names as comma separated string if maps is not None and maps != "": - if maps.find(",") == -1: - maplist = [ - maps, - ] - else: - maplist = maps.split(",") + maplist = [maps] if maps.find(",") == -1 else maps.split(",") # Build the maplist for count in range(len(maplist)): diff --git a/temporal/t.vect.observe.strds/t.vect.observe.strds.py b/temporal/t.vect.observe.strds/t.vect.observe.strds.py index 9c60a3d026c..547f60a3250 100755 --- a/temporal/t.vect.observe.strds/t.vect.observe.strds.py +++ b/temporal/t.vect.observe.strds/t.vect.observe.strds.py @@ -202,11 +202,8 @@ def main(): vector_db = gs.vector.vector_db(input) # We copy the vector table and create the new layers - if vector_db: - # Use the first layer to copy the categories from - layers = "1," - else: - layers = "" + # If vector_db, use the first layer to copy the categories from + layers = "1," if vector_db else "" first = True for layer in range(num_samples): layer += 1 diff --git a/temporal/t.vect.what.strds/t.vect.what.strds.py b/temporal/t.vect.what.strds/t.vect.what.strds.py index 6ea36e79579..b1f2c17a9c6 100755 --- a/temporal/t.vect.what.strds/t.vect.what.strds.py +++ b/temporal/t.vect.what.strds/t.vect.what.strds.py @@ -158,12 +158,9 @@ def main(): raster_maps = (new_map.get_id(),) for rastermap in raster_maps: - if column: - col_name = column - else: - # Create a new column with the SQL compliant - # name of the sampled raster map - col_name = rastermap.split("@")[0].replace(".", "_") + # Create a new column with the SQL compliant + # name of the sampled raster map if not column + col_name = column or rastermap.split("@")[0].replace(".", "_") coltype = "DOUBLE PRECISION" # Get raster type diff --git a/utils/g.html2man/ggroff.py b/utils/g.html2man/ggroff.py index e62c5ec2b1c..a0458dd0334 100644 --- a/utils/g.html2man/ggroff.py +++ b/utils/g.html2man/ggroff.py @@ -119,10 +119,7 @@ def fmt(self, format, content, var=None): self.show(pre) if sep != "": if var: - if var == "index": - val = self.get("index") + [0] - else: - val = True + val = self.get("index") + [0] if var == "index" else True self.pp_with(content, var, val) else: self.pp(content) diff --git a/utils/mkrest.py b/utils/mkrest.py index 24481bc3374..8d156e0bcc7 100755 --- a/utils/mkrest.py +++ b/utils/mkrest.py @@ -21,10 +21,7 @@ from datetime import datetime pgm = sys.argv[1] -if len(sys.argv) > 1: - year = sys.argv[2] -else: - year = str(datetime.now().year) +year = sys.argv[2] if len(sys.argv) > 1 else str(datetime.now().year) src_file = "%s.html" % pgm tmp_file = "%s.tmp.txt" % pgm diff --git a/utils/ppmrotate.py b/utils/ppmrotate.py index ec041e0f253..cef32eba0f4 100755 --- a/utils/ppmrotate.py +++ b/utils/ppmrotate.py @@ -110,11 +110,8 @@ def convert_and_rotate(src, dst, flip=False): to_png = False if dst.lower().endswith(".png"): to_png = True - if to_png: - tmp_img = gs.tempfile() + ".ppm" - # TODO: clean up the file - else: - tmp_img = dst + # TODO: clean up the file + tmp_img = gs.tempfile() + ".ppm" if to_png else dst write_ppm(tmp_img, ppm) if to_png: ppmtopng(dst, tmp_img) diff --git a/utils/update_version.py b/utils/update_version.py index c66a34a62cd..947e68a1648 100755 --- a/utils/update_version.py +++ b/utils/update_version.py @@ -226,10 +226,7 @@ def status(args): version_info = read_version_file() today = datetime.date.today().isoformat() version = construct_version(version_info) - if not version_info.micro.endswith("dev"): - tag = version - else: - tag = None + tag = version if not version_info.micro.endswith("dev") else None if args.bash: status_as_bash(version_info=version_info, today=today, version=version, tag=tag) else: