Loading configs in Python

Motivation

Simple solution for loading.

Possible extensions:

import yaml 
import json
from types import SimpleNamespace

def load_object(dct):
    return SimpleNamespace(**dct)

def load_config(path):
    """
    Loads the configuration yaml and parses it into an object with dot access.
    """
    with open(path, encoding="utf-8") as stream:
        # Load dict
        config_dict = yaml.safe_load(stream)

        # Convert to namespace (access via config.data etc)
        config = json.loads(json.dumps(config_dict), object_hook=load_object)
    return config, config_dict

For typing one can use the following construction:

from dataclasses import dataclass

@dataclass
class MyModelConfig: 
	module: models.mymodel
	hidden_size: int 



class MyModel: 
	def __init__(self, config: MyModelConfig):
		# Model def goes here.
		# Typing allows autocomplete.
		pass 
	
	...

References in yaml

  experimentconfig: &id-exp
  name: MyCoolExperiment

model: 
  module: models.mymodel
  hidden_size: 10 
  expconfig: *id-exp

data: 
  module: data.mydata
  root: ./data 
  expconfig: *id-exp

Saving configs

← Back to Wiki