#๐Ÿ”’ Question: Should I use a dictionary or list?

21 messages ยท Page 1 of 1 (latest)

warped badger
#

What would be best to use here when returning the values of a form? I believe both would work but what is best practice?
Code is not fully complete.

import discord
from discord import app_commands
from discord.ext import commands

class UnitCreationForm(discord.ui.Modal, title='Unit Creation Form'):
  unit_name = discord.ui.TextInput(
    label="Unit Name",
    required=True,
  )
  unit_name_abr = discord.ui.TextInput( # Unit Name Abbreviated
    label="Abbreviated Unit Name",
    placeholder="For example: USAF",
    required=True,
  )
  unit_desc = discord.ui.TextInput(
    label="Unit Description",
    style=discord.TextStyle.long,
    placeholder="Please describe your unit in detail",
    required=True,
    min_length=300,
    max_length=2000,
  )
  unit_member_count = discord.ui.TextInput(
    label="How many members does your unit have?",
  )
  unit_member_activity = discord.ui.TextInput(
    label="What is your unit's activity?",
    placeholder="How many on a typical operation...",
  )
  
  async def on_submit(self, interaction: discord.Interaction) -> :
    await interaction.response.send_message(f'Thank you for submitting your unit creation form, a member of the staff team will review it within 24 hours. We hope to see {self.unit_name.value} here soon!', ephemeral=True)
    unit_data = [self.unit_name.value, self.unit_name_abr.value, self.unit_desc.value, self.unit_member_count.value, self.unit_member_activity.value]
    return unit_data

class Unit(commands.Cog):
  group = app_commands.Group(name="unit", description="Unit and military affair commands.")

  def __init__(self, bot: commands.Bot) -> None:
    self.bot = bot

  @group.command(name="create", description="This command is used to create a unit.")
  async def create(self, interaction: discord.Interaction, user: discord.User = None) -> None:
    if user is None:
      await interaction.response.send_message("User not specified. Please specify a user.", ephemeral=True)

    try:
      await interaction.user.send()

So here:

unit_data = [self.unit_name.value, self.unit_name_abr.value, self.unit_desc.value, self.unit_member_count.value, self.unit_member_activity.value]
    return unit_data

I am using a list, which would work fine but wondering if it is better to use a dictionary or not for storing and accessing the values.

mint socketBOT
#

@warped badger

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

frozen hare
warped badger
frozen hare
#

It would be usual to return results as a tuple rather than a list. It depends if you intend to change the results. But why would you want to do that?

warped badger
#

Will do! Thank you!

frozen hare
# warped badger Will do! Thank you!

Instead of this

unit_data = [self.unit_name.value, self.unit_name_abr.value, self.unit_desc.value, self.unit_member_count.value, self.unit_member_activity.value]
    return unit_data

You could try this

return self.unit_name.value, 
       self.unit_name_abr.value, 
       self.unit_desc.value, 
       self.unit_member_count.value, 
       self.unit_member_activity.value
warped badger
frozen hare
#

And unpack the results where they are received.

frozen hare
warped badger
#

Okay, I'll do that then, saves writing out more code!

#

How would I access the values though?

#

As I would access currently like so: unit_data[1]

frozen hare
#

a, b, c = return_value

frozen hare
warped badger
#

Thank you.

#

!close

mint socketBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.