Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 892
James Avatar asked Sep 25 '22 07:09

James


2 Answers

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.

like image 52
notorious.no Avatar answered Nov 15 '22 08:11

notorious.no


So, I ran across you question and I thought this might help. PyYAML is awesome! One of the things it supports is class mapping directly from python

import yaml
class Hero:
    def __init__(self, name, hp, sp):
        self.name = name
        self.hp = hp
        self.sp = sp
    def __repr__(self):
        return "Hero %s has %d Hitpoints" % (self.name, self.hp)


f = "sample.yml"
with open(f, mode='w') as ff:
    ff.write(yaml.dump(Hero(name="Cool Guy", hp=4, sp=10)))

    # saves as !!python/object:__main__.Hero {hp: 4, name: Cool Guy, sp: 10}

    ff.close()
with open(f, mode="r") as fx:
    y = yaml.load(fx)
    print(y)

[out]: Hero Cool Guy has 4 Hitpoints

Hope you find as much use out of this as I did. Check out the PyYAML Docs for more information.

like image 24
Dan Temkin Avatar answered Nov 15 '22 08:11

Dan Temkin