Skip to main content

Config Object

Motivation

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 config.py 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 config.py 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
my:
  nested:
    - param1
    - param2

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

# run.py
import one_config
one_config.build_config().from_yaml('path/to/my_config.yaml')

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

# my_module.py
import one_config
CONFIG = one_config.get_config()
param1 = CONFIG.my.nested.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:

# run.py
import one_config
one_config.build_config('private').from_yaml('private_env.yaml')
# my_module.py
import one_config
PRIVATE_ENV = one_config.get_config('private')
key = PRIVATE_ENV.my.super.secret.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.

Metadata

Release files for one-config 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for one-config 0.2.1
File Size Uploaded Provenance
one-config-0.2.1.tar.gz 3.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for one-config 0.2.1
File Interpreter ABI Platform
one_config-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.5 kB

Release files / one-config-0.2.1.tar.gz

Download URL one-config-0.2.1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5d444716d96d307704729dcfd7b2e1439a10c4010c88ad9acf399e10d7241cff
BLAKE2b-256 checksum
How to use checksums
0786776624386abdd03423af85de142485f15272d8c456b887e5915b74ddd85b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

Release files / one_config-0.2.1-py3-none-any.whl

Download URL one_config-0.2.1-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69148eb0aa46885c3304e6a90137e2c663eca666224d18ea3f7d34375e6f8f12
BLAKE2b-256 checksum
How to use checksums
891f3d7028906154066c7af503e5f90741a3980d8f4ecd25bd8df3c94aafab42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

Security for one-config 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page