Writing a YAML Config Loader in Python (with Environment Overrides)

If you’re building a Python application that requires configuration data, you’ll want a flexible and reliable way to store and load that data.

Sure, you can use dotenv but it’s sometimes a bit cumbersome… So let’s see how to write a custom YAML config loader in Python using the yaml module.

Key Takeaways

  • A YAML file can be a convenient way to store configuration data for a Python application.
  • You can use Python’s yaml module to load and parse a YAML file.
  • You can write a Python class that loads configuration data from a YAML file and returns values using a method.

Loading Configuration Data from a YAML File

To load configuration data from a YAML file, we can use Python’s yaml module. Here’s an example YAML file that stores some default configuration parameters:

default:
  param1: 'default_value1'
  param2: 'default_value2'

We can then write a Python class called YamlConfigLoader that loads this YAML file and returns parameter values using a get_param method:

import yaml

class YamlConfigLoader:
    def __init__(self, file_path, env=None):
        self.file_path = file_path
        self.env = env
        self.data = self.load_data()

    def load_data(self):
        with open(self.file_path) as f:
            data = yaml.load(f, Loader=yaml.FullLoader)
        return data

    def get_param(self, param):
        try:
            params = {**self.data.get('default', {}), **self.data[self.env]}
        except KeyError:
            try:
                params = self.data['default']
            except KeyError:
                raise ValueError("Default config parameters not found in the config file.")

        if param not in params:
            raise ValueError(f"Param '{param}' not found in the config file.")

        return params[param]

The YamlConfigLoader class takes two arguments: file_path, which is the path to the YAML file, and env, which is the name of the environment to load parameters for.

The load_data method loads the YAML file using yaml.load, and the get_param method retrieves the parameter value for a given parameter name.

Using the YamlConfigLoader Class

To use the YamlConfigLoader class, we can create an instance of the class and call the get_param method with a parameter name. Here’s an example:

config = YamlConfigLoader('config.yaml', 'dev')
print(config.get_param('param1'))  # prints the value of 'param1' in the dev environment, or the default value if not present

config = YamlConfigLoader('config.yaml')
print(config.get_param('param2'))  # prints the value of 'param2' in the default environment, or raises an error if not present

Assuming the config.yaml file contains the following data:

default:
  param1: 'default_value1'
  param2: 'default_value2'
dev:
  param1: 'dev_value1'
prod:
  param1: 'prod_value1'
  param2: 'prod_value2'

The first print statement will output dev_value1, and the second print statement will output default_value2.

Happy configuring!

(I don’t know about you, but configuring makes me happy.)