-1

I am searching for a tip/technique to dynamically build function argument names when calling a method:

I have a function which is using dynamic arguments that later are posted to a Webservice using http:

def create_case(**fields):
...

Currently, I call the function like this:

create_case(field54=first_name,
            field_1003=last_name,
            field_948=street)

Since I have multiple instances of the Webservices, which have different field ID's I try to put those argument names into a configuration file and build them dynamically. All my current tries were not successful and I ran out of ideas on how to approach this.

What I tried:

config.py:

FIELD_FIRST_NAME=54
FIELD_LAST_NAME=1003
FIELD_STREET=948

client.py:

create_case(field_+config.FIELD_FIRST_NAME=first_name,
            field_+config.FIELD_LAST_NAME=last_name,
            field_+config.FIELD_STREET=street)

It seems, it's not possible to just concat the arguments together. Does anyone have a suggestion on how I could go on this ?

Best regards

Koren D
  • 41
  • 3

1 Answers1

2

You can create a dictionary of the arguments:

kwargs_dict = {
    "field_"+str(config.FIELD_FIRST_NAME): first_name,
    "field_"+str(config.FIELD_LAST_NAME): last_name,
    "field_"+str(config.FIELD_STREET): street
}

and then pass this to the function as:

create_case(**kwargs_dict)
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29