Skip to main content

Allows defining model classes and loading/saving them to INI files

Project description

https://img.shields.io/pypi/v/ConfigModel?color=0285f7 https://readthedocs.org/projects/py-configmodel/badge/?version=latest https://coveralls.io/repos/github/umanamente/py-configmodel/badge.svg?branch=master

ConfigModel

ConfigModel is a Python package that allows you create a config for your project quickly and easily.

?:

But why should I use ConfigModel instead of just using configparser?

ConfigModel offers several key advantages over the standard configparser:

  • Intellisense bonus: Your IDE will be able to autocomplete your config parameters, since they are defined as class attributes.

  • Structured Configuration: Define configurations as nested Python classes, allowing for clear, hierarchical organization.

  • Default Values: Specify default values within class definitions, ensuring consistency and allowing to generate “distro” configs.

  • Automatic File Generation: Automatically generate configuration files from class definitions, saving time and reducing manual errors.

Example

Here is a quick example of how to use ConfigModel.

First, define a config for your app:

from configmodel import ConfigModel, config_file, nested_field

# ============================
# define a config for your app
# ============================
@config_file("config.ini")
class AppConfig(ConfigModel):
    color_scheme = "dark"
    font_size = 12

    class AccountInfo(ConfigModel):
        username = "guest"
        password = ""
        last_login = ""

    class GoogleApi(ConfigModel):
        client_id = "<put your client id here>"
        secret = "<put your secret here>"

    photos_api = GoogleApi()
    maps_api = GoogleApi()

Then, use your config:

# ============================
# use your config
# ============================
def main():
    # get config parameter
    print(AppConfig.AccountInfo.username)
    # set config parameters
    AppConfig.AccountInfo.last_login = "2024-01-01"
    AppConfig.photos_api.client_id = "1234"
    AppConfig.maps_api.client_id = "5678"

It will create a config file config.ini with the following content:

[Global]
color_scheme = dark
font_size = 12

[account_info]
username = guest
password =
last_login = 2024-01-01

[photos_api]
client_id = 1234
secret = <put your secret here>

[maps_api]
client_id = 5678
secret = <put your secret here>

Note that

  1. You can specify config file name with @config_file decorator.

  2. Section names ([account_password]) of nested classes are automatically generated from class names, if no instances of this class are created.

  3. You can reuse nested classes (GoogleApi) in different places of your config.

Installation

You can install ConfigModel using pip:

pip install ConfigModel

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

configmodel-0.2.0.tar.gz (32.4 kB view details)

Uploaded Source

Built Distribution

ConfigModel-0.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file configmodel-0.2.0.tar.gz.

File metadata

  • Download URL: configmodel-0.2.0.tar.gz
  • Upload date:
  • Size: 32.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for configmodel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 105b2d5b1804bff3e7833d03248656ccdd116b5cee9a32570396aa36edf28bb6
MD5 ab038116d3dcd97a6c09e011f7c572f8
BLAKE2b-256 576be693e47a420eae4e9c00854b44d3d32eae9c4dc4735e01faf48e13e2548c

See more details on using hashes here.

File details

Details for the file ConfigModel-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ConfigModel-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for ConfigModel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b39ad23652d613ef03fa99f91e6dec98d4a65cf0f6572ec45120fd3b62e68d52
MD5 b8e20b4bec1ff5b87b1a5d9f17eb0097
BLAKE2b-256 80e2a0b243be5081ce21c761305a3f7fa306149f3290e185a29bd4c3efcfecd6

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