Add `difference` filter to Jinja context
See original GitHub issueAnsible has a few useful filters in their Jinja context that are not part of core Jinja. It’d be great to expose some of them to macro developers dbt.
For example, difference
is a helpful filter to get the difference between two lists.
@contextfilter
def difference(context, first, second):
second = set(second)
return [item for item in first if item not in second]
Used like so:
{%- set columns = columns | difference(except_columns) -%}
Since this would be run in Python, there should be no cross-database issues.
Related to #480 maybe although I don’t think it’s necessary to do from an end-user perspective, as long as its easy to enough to PR new functions like this.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:10 (9 by maintainers)
Top Results From Across the Web
Difference between jinja2 functions and filters? - Stack Overflow
The difference is that filters can have special access to the Environment or Context , but regular (global) functions cannot; specifically, ...
Read more >Jinja Documentation (3.0.x) » Template Designer ...
The context dictionary by the application with markupsafe.Markup. The template, with the |safe filter. If a string that you marked safe is passed...
Read more >Jinja built-in filters and tests (like Django filters) - Web Forefront
The Jinja documentation makes an explicit difference between what it calls filters and tests. The only difference is Jinja tests are used to...
Read more >Jinja2 templates and filters - Pexip Infinity Docs
variables that are either locally defined within the script or template, or that are provided automatically according to the context in which the...
Read more >Understanding Jinja - Salt Project Documentation
By adding with context to the include/import directive, the current context can be passed ... Saltstack extends builtin filters with these custom filters: ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Huh! Turns out I created a dupe of this in https://github.com/dbt-labs/dbt-core/issues/4571 - going to close that one and reopen this one. cc @gshank who triaged my one and might be able to provide some pointers of where to add things.
For the record, I was also jealous of Ansible’s casting datatypes in addition to the set theory functions
Although we are closing this issue as stale, it’s not gone forever. Issues can be reopened if there is renewed community interest; add a comment to notify the maintainers.