Use the walrus operator when assigning and checking for None.

This commit is contained in:
2025-03-12 09:35:44 -04:00
parent 29a163ec46
commit 304b089113
2 changed files with 17 additions and 45 deletions
+5 -10
View File
@@ -45,8 +45,7 @@ class GetCog(commands.Cog, name="Get command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Group or Category was not found.", ephemeral=True)
return
@@ -77,8 +76,7 @@ class GetCog(commands.Cog, name="Get command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Group or Category was not found.", ephemeral=True)
return
@@ -107,8 +105,7 @@ class GetCog(commands.Cog, name="Get command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Group or Category was not found.", ephemeral=True)
return
@@ -134,8 +131,7 @@ class GetCog(commands.Cog, name="Get command."):
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == int(group_id))
with Session(engine) as session:
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
@@ -161,8 +157,7 @@ class GetCog(commands.Cog, name="Get command."):
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == int(group_id))
with Session(engine) as session:
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
+12 -35
View File
@@ -50,8 +50,7 @@ class SetCog(commands.Cog, name="Set command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Category or Group was not found.", ephemeral=True)
return
category.size = size
@@ -59,13 +58,11 @@ class SetCog(commands.Cog, name="Set command."):
session.commit()
# Use the relationship to get group category is in.
group = category.category_group
if group is None:
if (group := category.category_group) is None:
agon_logger.warning(f"{category.name}:{category.id} group relationship was None.")
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == group_id)
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
@@ -98,8 +95,7 @@ class SetCog(commands.Cog, name="Set command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Category or Group was not found.", ephemeral=True)
return
category.is_ascending = ascending
@@ -107,13 +103,11 @@ class SetCog(commands.Cog, name="Set command."):
session.commit()
# Use the relationship to get group category is in.
group = category.category_group
if group is None:
if (group := category.category_group) is None:
agon_logger.warning(f"{category.name}:{category.id} group relationship was None.")
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == group_id)
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
@@ -148,25 +142,13 @@ class SetCog(commands.Cog, name="Set command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Category or Group was not found.", ephemeral=True)
return
category.is_validated = validated
# session.add(category) test if this is needed. SQLAlchemy can do the update without adding.
session.commit()
# Use the relationship to get group category is in.
group = category.category_group
if group is None:
agon_logger.warning(f"{category.name}:{category.id} group relationship was None.")
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == group_id)
group = session.scalar(stmt)
if group is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
await interaction.response.send_message(
f"{category.name} is_validated has been updated to {validated}.", ephemeral=True
)
@@ -195,8 +177,7 @@ class SetCog(commands.Cog, name="Set command."):
)
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
if (category := session.scalar(stmt)) is None:
await interaction.response.send_message("Category or Group was not found.", ephemeral=True)
return
old_name = category.name
@@ -205,13 +186,11 @@ class SetCog(commands.Cog, name="Set command."):
session.commit()
# Use the relationship to get group category is in.
group = category.category_group
if group is None:
if (group := category.category_group) is None:
agon_logger.warning(f"{category.name}:{category.id} group relationship was None.")
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == group_id)
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
@@ -240,8 +219,7 @@ class SetCog(commands.Cog, name="Set command."):
stmt = select(CategoryGroup).where(col(CategoryGroup.id) == int(group_id))
with Session(engine) as session:
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
old_name = group.name
@@ -315,8 +293,7 @@ class SetCog(commands.Cog, name="Set command."):
.where(col(CategoryGroup.guild_id) == interaction.guild_id)
.where(col(CategoryGroup.id) == int(group_id))
)
group = session.scalar(stmt)
if group is None:
if (group := session.scalar(stmt)) is None:
await interaction.response.send_message("Group was not found.", ephemeral=True)
return
group.PadLength = length