Skip to main content

Code generator that produces a Python class from a YAML input file. Can be used to facilitate code completion for config objects.

Project description

PyPI test

yaml2pyclass Code Generator

This package enables you to easily generate and instantiate dataclasses from an input YAML file. For example this YAML file

image_size: [ 1280, 960 ]
cluster_config:
  eps: 0.02
  min_num_samples: 10
path_output: "./output"

automatically converts to a Python class

import dataclasses
import yaml2pyclass

class Config(yaml2pyclass.CodeGenerator):
    @dataclasses.dataclass
    class ClusterConfigClass:
        eps: float
        min_num_samples: int

    image_size: list
    cluster_config: ClusterConfigClass
    path_output: str

In addition to code creation, an instance of the generated class that is initialized with the YAMLs values can be created automatically.

The use case it was primarily developed for is facilitating code completion for arbitrary YAML config files. Using YAML files as input config allows flexible and general information passing, however, when using pyyaml the information is saved as a dict which does not support any automatic code completion. This means, that you need to know the exact variable names for access.

Using this package you can enjoy YAMLs flexibility and generality, while additionally providing the benefit of code completion and type specific suggestions from your IDE. Thus, no need to memorize exact variable names or types.

Installation

You can install the package using pip

pip install yaml2pyclass

Usage

File Update

To update an existing file, as e.g. in the case of a config file, you need to create a base file. The base file (e.g. at path/to/config.py) should simply contain a class, that inherits from the yaml2pyclass base class CodeGenerator:

import yaml2pyclass


class Config(yaml2pyclass.CodeGenerator):
    # content is updated automatically from the specified YAML file
    pass

Then, in the function where you want to use this class based on the YAML input, import the created class and call the from_yaml function

from path.to.config import Config  # import the class Config from the path/to/config.py file

config = Config.from_yaml("config.yaml")

This will update the file of the Config class with dataclass that matches the input YAML.

Contributors

This project is a collaboration with Felix Hertlein.

License

This code is distributed under the 3-Clause BSD License, see 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

yaml2pyclass-0.1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

yaml2pyclass-0.1.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: yaml2pyclass-0.1.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.19

File hashes

Hashes for yaml2pyclass-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3b780aa3e33e9386a2c08da88a3a8908aaf2506cd65cd9c4ede8a84204250eef
MD5 072eea7b26a9e702e771fc1d04805abb
BLAKE2b-256 6ec4485a2855fa5a41ad44b41d0e87447407374c8c9f13c376653d0a85a5ea71

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for yaml2pyclass-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0cf24492751cbf7c4e9c4363122f0496c4be3539c1fd85901071336964af6d99
MD5 ddca34ab62032f6e54c0185b356e8e92
BLAKE2b-256 09fb1a074b30dcda298159a825f2d072f030b3596c02354017733ca2214e78b8

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