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

Contains as a new Test (flipped operation of in) #1766

Open
maweki opened this issue Nov 29, 2022 · 2 comments
Open

Contains as a new Test (flipped operation of in) #1766

maweki opened this issue Nov 29, 2022 · 2 comments

Comments

@maweki
Copy link

maweki commented Nov 29, 2022

Since 2.10 there is the in test where I can filter all elements of a list on whether they occur in another container like so: [1,2,2,4,5,6,7] | select('in', [2,6]) | list returns [2,2,6]. The reverse operation is also useful.

In my example I had a list of TV episode names and wanted only to show those containing a specific word. I would want the following: ['Mike','Joe','Michael'] | select('contains', 'Mi') | list returns ['Mike','Michael']

Writing the for-loop was no option for me, as I needed to select a single entry using | random in the same expression (home automation is a bit finicky).

Other solutions I've thought about were a test that allows to regex-check a string (this would also cover the startswith and endswith cases, and is in general very versatile, but restricted to strings), or the higher-order function flip (rejected in #906) to just flip the arguments of the already existing in filter. I think the contains filter is useful, as it seems to be the only binary operator that's missing. There are tests for a < b and if I want the flipped version, there is a test for a >= b. There is a test for a in b but the b in a test is missing.

I am happy to write the PR myself. That's not too too much work. The contributing guideline suggested though, that an issue is opened before any time is wasted for unwelcome features. :)

@sivel
Copy link
Contributor

sivel commented Dec 12, 2022

FWIW, we needed this a while back in ansible, and added our own in ansible/ansible@f728f2b

@Kobold
Copy link

Kobold commented Jun 29, 2023

I would like this, coming from a dbt project!

@pallets pallets deleted a comment from maweki Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants