Skip to content
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

Make zip function available in Jinja #2345

Closed
friendofasquid opened this issue Apr 20, 2020 · 5 comments · Fixed by #5107
Closed

Make zip function available in Jinja #2345

friendofasquid opened this issue Apr 20, 2020 · 5 comments · Fixed by #5107
Labels
enhancement New feature or request help_wanted Trickier changes, with a clear starting point, good for previous/experienced contributors

Comments

@friendofasquid
Copy link

Would like to use zip to loop over some results in a Jinja template

{% for j,k in zip(a, b) %

Currently zip is not available, but would make my code a much cleaner.

@friendofasquid friendofasquid added enhancement New feature or request triage labels Apr 20, 2020
@drewbanin
Copy link
Contributor

Thanks for opening this issue @friendofasquid!

I can buy either:

  1. Adding zip to the context directly, or
  2. Adding it under a namespace

I think if we're only going to add zip, then adding it to the context directly makes sense. If we're instead going to add other Python builtin functions in the future (like set(), dict(), tuple(), or similar) then we'd be better off namespacing the functions to avoid polluting the global namespace.

I think I'd be in favor of adding set at the very least -- maybe we would also benefit from:

  • all()
  • any()

As far as namespaces go, we already have a builtins namespace, so we could probably add builtins.zip with a single line of code :D

Do you have any thoughts / preferences / ideas? Curious to hear what you think

@drewbanin drewbanin removed the triage label Apr 21, 2020
@friendofasquid
Copy link
Author

I don’t know enough about Jinja templating to comment.

My gut would be to have a set of things that aren’t namespaces, mostly because I think dbt is a lot of people’s first time writing code that isn’t SQL.

@github-actions
Copy link
Contributor

This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please remove the stale label or comment on the issue, or it will be closed in 7 days.

@github-actions github-actions bot added the stale Issues that have gone stale label Nov 22, 2021
@mathcoder3141
Copy link

@drewbanin bumping this back up to see if this can be done

@jtcohen6
Copy link
Contributor

I buy the utility of this one, especially zip() + set(). There have been a handful of times I'd wanted to use set logic in particular (example).

I also think the initial reservation may not be as much of an issue, given how we construct builtins: Every method decorated @contextmember gets added to both the global namespace and the builtins namespace. If ever a user wanted to customize the set() macro, they could do it by writing a macro named set() that references builtins.set(). That's (my understanding of) how overrides for ref, source, config work today, and : https://docs.getdbt.com/reference/dbt-jinja-functions/builtins.

The code here can be a little bit tricky, but the basic implementation should look a lot like the tojson method:

  • Decorated with @staticmethod + @contextmember
  • Perform the Python operation (zip(), set(), etc) and return the value
  • Tested in test/unit/test_context.py

I'm going to open this one back up, and mark it as help_wanted (a.k.a. "good second issue," for a community contributor who's comfortable writing a bit of Python)

@jtcohen6 jtcohen6 reopened this Jan 28, 2022
@jtcohen6 jtcohen6 added help_wanted Trickier changes, with a clear starting point, good for previous/experienced contributors Team:Language and removed stale Issues that have gone stale labels Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help_wanted Trickier changes, with a clear starting point, good for previous/experienced contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants