Skip to main content

No project description provided

Project description

Config

PyPI - Python Version Codecov Downloads Build Status License: MIT Twitter: PetrochukM

This package allows you to configure functions explicitly and safely. You will be able to create an intuitive type-checked configuration file that directly sets function arguments, globally.

This is a lightweight package with only two widely used dependencies and only a couple hundred line of code.

Contents

Install

Make sure you have Python 3, then you can install pythonic-config using pip:

pip install pythonic-config

Install the latest code via:

pip install git+https://github.com/PetrochukM/Config.git

Usage 🤗

Configuring a function

Any function can be configured, and then used anywhere, see below:

import config

# Define function
def do_something_cool(how_many_times: int):
    pass

# Configure function
config.add({do_something_cool: config.Args(how_many_times=5)})

# Use the configured function anywhere! 🎉
do_something_cool(how_many_times=config.get())

This approach is simple but powerful. Now, each configuration can be directly attributed to a documented function argument.

Furthermore, config incorporates typeguard 💂‍♀️ so every configuration is type checked at runtime.

Writing a configuration file

The simple example above can be extended to create a configuration file, for example:

import config
import data
import train

config.add({
  data.get_data: config.Args(
      train_data_path="url_lists/all_train.txt",
      val_data_path="url_lists/all_val.txt"
  ),
  data.dataset_reader: config.Args(
      type_="cnn_dm",
      source_max_tokens=1022,
      target_max_tokens=54,
  ),
  train.make_model: config.Args(type_="bart"),
  train.Trainer.make_optimizer: config.Args(
      type_="huggingface_adamw",
      lr=3e-5,
      correct_bias=True
  )
  train.Trainer.__init__: config.Args(
      num_epochs=3,
      learning_rate_scheduler="polynomial_decay",
      grad_norm=1.0,
  )
})

With this approach, this configuration file will make it clear which (hyper)parameters are set and where. This improves overall readability of the configuration file.

🐍 Last but not least, the configuration file is written in Python, you can use variables, lambdas, etc to further modularize.

Configuring via the command line

In case you want to change one variable at a time, this package supports configuration from the command line, for example:

python example.py --sorted='Args(reverse=True)'
import sys
import config

config.add(config.parse_cli_args(sys.argv[1:]))

Logging the configuration

Lastly, it's useful to track the configuration file by logging it. This package supports that via config.log. In the example below, we log the configuration to Comet.

from comet_ml import Experiment
import config

experiment = Experiment()
experiment.log_parameters(config.log())

Advanced: Sharing configurations between processes

In multiprocessing, it may be useful to share the configuration file between processes. In this case, the configuration can be exported to another process and then subsequently imported, see below:

from multiprocessing import Process
import config

def handler(configs: config.Config):
    config.add(configs)

if __name__ == "__main__":
    process = Process(target=handler, args=(config.export(),))
    process.start()
    process.join()

Advanced: Ensuring the configuration is used

In a large code base, it might be hard to tell if the configuration has been set for every function call. In this case, we've exposed config.profile which can double check every function call against the configuration, see below:

import sys
import config

def configured(a=111):
    pass

sys.setprofile(config.profile)
config.add({configured: config.Args(a=1)})

configured()  # `config.profile` issues a WARNING!
configured(a=config.get())

How does this work?

Our approach is simple, config maintains a global configuration mapping each function to its associated arguments. When a user calls config.get, it'll attempt to parse the code to determine the calling function and associated argument. In most cases, this will be successful and it will fetch the appropriate value. If it's not successful, it'll raise an error that'll help you fix the issue.

Project details


Download files

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

Source Distribution

pythonic-config-0.4.7.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pythonic_config-0.4.7-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file pythonic-config-0.4.7.tar.gz.

File metadata

  • Download URL: pythonic-config-0.4.7.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.8

File hashes

Hashes for pythonic-config-0.4.7.tar.gz
Algorithm Hash digest
SHA256 2c21f3a7b741d570df4c1c119a647f202340c251be4e07e19ec833162cb49fd3
MD5 c324d3195b8ac5eea19416c5c0a6b771
BLAKE2b-256 72d06895f55cec0ced983acaadefcd247b9625221310fba07fc35002da935b93

See more details on using hashes here.

File details

Details for the file pythonic_config-0.4.7-py3-none-any.whl.

File metadata

  • Download URL: pythonic_config-0.4.7-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.8

File hashes

Hashes for pythonic_config-0.4.7-py3-none-any.whl
Algorithm Hash digest
SHA256 717576ad9e9d35e5b26dad7d056ac82d6854fa8fcfe0b4f41f0145cc145d26ba
MD5 4cc85d94cf9ee5d7d2d0fa8675efc1c9
BLAKE2b-256 8bd0825daf4bf51bf2c281cef8a84379e0743bf04c6e02ebab616d29ee2951e0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page