Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Python functions from YAML file

Tags:

python

yaml

I have a YAML script where the users can define some parameters. They can also state in the YAML file a custom function they want to apply. For example:

processes:
    args1: 10
    args2: 5
    funct: np.sum

This YAML will be passed to a function like this:

def custom_func(args1, args2, funct):
    return funct(args1, args2)

With the YAML example above, I expect custom_func() to execute np.sum(10,5). How can I make the data in YAML file to be parsed as Callable? eval() probably does the job but it might have security issue. Is there any proper way? Thanks!

like image 271
eng2019 Avatar asked May 02 '26 01:05

eng2019


1 Answers

You can also load the function given its Python dotted path.

However, this only gets the function and calls it, it does not try to figure out calling signature, which is np.sum([5,10]), not np.sum(10,5)

from yaml import safe_load as yload
from importlib import import_module

def load_func(dotpath : str):
    """ load function in module.  function is right-most segment """
    module_, func = dotpath.rsplit(".", maxsplit=1)
    m = import_module(module_)
    return getattr(m, func)
    
yaml = """
processes:
    args1: 10
    args2: 5

    #gotta give real package name, not `np`
    funct: numpy.sum
"""

di = yload(yaml)
pr = di["processes"]
func = load_func(pr["funct"])
array = [pr["args1"], pr["args2"]]

print (f"{func=} {func(array)=}")

output:

func=<function sum at 0x106645e50> func(array)=15
like image 69
JL Peyret Avatar answered May 04 '26 13:05

JL Peyret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!