Skip to main content

A universally accessible config object that only has to be constructed once

Project description

Config Object


It's common for ML projects to use a large configuration object with all parameters in one place. There are two broad requirements of a configuration object:

  1. It should be able to easily represent the parameters of different runtime situations, such as testing and production.
  2. It should be available for all modules

In order to achieve the first requirement, the object should be somehow constructed (and done so only once in the codebase).

In order to achieve the second requirement, we could have a file with all configuration parameters as attributes. This could be imported into other modules and used ad hoc.

However, in order to achieve both requirements, we would seemingly have to construct the config object once and inject the constructed config into all other objects in the codebase. This clutters the codebase.

The one_config module attempts to resolve this problem by using the singleton pattern.

A one_config object is constructed once and only once by a method parametrized with the path to a yaml file. Once constructed, the object will contain all parameters even if accessed by another module.

Although the code in may be opaque to many readers (and others might disagree about how "pythonic" it is), its implementation should be straightforward and it should be very useful for maintaining clean code in any project.

How to use it

Create a .yaml file in an appropriate place and add some parameters:

# my_config.yaml
    - param1
    - param2

Then, at the entry-point(s) of your python package, initialize the CONFIG object with the path to the .yaml file:

import one_config

In any other module, you can import the CONFIG object and the parameters from the yaml file will be present.

import one_config
CONFIG = one_config.get_config()
param1 =

Notice how the nested parameters are accessed as nested attributes.

Named configs

It is also possible to name a config in order to have multiple config environments simultaneously:

import one_config
import one_config
PRIVATE_ENV = one_config.get_config('private')
key =

A note on the Singleton Pattern

The singleton pattern is one of the more controversial design patterns. It is often ridiculed and seen as the "bad" pattern of the Gang of Four. On stack oveflow, the consensus seems to be that the singleton is either an anti-pattern or is useless in a language like python.

In its original form, the Singleton might be all of those things. However, if we expand our definition of singleton to include any object whose state will be the same everywhere in the entire code base, we notice that we encounter singletons all the time in python. A python module is a singleton.

Our singleton is simply an extension of the typical module singeton that allows us to easily add attributes based on an external file.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for one-config, version 0.2.1
Filename, size File type Python version Upload date Hashes
Filename, size one_config-0.2.1-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size one-config-0.2.1.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page