This library allows to load json configs and access the values like members (i.e., via dots), validate config field types and values and transform config fields.
Project description
Overview
This library allows to load json configs and access the values like members (i.e., config.server.port
instead of config['server']['port']
), validate the data types of fields and transform the values of fields.
Installing
pip install python-json-config
Usage
from python_json_config import ConfigBuilder
from python_json_config.validators import is_unreserved_port, is_ipv4_address, is_timedelta
from python_json_config.transformers import to_timedelta
from datetime import datetime, timedelta
# create config parser
builder = ConfigBuilder()
# assert that port contains an int value
builder.validate_field_type('server.ip', str)
builder.validate_field_type('server.port', int)
builder.validate_field_type('jwt.access_token_expires', str)
# assert that the port is not a reserved port
builder.validate_field_value('server.ip', is_ipv4_address)
builder.validate_field_value('server.port', is_unreserved_port)
builder.validate_field_value('jwt.access_token_expires', is_timedelta)
# return custom error messages in your lambdas
builder.validate_field_value('server.ip', lambda ip: (ip != '0.0.0.0', 'IP is unroutable.'))
# chain validation functions
builder.validate_field_value('server.ip', [lambda ip: ip != 'localhost', lambda ip: ip != '127.0.0.1'])
# parse a timedelta (e.g., Jun 1 2005) into a datetime object
builder.transform_field_value('important_date', lambda date: datetime.strptime(date, '%b %d %Y'))
builder.transform_field_value('jwt.access_token_expires', to_timedelta)
# parse config
config = builder.parse_config('path/to/config.json')
# access config values
port = config.server.port
assert port > 1023
ip = config.server.ip
assert ip not in ['0.0.0.0', 'localhost', '127.0.0.1']
important_date = config.important_date
assert isinstance(important_date, datetime)
jwt_access_token_expires = config.jwt.access_token_expires
assert isinstance(jwt_access_token_expires, timedelta)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python_json_config-1.1.0.tar.gz
.
File metadata
- Download URL: python_json_config-1.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d494bfccb3a85fd2d708ae39c298325b1025ed2ee18368f8af4b8bebf5cc897 |
|
MD5 | 1137681504a0d835d0711c97526d4fd6 |
|
BLAKE2b-256 | 16d05869b600854b284b795919aafb670cb59d6fbcd85ce13aaf6c1f16b6a7fd |
File details
Details for the file python_json_config-1.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: python_json_config-1.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00a07e1d92d0afab298516e926e63dbbdf5e8f1bcfd5484bf2e71730b627607b |
|
MD5 | 895a0f237c3951c9efb2f00360db094b |
|
BLAKE2b-256 | fef7ce19588e9d47730bfe87dbdad70443b1ec6900b75e808bfa2b21526e2551 |