Home > Enterprise >  Passing tuples with default arguments to a function in Python?
Passing tuples with default arguments to a function in Python?

Time:09-12

I have to call a function by passing predefined parameters from a huge list of tuples.

All function parameters are default type and they change their length and type according to CATEGORY parameter (please, see in the example code below to better understand what I want to say).

LIST_ALL_BETA = [1, 2, 3, 4, 5, 7, 23, 28, 29] # huge list with numbers, all integers
LIST_ALL_FREQ = [1.23, 1.30, 1.35, 2.18, 2.57] # huge list of floats
def MyFunction(idx=None, CATEGORY=None, LOW_LIMIT=None, HIGH_LIMIT=None, ALL_BETA=None, ALL_FREQ=None, NONE=None, LAST_X=None, LST=None):
    if CATEGORY=="COUNTED":
        partial_COUNTED_lst = globals()[f'LIST_COUNTED_LAST_{LAST_X}']
        return [x for x in range(1, 81) if partial_COUNTED_lst[idx][x-1] is not None if LOW_LIMIT <= partial_COUNTED_lst[idx][x-1] <= HIGH_LIMIT]
    elif CATEGORY=="BETA":
        if ALL_BETA==True:
            if NONE == "ignored":
                all_BETA_lst = LIST_ALL_BETA
                return [x for x in range(1, 81) if all_BETA_lst[idx][x-1] is not None if LOW_LIMIT <= all_BETA_lst[idx][x-1] <= HIGH_LIMIT]
            else: # NONE = 0
                all_BETA_lst = LIST_ALL_BETA
                return [x for x in range(1, 81) if LOW_LIMIT <= [y if y is not None else 0 for y in all_BETA_lst[idx]][x-1] <= HIGH_LIMIT]
        else:
            if NONE == "ignored":
                all_BETA_lst = globals()[f'LIST_BETA_LAST_{LAST_X}']
                return [x for x in range(1, 81) if all_BETA_lst[idx][x-1] is not None if LOW_LIMIT <= all_BETA_lst[idx][x-1] <= HIGH_LIMIT]
            else: # NONE = 0
                all_BETA_lst = globals()[f'LIST_BETA_LAST_{LAST_X}']
                return [x for x in range(1, 81) if LOW_LIMIT <= [y if y is not None else 0 for y in all_BETA_lst[idx]][x-1] <= HIGH_LIMIT]
    elif CATEGORY=="FREQUENCY":
        if ALL_FREQ==True:
            all_frequency_lst = LIST_ALL_FREQ
            return  [x for x in range(1, 81) if all_frequency_lst[idx][x-1] is not None if LOW_LIMIT <= all_frequency_lst[idx][x-1] <= HIGH_LIMIT]
        else:
            all_frequency_lst = globals()[f'LIST_FREQ_LAST_{LAST_X}']
            return  [x for x in range(1, 81) if all_frequency_lst[idx][x-1] is not None if LOW_LIMIT <= all_frequency_lst[idx][x-1] <= HIGH_LIMIT]
    elif CATEGORY=="CMB4":
        return LST[idx]

# huge list of parameters:
parametters = [
    (idx=23, CATEGORY="COUNTED", LOW_LIMIT=265, HIGH_LIMIT=269, LAST_X=1000),
    (idx=23, CATEGORY="COUNTED", LOW_LIMIT=241, HIGH_LIMIT=272, LAST_X=1000),
    (idx=23, CATEGORY="COUNTED", LOW_LIMIT=16, HIGH_LIMIT=19, LAST_X=100),
    (idx=23, CATEGORY="BETA", LOW_LIMIT=1.55, HIGH_LIMIT=2.01, ALL_BETA=True, NONE="ignored", LAST_X=0),
    (idx=23, CATEGORY="BETA", LOW_LIMIT=1.55, HIGH_LIMIT=2.01, ALL_BETA=True, NONE="0", LAST_X=0),
    (idx=23, CATEGORY="BETA", LOW_LIMIT=0.0, HIGH_LIMIT=5.3, ALL_BETA=True, NONE="ignored", LAST_X=0),
    (idx=23, CATEGORY="FREQUENCY", LOW_LIMIT=0.2587, HIGH_LIMIT=0.2608, ALL_FREQ=True, LAST_X=0),
    (idx=23, CATEGORY="FREQUENCY", LOW_LIMIT=0.2478, HIGH_LIMIT=0.2607, ALL_FREQ=True, LAST_X=0),
    (idx=23, CATEGORY="FREQUENCY", LOW_LIMIT=0.259, HIGH_LIMIT=0.261, ALL_FREQ=True, LAST_X=0),
    (idx=23, CATEGORY="CMB4", LST = LIST_CMB4_LAST_1000_1),
    (idx=23, CATEGORY="CMB4", LST = LIST_CMB4_LAST_1000_2)
]

The problem is I can't create this kind of tuple because it is throwing me an "Invalid syntax error."

Any idea on how to do this, please?

Thank you in advance!

P.S. I have also found this topic which is somehow on about same problem but it doesn't help me because the problem is different and I don't really understand that situation involved so I could use the solution for this problem I'm asking about.

CodePudding user response:

Rather than trying to use tuples to call, you can use a list of dicts and used something called dictionary unpacking. If you add ** before the dict and pass it into a function call, it will unpack the key value pairs into the named parameters of the function.

Here is what your new parameter list will look like:

parameters = [
    {"idx": 23, "CATEGORY": "COUNTED", "LOW_LIMIT": 265, "HIGH_LIMIT": 269, "LAST_X": 1000},
    {"idx": 23, "CATEGORY": "COUNTED", "LOW_LIMIT": 241, "HIGH_LIMIT": 272, "LAST_X": 1000},
    {"idx": 23, "CATEGORY": "COUNTED", "LOW_LIMIT": 16, "HIGH_LIMIT": 19, "LAST_X": 100},
    {"idx": 23, "CATEGORY": "BETA", "LOW_LIMIT": 1.55, "HIGH_LIMIT": 2.01, "ALL_BETA": True, "NONE": "ignored", "LAST_X": 0},
    {"idx": 23, "CATEGORY": "BETA", "LOW_LIMIT": 1.55, "HIGH_LIMIT": 2.01, "ALL_BETA": True, "NONE": "0", "LAST_X": 0},
    {"idx": 23, "CATEGORY": "BETA", "LOW_LIMIT": 0.0, "HIGH_LIMIT": 5.3, "ALL_BETA": True, "NONE": "ignored", "LAST_X": 0},
    {"idx": 23, "CATEGORY": "FREQUENCY", "LOW_LIMIT": 0.2587, "HIGH_LIMIT": 0.2608, "ALL_FREQ": True, "LAST_X": 0},
    {"idx": 23, "CATEGORY": "FREQUENCY", "LOW_LIMIT": 0.2478, "HIGH_LIMIT": 0.2607, "ALL_FREQ": True, "LAST_X": 0},
    {"idx": 23, "CATEGORY": "FREQUENCY", "LOW_LIMIT": 0.259, "HIGH_LIMIT": 0.261, "ALL_FREQ": True, "LAST_X": 0},
    {"idx": 23, "CATEGORY": "CMB4", "LST": LIST_CMB4_LAST_1000_1},
    {"idx": 23, "CATEGORY": "CMB4", "LST": LIST_CMB4_LAST_1000_2}
]

and here is how you would call the function:

for parameter_dict in parameters:
    MyFunction(**parameter_dict)

1 additional feedback item: You should create a separate function per category IMO. Trying to cram all of this into one function will be hard to maintain and confusing. Maybe create a process_counted, process_beta, process_frequency, and process_cmb4 function. Then you can have some controlling logic somewhere that calls the separate function based on each category.

  • Related