Skip to main content

PyPI Github Test

ConfigMyPy

Configure My Python: Easy configuration of Python projects.

Quickstart

Just clone the repository and install it, here in editable mode:

git clone https://github.com/JeanKossaifi/configmypy
cd configmypy
python -m pip install -e .

Then, assuming you have a configuration file config.yaml in your folder, get your configuration using

from configmypy import ConfigPipeline, YamlConfig, ArgparseConfig

pipe = ConfigPipeline([YamlConfig('./config.yaml'),
                        ArgparseConfig()])
config = pipe.read_conf()

Then just read your parameters from config:

arg = config.arg
arg2 = config['arg2']
...

The script will read config.yaml, as well as optional command-line arguments, which will overwrite the config.

Rationale

How many times did you start having a large Python project, let's say to run some Machine Learning models, where you wanted to change quickly the parameters. Then you want to track them. In you write a small logger. Then you write a small yaml config file to more easily manage the quickly growing list of parameters. Then you realize you sometimes need to change some of these parameters on the fly. In comes argparse, and you manually override an increasingly long list of paramters with a code becoming a long list of

if args.parameter is not None:
    config['parameter'] = args.parameter

...and there goes your sanity... Along with any hope of properly managing and tracking your experiments.

To address this frustration, I wrote this minimal package that makes this easy.

Reading from a .yaml config file

Imagine you have a config.yaml file that looks like this:

default:
  opt:
    optimizer: 'adam'
    lr: 0.1
  data:
    dataset: 'ns'
    batch_size: 12
    
test:
  opt:
    optimizer: 'SGD'

You can read that using configmypy.YamlConfig:

reader = YamlConfig('./config.yaml', config_name='default')
config, _ = reader.read_conf()

Bunch configurations

All our configurations return a Bunch, not a dict. A Bunch is simply a dictionary that exposes its parameters as attributes so you can access them, equivalently, as config['param'] or config.param.

Configuration Pipeline

The real power of configmypy comes from the ConfigPipeline: you can have several steps called sequentially. A typical usecase would be:

  1. Read a default (.yaml) config
  2. Optionally overwrite any of those parameters using argparse (command-line arguments)
  3. If the user specified another configuration, update the parameters read so far with that new config

This would look like this with configmypy:

pipe = ConfigPipeline([YamlConfig('./config.yaml', config_name='default'),
                        ArgparseConfig(config_file=None, config_name=None),
                        YamlConfig()])
config = pipe.read_conf()

This will:

  1. first read the default section in config.yaml
  2. update any parameters with those passed by the user, including additional parameters config_file and config_name which are automatically passed to the next step
  3. If the user specified a config_file, that will be read by the next step and used to update the configuration.

You can check the configuration by calling pipe.log():

>>> pipe.log()
###############################
#####    CONFIGURATION    #####
###############################

Steps:
------
 (1) YamlConfig with config_file=./config.yaml, config_name=default
 (2) ArgparseConfig with additional config={'config_file': None, 'config_name': None}
 (3) YamlConfig with config_file=config.yaml, config_name=test

-------------------------------

Configuration:
--------------

opt.optimizer=SGD
opt.lr=0.1
data.dataset=ns
data.batch_size=24

###############################

Questions or issues

This is very much a project in development that I wrote for myself and decided to open-source so myself and others could easily reuse it for multiple projects, while knowing it is actually tested!

If you have any questions or find any bugs, please open an issue, or better yet, a pull-request!

Metadata

Release files for configmypy 0.2.0

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

Source distribution (sdist)

Source distribution for configmypy 0.2.0
File Size Uploaded
configmypy-0.2.0.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for configmypy 0.2.0
File Interpreter ABI Platform
configmypy-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / configmypy-0.2.0.tar.gz

Download URL configmypy-0.2.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a517921d18656844dae6831665cbf7718f153876b9cbc526f8bca6a6fb8cd28a
BLAKE2b-256 checksum
How to use checksums
e22fdf2bd442d1767a879da63091394b0dc89a5800da84cd9fb36ecb818bfcfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.4

Release files / configmypy-0.2.0-py3-none-any.whl

Download URL configmypy-0.2.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c50e998edc052f3d3f10659338e28631cf3962a60778a55d318edcc4cebf2cfb
BLAKE2b-256 checksum
How to use checksums
0d22d1d35a8d667592ac59b57e214ce92abd1019437f00e8362fef17d99eca2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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