Skip to main content

Build Status License Version

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

# create config parser
builder = ConfigBuilder()

# parse config
config = builder.parse_config('path/to/config.json')

# access elements
host = config.server.host
port = config.server.port
myfield = config.myfield

Validate field types

builder.validate_field_type('server.ip', str)
builder.validate_field_type('server.port', int)
builder.validate_field_type('jwt.access_token_expires', str)

Validate field values

from python_json_config.validators import is_unreserved_port, is_ipv4_address, is_timedelta

# use provided methods
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)

# use custom validation function
builder.validate_field_value('server.ip', lambda ip: ip != '0.0.0.0')

# return custom error messages in your lambda
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'])

Transform field values

from python_json_config.transformers import to_timedelta

# use provided methods
builder.transform_field_value('jwt.access_token_expires', to_timedelta)


from datetime import datetime

# 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'))

Define field access settings

# required means an error is thrown if a non-existing field is accessed 
builder.set_field_access_required()
# return None for the following fields instead of throwing an error
builder.add_optional_field('server.host')
builder.add_optional_fields(['cache.ttl', 'server.path'])

# optional means None is returned if a non-existing field is accessed 
builder.set_field_access_optional()
# throw an error for the following fields instead of returning None
builder.add_required_field('server.user')
builder.add_required_fields(['cache.backend', 'server.password'])

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)

Change config values

config = ConfigBuilder().parse_config({"server.port": 1024})

config.add("server.host", "localhost")
assert config.server.host == "localhost"

config.add("cache", "redis")
assert config.cache == "redis"

config.update("server.port", 1025)
assert config.server.port == 1025

config.update("server.user", "user", upsert=True)
assert config.server.user == "user"

Overwrite fields with environment variables

First, set environment variables (e.g., via bash):

$ MYPROJECT_SERVER_HOST="localhost"
$ MYPROJECT_CACHE="redis"
$ MYPYTHONPROJECTS_USER="user"

Escape underscores in names of variables with another underscore:

$ MYPYTHONPROJECTS_LOG__FILE="project.log"

Then just tell the builder, which prefixes should be merged:

builder = ConfigBuilder()
# you can also just pass a single prefix (builder.merge_with_env_variables("MYPROJECT")
builder.merge_with_env_variables(["MYPROJECT", "MYPYTHONPROJECTS"])
config = builder.parse_config({"server.host": "0.0.0.0"})

assert config.server.host == "localhost"
assert config.cache == "redis"
assert config.user == "user"
assert config.log_file == "project.log"

Alternatively you can also do the merging after creating the config object:

builder = ConfigBuilder()
config = builder.parse_config({"server.host": "0.0.0.0"})
config.merge_with_env_variables(["MYPROJECT", "MYPYTHONPROJECTS"])

assert config.server.host == "localhost"
assert config.cache == "redis"
assert config.user == "user"

Serialization

The config can be serialized to a dictionary, json or binary (via pickle or msgpack).

builder = ConfigBuilder()
config = builder.parse_config({"server.host": "0.0.0.0"})

import pickle
pickle_config = pickle.loads(pickle.dumps(config))

dict_config = builder.parse_config(config.to_dict())

import json
json_config = builder.parse_config(config.to_json())

import msgpack
msgpack_config = Config.from_msgpack(config.to_msgpack())

Important note: serializing via json or msgpack will stringify any non-serializable value (e.g., datetime objects).

Metadata

Release files for python-json-config 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-json-config 1.2.3
File Size Uploaded
python_json_config-1.2.3.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-json-config 1.2.3
File Interpreter ABI Platform
python_json_config-1.2.3-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 34.9 kB

Release files / python_json_config-1.2.3.tar.gz

Download URL python_json_config-1.2.3.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f2af5efab25df394880a0da89e4604539616451cd909a29e4b4eeb115e9978db
BLAKE2b-256 checksum
How to use checksums
2d1fd1e59b55d9606d83bd0d1acfa3c2c961bbdabdd73ee83f4f9630a3e121d3
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / python_json_config-1.2.3-py2.py3-none-any.whl

Download URL python_json_config-1.2.3-py2.py3-none-any.whl
Size 19.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9bcea2473ccc0ffcb204cc99853bcbdd844e61dfc0cbe2975b5e31a944d0a869
BLAKE2b-256 checksum
How to use checksums
fffcce228af4c6864db62c3501f3396b4ed64e46d187c42a087f6513d23e3f76
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.2.3 This release

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page