A small collections of modular components useful in other projects.
Project description
projectutils
A small collections of modular components useful in other projects.
projectutils.init
The init
module is helpful when you need to create a complex directory structure.
You can create objects that represent Directories and Files to create a tree.
projectutils.config
The config
module allows you to define a configuration schema and dinamically load configurations
from multiple sources.
schema.json
:
{
"string": {"doc": "String config", "format": "string", "default": "DEFAULT"},
"integer": {"doc": "Integer config", "format": "int", "default": 1},
"float": {"doc": "Float config", "format": "float", "default": 1.1},
"list": {"doc": "List config", "format": "list", "default": ["a", "b", "c"]},
"bool": {"doc": "Bool config", "format": "bool", "default": true},
"nested.string": {"doc": "String config", "format": "string", "default": "DEFAULT"},
}
/etc/myapp/config.json
:
{
"float": 2.2,
"string": "loaded from json",
"nested": {
"string": "loaded from json"
}
}
/etc/myapp/.env
:
MYAPP_CONF_LIST="e,n,v"
MYAPP_CONF_BOOL="False"
MYAPP_CONF_NESTED_STRING="loaded from env"
app.py
:
import json
from pathlib import Path
from projectutils.config import Config, ENVSource, JSONSource
# Setup includes loading the schema
# and defining some params used in sources.
with open("schema.json", "r") as fp:
schema = json.load(fp)
envs_prefix = "MYAPP_CONF_"
configs_root = Path("/etc/myapp")
# Source definition dictates precedence.
# In this case ENV values will override JSON values.
sources = [
JSONSource(configs_root / "config.json"),
ENVSource(envs_prefix, configs_root),
]
# Load config
config = Config(schema, sources)
config.get("integer")
# 1
config.get("float")
# 2.2
config.get("bool")
# False
config.get("list")
# ['e', 'n', 'v']
config.get("nested.string")
# 'loaded from env'
config.get("string")
# 'loaded from json'
config.get("nested")
# {'nested': 'loaded from env'}
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
projectutils-1.1.tar.gz
(19.8 kB
view hashes)
Built Distribution
projectutils-1.1-py3-none-any.whl
(20.6 kB
view hashes)
Close
Hashes for projectutils-1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cbc115e0349f8182b9f4958733bb95a2813a7c0580ca63589ba47a535410bcb |
|
MD5 | fdeed581036f0ab52a3e7128ec717454 |
|
BLAKE2b-256 | 3924b049737c102d85ac9f628ac0009f773fdc53a5b48aa38bee231b089f64ce |