Template loader that loads common formats and replaces placeholders
For installation with pip directly from this GitHub repository simply open a terminal and type
pip install git+ssh://[email protected]/rlipperts/template.git
There are no PyPI releases. Neither are they planned.
Import the package
from template_loader import loaderLoad json template files
template_path = Path.cwd() / ('data/example_template.json')
check_me = loader.load(template_path, placeholder='replacement', something_else=3.14159,
some_bool=True)Load yaml template files
template_path = Path.cwd() / ('data/example_template.yaml')
check_me = loader.load(template_path, placeholder='replacement', something_else=3.14159,
some_bool=True)Load toml template files
template_path = Path.cwd() / ('data/example_template.toml')
check_me = loader.load(template_path, placeholder='replacement', something_else=3.14159,
some_bool=True)Pass replacements from dictionaries.
The optional safe parameter (default is True) makes the loader raise an KeyError, if there are
any placeholders for which no replacements exist.
replacements = {
'Lorem': 'Lorem',
'ipsum': 'ipsum',
'adipiscing': 'adipiscing',
}
template_path = Path.cwd() / ('data/example_template.toml')
check_me = loader.load(template_path, safe=False, **replacements)Replacement types are preserved, if possible:
-
Loading
key="${int}"withint=123from a json file will replace the placeholder like thiskey=123before loading the json data into a python data structure. -
Loading
key="some text ${int}"withint=123from a json file will replace the placeholder like thiskey="some text 123"before loading the json data into a python data structure.
Code:
import json
from pathlib import Path
from template_loader import loader
template_path = Path.cwd() / ('data/example_template.json')
replacements = {
'placeholder': 'replacement',
'float_placeholder': 3.14159,
}
loaded_json_data = loader.load(template_path, **replacements)
output_folder = Path.cwd() / 'out'
output_folder.mkdir(exist_ok=True)
output_path = output_folder / 'some_output.json'
with open(output_path, 'w') as file:
json.dump(loaded_json_data, file)Input json data/example_template.json:
{
"test": {
"testA": ["testy", "testtest", "test ${placeholder}"],
"testB": 125,
"testC": {
"testD": {
"test": "${float_placeholder}"
}
}
},
"testB": true,
"testC": 1.2345,
"${placeholder}": "test"
}Output json out/some_output.json:
{
"test": {
"testA": ["testy", "testtest", "test replacement"],
"testB": 125,
"testC": {
"testD": {
"test": 3.14159
}
}
},
"testB": true,
"testC": 1.2345,
"replacement": "test"
}