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

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

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

    database:
      host: localhost
      port: 5432
      user: myuser
      password: mypassword
    
  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
    

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.
  • Customizable output: Control the output file name and code formatting.

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.1.2.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

config2class-0.1.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: config2class-0.1.2.tar.gz
  • Upload date:
  • Size: 7.4 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.1.2.tar.gz
Algorithm Hash digest
SHA256 d690d76846f9495f9a04bcf37c4f1090d6ae92178add85615bc581ec94327b89
MD5 0eee4348d659101ccfe7511a2d489749
BLAKE2b-256 b164b2baddf0d49f040ec752e39e48061609a30689e9e080689c083d3b971c2e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: config2class-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.1 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b9ad9b4a6c4a2021cec5dc09e15e578769eb1f723f8a18a578a740bb898e38b4
MD5 cb4fcce6c4a77ec41b4035fe80f93400
BLAKE2b-256 4cb7604a34595ce665de90fbb1c2fee7654443a792f2638f934be726f450634b

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