Skip to main content

A yaml config mapper to create static config classes.

Project description

PIGCONF Configuration Mapper


Pigconf is a simple config mapper that maps yaml style configurations to configuration classes. It allows strict mapping of yaml fields to class attributes based on specified keys.

Installation

Run the followint to install:

pip install pigconf

Quickstart Example

student_conf.yml

student:
  id: 1
  name: "Alice"
  info:
    grade: "A"
import mapper

@mapper.set_attr("grade","student.info.grade", str)
@mapper.set_attr("name", "student.name", str)
@mapper.set_attr(attr_name="id", config_key="student.id", attr_type=int)
@mapper.load_config("student_conf.yml")
class Student:
    pass
>>> import mapper
>>> 
>>> 
>>> @mapper.set_attr("grade","student.info.grade", str)
... @mapper.set_attr("name", "student.name", str)
... @mapper.set_attr(attr_name="id", config_key="student.id", attr_type=int)
... @mapper.load_config("student_conf.yml")
... class Student:
...     pass
... 
>>> s = Student
>>> print(f"Student name: {s.name} with id: {s.id} has the grade: {s.grade}")
Student name: Alice with id: 1 has the grade: A

The configuration can also be loaded with a config_key argument so that the whole object under that key is mapped.

Example:

import mapper

@mapper.set_attr("grade","info.grade", str)
@mapper.set_attr("name", "no_key", str)
@mapper.set_attr(attr_name="id", config_key="id", attr_type=int)
@mapper.load_config("student_conf.yml",config_key="student")
class Student:
    pass

Now the attributes only need the keys under defined under the "student" key in the yaml.

Strict Mapping

The mapper doesn't allow None values or missing keys. Anything specified as an attribute key must be defined in the yaml. This simply allows the application to fail during the configuration mapping phase rather than failing at runtime.

>>> import mapper
>>> @mapper.set_attr("grade","info.wrong_key", str)
... @mapper.load_config("student_conf.yml","student")
... class Student:
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/Users/anshumanrohella/Documents/Workspace/pigconf/src/mapper.py", line 31, in decorator
    value = _get_value_from_parsed_config(cls._config, config_key)
  File "/Users/anshumanrohella/Documents/Workspace/pigconf/src/mapper.py", line 46, in _get_value_from_parsed_config
    raise KeyError(f"Could not find key: {key} in config file.")
KeyError: 'Could not find key: wrong_key in config file.'

Contribute

To install pigconf, along with the tools you need to develop and run tests, run the following in your virtualenv.

pip install -e .[dev]

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

pigconf-0.0.3.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

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

pigconf-0.0.3-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file pigconf-0.0.3.tar.gz.

File metadata

  • Download URL: pigconf-0.0.3.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.5

File hashes

Hashes for pigconf-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c334e7e5813629b2bee06f103e3ddb6bc4480f9201ba516f549758cabbe87dae
MD5 e1cb7c209196271a46e48c35669454c1
BLAKE2b-256 bb723cabe13b90f8738660a853e5b98d28377d44f5639c73d81a8ce135f7ca86

See more details on using hashes here.

File details

Details for the file pigconf-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pigconf-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.5

File hashes

Hashes for pigconf-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1edc1e3ca9dc8abe31d784701632c956e09b286ceaed8cc95807b330b9f7a1f1
MD5 3db3b1a236ee215edd495c49a3a5c426
BLAKE2b-256 1398b72483afa5e4cad890fba8367d6b50fb57e5c4be2eefa03cd88c40c3c26f

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