Skip to main content

Object configuration for Python projects

Project description

Objconf - Object Configuration for Python

Build Status

What is objconf?

Objconf provides object configuration for Python. It allows accessing the configuration values as attributes of a configuration object.

Unlike Python built-in configparser, all values have a type. Objconf also supports transformation and validation of values. For example, you can tell objconf to transform relative path to absolute and verify that the file exists.

Here is an example of what would this simple configuration object look like:

import objconf, os

class AppConfig(objconf.Config):
    key_path = objconf.Attribute(
        str,
        transformer=lambda x: os.path.abspath(x),
        validator=lambda x: os.path.isfile(x))

Supported configuration formats:

  • YAML
  • JSON
  • Python ini-like configparser format

Installation

Objconf can be installed using pip:

python3 -m pip install objconf 

Documentation

Config class

Config is a base class for your configuration class. Inherit from Config when defining configuration for your application.

from objconf import Config, Attribute
import io

# Define the configuration
class AppConfig(Config):
    simple_string_attr = Attribute(str)
    int_attr_with_default = Attribute(int, default=5)
    bool_attr_different_key = Attribute(bool, key='actual_bool_key')
    int_over_ten = Attribute(int, validator=lambda x: x > 10)
    upper_str_attr = Attribute(str, transformer=str.upper)

# Load the configuration
yaml_config = '''
simple_string_attr: string_value
actual_bool_key: True
int_over_ten: 42
upper_str_attr: lower
'''
app_config = AppConfig.load_yaml(io.StringIO(yaml_config))

# Use the configuration
print(app_config.int_attr_with_default)  # 5
print(app_config.upper_str_attr)  # 'LOWER'

Loading configuration

The configuration is loaded during creation of the Config instance that defines the configuration values for your application. The instance is created by calling the corresponding factory method:

load_<format>(stream: TextIO, extra_vals: ExtraVals = ExtraVals.WARNING) -> 'Config'

Arguments:

  • stream: A file-like object containing configuration values in the corresponding format. (Except load_dict, it takes a dictionary and all other loading methods use it internally.)
  • extra_vals: It specifies behavior when encountering an undefined attribute in the configuration. Defaults to warning, other options are ignore and error.

The loading functions take also other arguments specific for the configuration format and modifying the behaviour of underlying parser.

load_yaml - Load configuration from YAML

load_json - Load configuration from JSON

load_ini - Load configuration from python ini-like configparser format

Attributes

Attribute stores the value in the owning class (Config) instance. Constructor has the following parameters:

  • type_: The only required parameter - type of the attribute (bool, str, int, list(if supported), …). It is a callable that converts the value to the correct type. Some types might not be supported; e.g. the Python ini-like format does not support lists by default.
  • default: Default value to use if not present in the configuration file.
  • key: Specify key in configuration file if different from attribute name.
  • validator: Callable that takes the configuration value and checks whether it is valid - return True, otherwise return False.
  • transformer: Transform the value from configuration file. This happens after the type conversion but before the validation. Can be used for example for transforming paths (expanding user home dire, changing relative paths to absolute, …).

Tests

Objconf uses tox to run the tests.

python3 -m pip install tox
python3 -m tox

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

objconf-0.3.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

objconf-0.3.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file objconf-0.3.0.tar.gz.

File metadata

  • Download URL: objconf-0.3.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for objconf-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b8519d25d5e941f428139c32f30d0e3f4c78a10560431992d86a6a2714776aa9
MD5 3e8e90e524c3e23acd6f21ec3db99e43
BLAKE2b-256 7606854b27399f7b83f1ef44a833da068b9f681f2025fe492a709f577b1af089

See more details on using hashes here.

File details

Details for the file objconf-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: objconf-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for objconf-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c294c4cfe5c564450dc0719525967a6c5d0e1965c914df01fbd3992af75f36d9
MD5 d22af45fd7105db51eeb6b7cbc79a700
BLAKE2b-256 6a3687fbb15b391dafbe9681cd0e30ad2a499e4bf1ba96c880509c574bb0f9dd

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