Skip to main content

Config2Code simplifies configuration management by automatically generating Python dataclasses from YAML or JSON files. This tool enhances code readability, maintainability, and type safety, saving you time and effort.

Project description

Config2Code: A Tool to Generate Python Dataclasses from Configuration Files

PyPI - Version PyPI - License PyPI - Python Version Coverage Status PyPI - Downloads

Introduction

Config2Code is a Python tool designed to streamline the process of converting configuration files (YAML or JSON or TOML) into Python dataclasses. By automating the generation of dataclasses, you can improve code readability, maintainability, and type safety.

Installation

You can install Config2Code using pip:

pip install config2code

Usage

Basic Example

  1. Prepare your configuration file: Create a YAML or JSON file containing your configuration data. Here's an example YAML file:

    DatabaseConfig:
      host: localhost
      port: 5432
      user: myuser
      password: mypassword
      secret: {{database.password}}
    
  2. Run the tool: Use the config2code command-line interface to convert the configuration file:

    config2code to-code --input input.yaml --output output.py
    

    This will generate a Python file output.py containing a dataclass representing the configuration:

    from dataclasses import dataclass
    
    @dataclass
    class DatabaseConfig:
        host: str
        port: int
        user: str
        password: str
        secret: str
    

Placeholder Example

Sometimes you put redundant data in your config file because it is more convenient to only move parts of the config further down the road. Examples could be a machine learning pipeline where you have parameters for your dataset and model which can have redundant values. To counter the problem of always changing multiple values at once in your config we introduce placeholder. A placeholder is a path packed into a token {{<path-in-config>}} which points to a value you want to insert automatically into your loaded config file. This path starts always at the yaml root and ends at the value to insert.

   pipeline:
      dataset: 
         x_dim: 42
         y_dim: 5
         batch_size: 128
         shuffle: True
      model:
         input_dim: {{pipeline.dataset.x_dim}}
         output_dim: {{pipeline.dataset.y_dim}}
         activation_func: ReLU
         learning_rate: 0.0001

In the case of not having a yaml root you can still use the placeholder with a leading . inside the token.

   dataset: 
      x_dim: 42
      y_dim: 5
      batch_size: 128
      shuffle: True
   model:
      input_dim: {{.dataset.x_dim}}
      output_dim: {{.dataset.y_dim}}
      activation_func: ReLU
      learning_rate: 0.0001

Service

This service monitors the requested configuration file. If the services detects changes in the file it will automatically write those changes into the specified output.py.
You can start the service for example with:

config2code service-start --input input.yaml --output output.py

To stop it you can stop all with

config2code stop-all

Use Config in Code

After you created your python config you can easily use as follows:

from output import DatabaseConfig

config = DatabaseConfig.from_file("input.yaml")
# access config field with dot operator
config.host

Key Features

  • Supports YAML, JSON and TOML: Easily convert both formats.
  • Automatic dataclass generation: Generates well-structured dataclasses.
  • Nested configuration support: Handles nested structures in your configuration files.
  • Type inference: Infers types for fields based on their values.
  • Placeholder: Choose which values in your config file are dependent on others

Additional Considerations

  • Complex data structures: For more complex data structures, consider using custom type hints or additional configuration options.
  • Error handling: The tool includes basic error handling for file loading and parsing.
  • Future enhancements: We plan to add support for additional file formats, advanced type inference, and more customization options.

Features to expand

  • add VS Code extension (create new file on config file save)
  • add renaming feature from config to code (renaming a field in the config file should resolve in renaming a field in the code
  • add token in config yaml to overwrite field automatically with a dependency on another field (something like <c2c/2*:dep.config.a/c2c>) or costum functions <c2c/module.submodule:func(some_value)/c2c>

Contributing

We welcome contributions to improve Config2Code. Feel free to fork the repository, make changes, and submit a pull request.

License

This project is licensed under the MIT License.

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

config2class-0.2.3.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

config2class-0.2.3-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file config2class-0.2.3.tar.gz.

File metadata

  • Download URL: config2class-0.2.3.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.8.0-48-generic

File hashes

Hashes for config2class-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4b9bfa1547b2f22d994df0bf802eb178d59af8b7e46f6af10fbff069701dd7e8
MD5 600f994ade976f134a0bde01566e3a77
BLAKE2b-256 c702a81837eef7bd8cd1c74926ac05c893ec468b2c4a60270ad75eff5ab8048a

See more details on using hashes here.

File details

Details for the file config2class-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: config2class-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.8.0-48-generic

File hashes

Hashes for config2class-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dc4890cef70d5908e44eb64799c2f7171a0ee7a894343c3988efaf899666369b
MD5 fbfa1a5d7c4c1b75f67b55c18da0fd6b
BLAKE2b-256 68319795bf64a308802db52f2f2c8ab1355f8bc470350b0e980d1e484a1fa528

See more details on using hashes here.

Supported by

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