Search code examples
pythonyamlpyyaml

Appropriate ways to read config values in a YAML file


My configuration for a python program is stored in a yaml file;

config = yaml.load('/some/folder/path/myconfig.yaml'))

I load all my config values early in my python script in one area near the beginning

infldr = config['myapp1']['inputFolder']
outfldr = config['myapp1']['outputFolder']

In later sections I refer to the variables that I copied the yaml config values into;

Original method

filename = 'blahblah.blah'
copyfile(infldr + filename, outfldr + filename)

It works fine but I was wondering, would it be more "pythonic" or more efficient to just refer to the yaml values directly;

Possible New method

copyfile(config['myapp1']['inputFolder'] + filename, 
         config['myapp1']['outputFolder'] + filename)

If I am looping around all the files in the folder which is better? What is more pythonic? When is it proper to use the orriginal method vs the new method? I suspect python yaml library is parsing the values each time to get to it and it's more efficient to copy the value to a variable once. Am I right or wrong?


Solution

  • Short answer: in my opinion, this is largely personal preference.

    Slightly longer answer: a common Python aphorism is "simple is better than complex". Whatever is the most readable and the easiest to understand is the best solution.

    As far as your concerns about parsing go, the config object obtained via yaml.load is only instantiated once. The config dictionary is not reloaded from file when you grab from it again.

    Final note: if you edit your variables in place, you could end up changing your original config object. This is particularly easy to do with nested dictionaries such as that which is produced by yaml.load. It's worth noting that 'variables' in Python are actually just pointer references with some syntactic sugar -- be wary of this, and copy objects when necessary to avoid overwriting the original.