Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Created January 10, 2026 17:48
Show Gist options
  • Select an option

  • Save sebastianknopf/0928ec954a98ec2feb17059b858b9460 to your computer and use it in GitHub Desktop.

Select an option

Save sebastianknopf/0928ec954a98ec2feb17059b858b9460 to your computer and use it in GitHub Desktop.
dependency free configuration utility for python

This configuraiton utility has three main purposes...

  1. Validate the given configuration for having all required keys
  2. Setting defined default values for optional keys
  3. Putting everything into the Configuration class

After running Configuration.apply_config(...), you simply can access your configs with Configuration.this.is.a.required.key or Configuration.this.is.an.optional.key.

class _ConfigNamespace:
def __init__(self, **entries):
for key, value in entries.items():
setattr(self, key, value)
def __repr__(self):
return f"<Config {self.__dict__}>"
class Configuration:
@classmethod
def apply_config(cls, config: dict) -> None:
# define required config keys
required_config: list[tuple[str]] = [
('this', 'is', 'a', 'required', 'key'),
]
# define default config values
# some of them are added as comment for documentation
default_config = {
'this': {
'is': {
'an': {
'optional': {
'key': True
}
}
}
}
}
cls._validate_required(required_config, config)
config = cls._merge_config(default_config, config)
namespace = cls._dict_to_namespace(config)
for key, value in namespace.__dict__.items():
setattr(cls, key, value)
@classmethod
def _merge_config(cls, defaults: dict, actual: dict) -> dict:
if isinstance(defaults, dict) and isinstance(actual, dict):
return {
k: cls._merge_config(
defaults[k] if k in defaults else {},
actual[k] if k in actual else {}
)
for k in set(defaults) | set(actual)
}
return actual if actual is not None else defaults
@classmethod
def _validate_required(cls, required: list[tuple[str]], config: dict):
for path in required:
current = config
for key in path:
if key not in current:
raise ValueError(f"Missing required config key: {'.'.join(path)}")
current = current[key]
@classmethod
def _dict_to_namespace(cls, data: dict) -> _ConfigNamespace:
if isinstance(data, dict):
return _ConfigNamespace(
**{k: cls._dict_to_namespace(v) for k, v in data.items()}
)
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment