Template-dict

Quickstart


pypi docs codecov tests mypy code style: black python

template-dict - JSON compatible Python dictionary templates. You can use them to fill some schema with data dynamically. Template dicts support nested data, lists. functions and eval of simple Python data types.

Installation

With pip and python 3.8+:

pip3 install template-dict

How to use

See the user guide for more info.

Template objects can be used to fill dictionary-like schema with dynamic data.

Note that these are not Jinja templates but rather a tool to manipulate Python data dynamically. To create a template you must define a schema and then feed it with some data mapping.

from template_dict import Template

t = Template({'value': '[key.nested]'})
t.eval({'key': {'nested': True}})  # -> {'value': True}

Template can accept a string or an iterable as a schema.

t = Template(['[name]', '[name]', '[name]'])
t.eval({'name': 'dogs'})  # -> ["dogs", "dogs", "dogs"]

You can access template keys before evaluating it.

t = Template({'value': '[key]'})
t.keys  # -> ['key']

License

MIT License

Copyright (c) 2024 violet-black

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.