*args & **kwargs python arguments

33 views
Skip to first unread message

Romain Bodson

unread,
May 8, 2025, 2:56:56 AMMay 8
to xlSlim Support
Hi,

I have a question about the utilisation of *args and **kwargs argument in excel from a python function. I try to use a function that returns a value from a dict where I can have more than 1 key.

I have this python function :
def get_value(material, **keys):
    global catalogue_data
    try:
        if catalogue_data is None:
            return "Catalogue non initialisé"
       
        value = catalogue_data[material]
        for key in keys:
            value = value[keys[key]]
        return str(value)
    except Exception as e:
        return f"Erreur: {e}"

in excel i use this formula for exemple :
=get_value("C50";"E 0,mean")
OR
=get_value(D7;E7) where D7 = "C50" and E7 = "E 0,mean"
it works very well !

When i tried to use a second 'keys' arguments it doesn't works, i tried :
=get_value("C50";"Classe de service 1";"k def")
or
=get_value("C50";"Classe de service 1""k def")
or
=get_value("C50";["Classe de service 1""k def"])

Do you thinks it's possible to use *args and **kwargs arguments in excel ?

thanks for your help,
BODSON Romain

xlSlim Dev

unread,
May 8, 2025, 3:04:31 AMMay 8
to xlSlim Support
Hi Romain,

Good to hear from you.

This is an interesting question. I will investigate and get back to you in a few days.

All the best,
Russel

xlSlim Dev

unread,
May 9, 2025, 4:21:05 AMMay 9
to xlSlim Support
Hi,

I believe this Python code will do what you are looking for:

"""
catalogue_data = {
    "C50": {
        "E 0, mean":16,
        "G mean": 1,
        "F mk": 50},
    "C45": {
        "E 0, mean":15,
        "G mean": 0.94,
        "F mk": 45}
        }

def get_values(material, keys):
    global catalogue_data

    if catalogue_data is None:
        return "Catalogue non initialisé"
    material_values = catalogue_data.get(material, {})
    values = []
    for key in keys:
        values.append([key, material_values.get(key)])
    return values
"""

This function can then be called from Excel by passing a range of cells as the keys argument.

Screenshot 2025-05-09 091829.png
You can also call it like this:
Screenshot 2025-05-09 092015.png

Hope that helps.

Regards,
Russel

Romain Bodson

unread,
May 12, 2025, 2:49:17 AMMay 12
to xlSlim Support
Hi Russel !
Thanks for your reply. 

I tested this and it works well! but it's not quite what I'm looking for yet, if I have a dictionary like this one :

"""
catalogue_data = {
    "C50": {
        "E 0, mean":16,
        "G mean": 1,
        "F mk": 50,
        " Classe 1": {
                "K mod" : 1.0 ,
                "K mod 2" : 1.5}
        " Classe 2": {
                "K mod" : 1.1 ,
                "K mod 2" : 1.6}
       " kdef" : 1.0}
        }

def get_value(material, *keys):

    global catalogue_data
    try:
        if catalogue_data is None:
            return "Catalogue non initialisé"
       
        value = catalogue_data[material]
        for key in keys:
            value = value[key]

        return str(value)
    except Exception as e:
        return f"Erreur: {e}"
"""
the idea behind this function is to be able to give the materials and key(s) associated with a value, like :

16 = get_value("C50", "E 0, mean") (in this case, ONE key arguments )
1.0 = get_value("C50", " Classe 1", "k mod 1") (in this case, TWO keys arguments are needed)

if i use the python function only, it works very well.

i hope that my explication are clear ?


thank you a lot,
Romain

xlSlim Dev

unread,
May 13, 2025, 1:52:20 AMMay 13
to Romain Bodson, xlSlim Support
I understand, let me see what I can do.

--
You received this message because you are subscribed to the Google Groups "xlSlim Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xlslim-suppor...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/xlslim-support/b080882c-c74a-47c3-a2c4-08c4d6ed4fd4n%40googlegroups.com.

xlSlim Dev

unread,
May 14, 2025, 3:57:14 AMMay 14
to Romain Bodson, xlSlim Support
Hi,

You'll need a little wrapper function to handle the translation from Excel ranges to *keys.

def wrapper(material, keys):
    if isinstance(keys, str):
        return get_value(material, keys)
    else:
        return get_value(material, *keys)

This can then be called passing a range of cells as the keys arguments.

Screenshot 2025-05-14 085451.png

Screenshot 2025-05-14 085556.png

Regards,
Russel

Romain Bodson

unread,
May 14, 2025, 7:18:31 AMMay 14
to xlSlim Support
hi,

thanks a lot for these informations. It will be solve a lot of my problems :)

But there is any issue to have the formula =wrapped("C50", "Classe 1", "k mod") ; with 3 argument and not a range of argument ? In excel the number of arguments is fixed ? we cannot have a variable number of argument for the same function ?

have a good day,

Romain

xlSlim Dev

unread,
May 15, 2025, 7:01:54 AMMay 15
to xlSlim Support
Hi,

It is not possible, the internals of mapping Excel to Python don't allow for variable numbers of arguments.

You can use CreateRange() if you don't want to use real ranges on your sheet, =wrapped("C50", CreateRange("Classe 1", "k mod"))

Regards,
Russel

xlSlim Dev

unread,
May 15, 2025, 11:34:38 AMMay 15
to xlSlim Support
Hi,
You can of course achieve similar results using optional arguments https://russelwebber.github.io/xlslim-docs/html/user/keyword_args.html
Regards,
Russel
Reply all
Reply to author
Forward
0 new messages