Skip to content

Instantly share code, notes, and snippets.

Last active June 9, 2020 04:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Python Jinja'ing Objects
#!/usr/bin/env python3
import time
import json
import jinja2
import yaml
twoweeks: "{{ twoweeksago }}"
hey: true
unhydrated_config = yaml.safe_load(const_date_yaml)
dynam_vars = {"twoweeksago" : int(time.time()) - 604800*2}
if isinstance(unhydrated_config, str) is False:
# If it's a string
do_json = True
template = json.dumps(unhydrated_config, default=str)
# Won't be ran in example
do_json = False
template = unhydrated_config
jtemplate = jinja2.Environment(loader=jinja2.BaseLoader).from_string(template)
rstring = jtemplate.render(**dynam_vars)
if do_json is True:
rendered = json.loads(rstring)
# Not called in Example
rendered = rstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment