Confident

https://img.shields.io/pypi/pyversions/confident?style=plastic:alt:PyPI-PythonVersion https://img.shields.io/pypi/v/confident?style=plastic&color=%2334D058:alt:PyPI https://img.shields.io/github/workflow/status/limonyellow/confident/Python%20package/main?style=plastic:alt:GitHubWorkflowStatus(branch) https://img.shields.io/github/license/limonyellow/confident?style=plastic:alt:GitHubLicense

Confident helps you create configuration objects from multiple sources such as files and environment variables. Confident config objects are data models that enforce validation and type hints by using pydantic library.

With Confident you can manage multiple configurations depend on the environment your code is deployed. While having lots of flexibility how to describe your config objects, Confident will provide visibility of the process and help you expose misconfiguration as soon as possible.

Example

 1import os
 2
 3from confident import Confident
 4
 5
 6# Creating your own config class by inheriting from `Confident`.
 7class MyAppConfig(Confident):
 8    port: int = 5000
 9    host: str = 'localhost'
10    labels: list
11
12
13# Illustrates some environment variables.
14os.environ['host'] = '127.0.0.1'
15os.environ['labels'] = '["FOO", "BAR"]'  # JSON strings can be used for more types.
16
17
18# Creating the config object. `Confident` will load the values of the properties.
19config = MyAppConfig()
20
21print(config.host)
22#> 127.0.0.1
23print(config.json())
24#> {"port": 5000, "host": "127.0.0.1", "labels": ["FOO", "BAR"]}
25print(config)
26#> port=5000 host='127.0.0.1' labels=['FOO', 'BAR']
27print(config.full_details())
28#> {
29# 'port': ConfigField(name='port', value=5000, origin_value=5000, source_name='MyAppConfig', source_type='class_default', source_location=PosixPath('~/confident/readme_example.py')),
30# 'host': ConfigField(name='host', value='127.0.0.1', origin_value='127.0.0.1', source_name='host', source_type='env_var', source_location='host'),
31# 'labels': ConfigField(name='labels', value=['FOO', 'BAR'], origin_value='["FOO", "BAR"]', source_name='labels', source_type='env_var', source_location='labels')
32# }

Capabilities

Confident object can load config fields from multiple sources:

  • Environment variables.

  • Config files such as ‘json’ and ‘yaml’.

  • ‘.env’ files.

  • Explicitly given fields.

  • Default values.

  • Deployment configs. (See below)

Loading capabilities can be customized easily. Confident handles the loading and then provides ways to understand which value was loaded from what source.

Confident object core functionality is based on pydantic library. That means the Confident config object has all the benefits of pydantic’s BaseModel including Type validation, object transformation and many more features.

Contents