Skip to content

Commit

Permalink
style: Fix if-else-block-instead-of-if-exp (SIM108) (part 1) (#4561)
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
echoix authored Oct 20, 2024
1 parent e523163 commit 870343f
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 181 deletions.
5 changes: 1 addition & 4 deletions doc/python/raster_example_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
5 changes: 1 addition & 4 deletions doc/python/vector_example_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
33 changes: 8 additions & 25 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,16 @@ 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()``.
"""
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):
Expand Down Expand Up @@ -553,21 +548,15 @@ 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
write_gisrc(kv, gisrc)


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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down
5 changes: 1 addition & 4 deletions man/build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions temporal/t.info/t.info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions temporal/t.list/t.list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
41 changes: 8 additions & 33 deletions temporal/t.rast.accdetect/t.rast.accdetect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
38 changes: 7 additions & 31 deletions temporal/t.rast.accumulate/t.rast.accumulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)
Expand Down Expand Up @@ -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():
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
5 changes: 1 addition & 4 deletions temporal/t.rast.aggregate/t.rast.aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions temporal/t.rast.to.rast3/t.rast.to.rast3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 4 additions & 16 deletions temporal/t.rast.what/t.rast.what.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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" % (
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 2 additions & 9 deletions temporal/t.rename/t.rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 870343f

Please sign in to comment.