A lightweight dict-like config library with validation support
Project description
cfgdict
A lightweight dict-like config library with validation support
Features
- Supports nested dictionary structures
- Provides configuration validation with customizable rules
- Includes utility functions for flattening and reconstructing dictionaries
- Easy-to-use API for creating and managing configurations
Installation
You can install cfgdict directly from GitHub using pip:
pip install git+https://github.com/gseismic/cfgdict.git
For development installation:
git clone https://github.com/gseismic/cfgdict.git
cd cfgdict
pip install -e .
Usage
Creating a Config
from cfgdict import Config
config_schema = [
dict(field='n_step', required=True, default=3, rules=dict(type='int', gt=0)),
dict(field='learning_rate', required=True, default=0.1, rules=dict(type='float', gt=0, max=1)),
dict(field='nest.gamma', required=True, default=0.99, rules=dict(type='float', min=0, max=1)),
dict(field='nest.epsilon', required=True, default=0.1, rules=dict(type='float', min=0, max=1)),
dict(field='nest.verbose_freq', required=True, default=10, rules=dict(type='int', gt=0)),
]
cfg_dict = {
'n_step': 3,
'learning_rate': 0.1,
'nest': {
'gamma': 0.99,
'epsilon': 0.1,
'verbose_freq': 10
}
}
config = Config.from_dict(cfg_dict, schema=config_schema, strict=True)
print(config.to_dict())
Flattening and Unflattening Dictionaries
from cfgdict import flatten_dict, unflatten_dict
nested_dict = {
'a': 1,
'b': {
'c': 2,
'd': {
'e': 3
}
},
'f': 4
}
flattened = flatten_dict(nested_dict)
print(f'Flattened: {flattened}')
# Output: {'a': 1, 'b.c': 2, 'b.d.e': 3, 'f': 4}
unflattened = unflatten_dict(flattened)
print(f'Unflattened: {unflattened}')
# Output: {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}, 'f': 4}
Validation Rules
cfgdict supports the following validation rules:
type
: Specify field type (e.g., 'int', 'float', 'str', etc.)required
: Whether the field is required (True/False)default
: Default value if not provided
Comparison operators:
eq
: Equal tone
: Not equal togt
: Greater thange
: Greater than or equal tolt
: Less thanle
: Less than or equal tomin
: Minimum value (inclusive)max
: Maximum value (inclusive)
Example usage:
config_schema = [
dict(field='age', required=True, rules=dict(type='int', ge=18, lt=100)),
dict(field='score', required=False, default=0, rules=dict(type='float', min=0, max=100)),
dict(field='status', required=True, rules=dict(type='str', ne='inactive')),
]
In this example:
- 'age' must be an integer, greater than or equal to 18, and less than 100
- 'score' is optional with a default of 0, must be a float between 0 and 100 (inclusive)
- 'status' is required and must be a string not equal to 'inactive'
Nested configurations with logger
set verbose=True
cfgdict supports nested configurations:
from cfgdict import Config
nested_schema = [
dict(field='database.host', required=True, rules=dict(type='str')),
dict(field='database.port', required=True, rules=dict(type='int', min=1, max=65535)),
dict(field='api.version', required=True, rules=dict(type='str')),
dict(field='api.endpoints.users', required=True, rules=dict(type='str')),
dict(field='api.endpoints.products', required=True, rules=dict(type='str')),
]
nested_config = Config.from_dict({
'database': {
'host': 'localhost',
'port': 5432
},
'api': {
'version': 'v1',
'endpoints': {
'users': '/api/v1/users',
'products': '/api/v1/products'
}
}
}, schema=nested_schema, verbose=True)
# verbose=True: log enabled
print(config.to_dict())
Custom Validation Rules
You can extend the validation system with custom rules:
from cfgdict import Config, ConfigValidationError
def validate_even(value):
if value % 2 != 0:
raise ConfigValidationError(f"Value {value} is not even")
config_schema = [
dict(field='even_number', required=True, rules=dict(type='int', custom=validate_even))
]
config = Config.from_dict({'even_number': 4}, schema=config_schema)
# Valid
# config = Config.from_dict({'even_number': 3}, schema=config_schema) # Raises ValidationError
More Examples
For more usage examples, please refer to:
Contributing
We welcome issue reports and pull requests. If you have any suggestions or improvements, please feel free to contribute.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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 cfgdict-1.0.1.tar.gz
.
File metadata
- Download URL: cfgdict-1.0.1.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3242d1b705fb05933ab64246a02499aeb8ff3190419c974a5cd634bbb3b6988f |
|
MD5 | ccc2d156eebb25575092a723f435f9b1 |
|
BLAKE2b-256 | ccb0be757ba40e8baafda1296a82338388dc09755f39eddfe4eb8a837edb1585 |
File details
Details for the file cfgdict-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: cfgdict-1.0.1-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d888fedc02edb257462476f905bd1e57d18ac4c0583e2630c77650babd7cceac |
|
MD5 | e30c1ec84f9a0a4b3b9b1008ccc0dd4d |
|
BLAKE2b-256 | 8977a5f1fe3c4f569f592341739582881de08140be779390674e3c9604e5119d |