Skip to main content

Generate configuration files for classes.

Project description

pyfigure

Generate configuration files for classes.

Installation

pip install pyfigure

Usage

To generate a configuration file for a class, you must extend the class with Configurable, from the pyfigure module.

Then, to add options to the configuration, create a new Config class, and add variables of the Option class.

class YourClass(Configurable):

    class Config:

        option_name: type = Option(
            default,
            parse,
            description,
        )

        class sub_config:

            cub_option: type = Option()

Where the arguments are as follows:

argument description
default The default value of the option
description A comment to explain what the option is for
parse A function that parses the given value, returns a new value or throws an exception

file.py

from pyfigure import Configurable, Option
from typing import Literal, List

class ExampleClass(Configurable):

    #config_file = 'other_file.toml'

    class Config:
        string_option: str = Option('default', "Any string works here.")
        integer_option: int = Option(24, "Any integer here.")
        float_option: float = Option(15.55, "Any float.")
        bool_option: bool = Option(True, "Any boolean.")
        list_option: list = Option(['e', 'a', 'o'], "A list of things.")
        int_list_option: List[int] = Option([1, 2, 3], 'A list of integers.')
        choice_option: Literal['spam', 'eggs'] = Option('spam', "Either 'spam' or 'eggs'")
        class sub_config:
            sub_option: str = Option('this value in a sub value')
            class sub_sub_config:
                sub_sub_option: str = Option('this value is a sub value of a sub value')

    #def __init__(self):
    #    Configurable.__init__(self)

ec = ExampleClass()
print(ec.config)

This class, when initialized, will print:

{
    'string_option': 'default',
    'integer_option': 24,
    'float_option': 15.55,
    'bool_option': True,
    'list_option': ['e', 'a', 'o'],
    'int_list_option': [1, 2, 3],
    'choice_option': 'spam',
    'sub_config': {
        'sub_option': 'this value in a sub value',
        'sub_sub_config': {
            'sub_sub_option': 'this value is a sub value of a sub value'
        }
    }
}

And create a new file:

file.toml

string_option = "default" # Any string works here.
integer_option = 24 # Any integer here.
float_option = 15.55 # Any float.
bool_option = true
list_option = ["e", "a", "o"] # A list of things.
int_list_option = [1, 2, 3] # A list of integers.
choice_option = "spam" # Either 'spam' or 'eggs'

[sub_config]
sub_option = "this value in a sub value"

[sub_config.sub_sub_config]
sub_sub_option = "this value is a sub value of a sub value"

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

pyfigure-2.3.1.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

pyfigure-2.3.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file pyfigure-2.3.1.tar.gz.

File metadata

  • Download URL: pyfigure-2.3.1.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for pyfigure-2.3.1.tar.gz
Algorithm Hash digest
SHA256 80344c204daac7c839a3be3e9b2385a9d84909a48d681a1eec0754b6286215ef
MD5 9c98aa5f5ed17e5eee4a5ad89fa37532
BLAKE2b-256 a2f3066ee7c284744151d4329b7bb29035ea78ec8235a7845cc44fc31b646dbe

See more details on using hashes here.

File details

Details for the file pyfigure-2.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyfigure-2.3.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for pyfigure-2.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e1003b4b601f173465bb0ba63d013a5dbdd3e9942b40740a0683cbd1eff18ba1
MD5 75580a296d1282c897c50c21a2410399
BLAKE2b-256 b8297d9d42bd25d9fca493f206e1d0e21668ba74ada4ad9fbf0dfc82829272b5

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