question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `difference` filter to Jinja context

See original GitHub issue

Ansible 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
joellabescommented, Feb 21, 2022

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

0reactions
github-actions[bot]commented, Aug 29, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found