-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand jinja environment #26
Expand jinja environment #26
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #26 +/- ##
===========================================
+ Coverage 47.84% 47.87% +0.03%
===========================================
Files 18 18
Lines 1649 1650 +1
Branches 335 335
===========================================
+ Hits 789 790 +1
Misses 801 801
Partials 59 59 ☔ View full report in Codecov by Sentry. |
f4be48e
to
1873fee
Compare
After a conversation with @aerorahul, I removed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good.
Description
This adds
threenew features to theJinja
class:1.Template.substitute_structure
is now available as a filter. This is useful for parsing jinja templates that reference multiple similar variables (e.g. ensemble member directories). A template for the ensemble members can be passed into the jinja context dictionary and used to construct the final variables based on iterators, cycle date/time, etc.- For example, if 20 ensemble members each have data in
data/YYYYMMDD/HH/analysis/mem###
, one could define'ANALYSIS_tmpl' : "data/${YMD}/{HH}/analysis/mem{MEM}", 'current_cycle': datetime.datetime(year=2024, month=5, day=3, hour=6)
- Then in the template:
{% set cycle_YMD = to_YMD(current_cycle) %}
{% set cycle_HH = strftime(current_cycle, "%H") %}
{% for mem in range(1,21) %}
{% set mem3 = '%03d' % mem %}
{% set tmpl_dict = {'YMD': cycle_YMD, 'HH': cycle_HH, 'MEM': mem3} %}
{{ ANALYSIS_tmpl | template_substitute_structure(DOLLAR_CURLY_BRACE, tmpl_dict.get) }}
{% endfor %}
jinja.ext.do
extension to Jinja. This enables{% do ... %}
statements which execute a command for which the output should not be saved. A useful example is appending to a list. Since lists in Python returnNone
, "None" would be written to the parsed jinja template in the following example:{% set a_list = [] %}{{ a_list.append("foo") }}
. Instead, one can now use do:lambda
function for theadd_to_datetime
filter so anySilentUndefined
input will return aSilentUndefined
, preventing obscure failures.This is needed for NOAA-EMC/global-workflow#2491
Type of change
How Has This Been Tested?
Checklist