-1

I have this code

response = sf.list_state_machines(maxResults=2)

I want to:

  • make list_state_machines as a passed in argument to the function - named action_cmd
  • make maxResults=2 be a passed in argument to the function - named payload then I can do the following

response = sf.action_cmd(payload)

this does not work, of course. How do I pass these variables so sf recognizes contents of action_cmd as the name of the command and passes the contents of payload as input to the function?

I have no control over the function I am calling

moken
  • 3,227
  • 8
  • 13
  • 23
efultz
  • 1,135
  • 2
  • 11
  • 26
  • 1
    variables do not *refer to source code*. Variables refer to *objects*. It isn't really clear how you expect this to work. If you could give a clear example, we can help you with alternatives – juanpa.arrivillaga Jul 24 '23 at 21:30
  • I'm not sure I understand the question. Are you looking for `response = getattr(sf, action_cmd)(**payload)`? where `action_cmd = 'list_state_machines'` and `payload = dict(maxResults=2)`. Or maybe you want `response = action_cmd(**payload)` where `action_cmd = sf.list_state_machines`. There are existing questions about these, like [How to access object attribute given string](/q/2612610/4518341), [Converting dict to kwargs?](/q/5710391/4518341), and [How do I pass a method as a parameter](/q/706721/4518341) – wjandrea Jul 24 '23 at 22:09

2 Answers2

0

better to use a wrapper function,I will give you an example, in my example action_cmd is a wrapper function that takes the command and payload as arguments

check this out :

def action_cmd(command, **kwargs):
    if command == "list_state_machines":
        return sf.list_state_machines(**kwargs)
    #add your conditions for other commands

#you can use it like this
command = "list_state_machines"
payload = {"maxResults": 2}
response = action_cmd(command, **payload)
Freeman
  • 9,464
  • 7
  • 35
  • 58
  • Thank you - what you have is what I have now - I am trying to make a generic wrapper where I can pass in the command and the payload. I see, now, the command can only be passed in as an item to send me to the correct wrapper. I wanted to avoid hard-coding the parameters so that a calling program could build the payload as "maxresults=2" and my wrapper would just pass that in to list_state_machines – efultz Jul 24 '23 at 21:46
-1

If you want to dynamically call a method on an object in Python, you can use the getattr() function. It allows you to get the value of a named attribute of an object. If the name is a method, you can then call it.

Here is an example:

def dynamic_call(sf, action_cmd, payload):
    # Get the method from 'sf' based on 'action_cmd'
    method_to_call = getattr(sf, action_cmd)
    # Call the method and pass 'payload' as a parameter
    response = method_to_call(**payload)
    return response

This function takes your object (sf), the name of the method to call as a string (action_cmd), and a dictionary of parameters to pass to the method (payload). It uses getattr() to get the method from sf, then calls it using the parameters in payload.

You could use this function like this:

payload = {'maxResults': 2}
action_cmd = 'list_state_machines'
response = dynamic_call(sf, action_cmd, payload)

Here, payload is a dictionary where the keys are the names of the parameters for the method you want to call and the values are the values for those parameters.

This will work as long as the methods you're trying to call always take their parameters as keyword arguments. If some methods require positional arguments, you'll need a more complex solution.

Rapidmod
  • 382
  • 1
  • 6