#๐Ÿ”’ More pythonic way of handling output of function that can return 1 or 2 arguments?

57 messages ยท Page 1 of 1 (latest)

lone fern
#

maybe something with unpacking?

maybe2 = foo() # foo can return 1 or 2 arguments
try:
  a, b = maybe2
except ValueError:
  a = maybe2
try:
  foo2(a, b)
except (SomeError, NameError):
  foo3(a)
haughty thicketBOT
#

@lone fern

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.

buoyant coral
lone fern
#

because foo() can be multiple different functions

buoyant coral
#

what do you mean?

lone fern
#
def outer(foo: Callable)
  maybe2 = foo() # foo can return 1 or 2 arguments
  try:
    a, b = maybe2
  except ValueError:
    a = maybe2
  try:
    foo2(a, b)
  except (SomeError, NameError):
    foo3(a)

outer(someFunc)
outer(someFunc2)
buoyant coral
#

What kind of objects do they return?

lone fern
#

a is a discord embed

#

b is a filepath string

#

it's a path to an icon for the embed

#

foo is a function that creates an embed that may or may not have a thumbnail

buoyant coral
#
def outer(foo):
    embed, filepath = foo()
    if filepath is None:
        return foo3(embed)
    else:
        return foo2(embed, filepath)
lone fern
#

you forgot about the SomeError

#

if foo2 returns SomeError I want the same behavior as if there was no b

buoyant coral
#
def outer(foo):
    embed, filepath = foo()
    if filepath is None:
        return foo3(embed)
    else:
        try:
            return foo2(embed, filepath)
        except SomeError:
            return foo3(embed)
#

what is SomeError?

lone fern
#

FileNotFound

buoyant coral
#

Maybe that try-except belongs inside foo2?

#

If the file is missing, log some warning message and return an embed as if there was no thumbnail

#

actually I'm not sure why you'd need this outer function

#

If you know you have an embed with thumbnail, call foo2. If you know that you don't have a thumbnail, call foo3

#

Maybe you could have a single foo1 function that accepts an optional thumbnail_path parameter

lone fern
#

if you really want more context

#

i can't put the try except inside foo2

#

since that is interaction.response.edit_message

buoyant coral
lone fern
#

foo is embedAndIconPath

#

which is the output of self.matchFunc(**self.kwargs)

buoyant coral
#

Wait. you are always returning embed, fullIconPath from match_and_create_embed

lone fern
#

there's another function besides match_and_create_embed

#

that will not be returning fullIconPath

buoyant coral
#

return embed, None from it then

#

If you're parameterizing something with a function, it's better if it has a uniform interface. It is a big pain to figure out what the function intended to return if it's very diverse

lone fern
#

hmm i don't like that foo3 needs to be repeated

#

is this really the best way?

buoyant coral
#

If the icon file doesn't exist, do you really want it to pass silently?

lone fern
#

it's not silent

#

i have a print statement

#

that specifies when it gets returned without an icon

#

oh you mean separately from intentionally returning without icon?

#

i mean i'll see it

#

and it's expected in most casese

buoyant coral
#

I'd do something like this:

            embed, fullIconPath = embedAndIconPath
            if fullIconPath is None:
                await interaction.response.edit_message(content=None, embed=embed, view=None)
                print('Successfully returned request without icon after editing.')
                return
            image = discord.File(fullIconPath, filename='icon.png')
            embed.set_thumbnail(url="attachment://icon.png")
            try:
                await interaction.response.edit_message(content=None, embed=embed, attachments=[image], view=None)
                print('Successfully returned request after editing.')
            except FileNotFoundError:
                await interaction.response.edit_message(content=None, embed=embed, view=None)
                print(f'Icon {fullIconPath!r} was not found. Sending embed without icon')
#

Catching an error for an undefined variable is a pretty non-obvious move (what variable are you catching?), and it's definitely not the idiomatic way.

wet cairn
#

pattern matching would be cleaner

buoyant coral
#

that is also possible

lone fern
#

what's that

buoyant coral
wet cairn
lone fern
#

this seems like a very neat feature

#

im trying to build a query command for my bot with stuff like AND and NOT and OR

#

could be useful for that

haughty thicketBOT
#
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.