Search code examples
pythonpython-3.xpyyaml

Input YAML config values into keyword arguments for Python


I'd like to have a YAML config file for certain analysis settings for my Python script. The YAML file looks something like this:

settings:
  param1: 1
  param2: two
  ...

In my Python script, I want to pass these values as keyword arguments to a function. Loading in the YAML file gives me a dictionary. The function only has keyword arguments and I don't know beforehand which keyword arguments the config file will have. The function looks something like this:

def process(param1=None, param2=None, param3=None):
    ...

Note that the name of the YAML settings are the same as the keyword argument names. The function has several possible keyword arguments, and the config file may only have one or two. Obviously, I want to assign the values to the right keyword arguments.

How can I assign YAML values (and/or a dict) to their keyword argument assignments?


Solution

  • You can get the dictionary from yaml.load() and use the ** syntax to pass the key/values into a function. This way you don't have to know the keys beforehand. For example:

    import yaml
    
    with open('path/to/config.yml', 'rb') as f:
        conf = yaml.safe_load(f.read())    # load the config file
    
    def process(**params):    # pass in variable numbers of args
        for key, value in params.items():
            print('%s: %s' % (key, value))
    
    process(**conf)    # pass in your keyword args
    

    Try this out with your code and it should give you enough insight into how to implement your own.