contains_any_in and contains_all_in

Hello, If we want to check if a string contains any/all of several other strings we have to use several or/and conditions or any/all. For any: if ('string1' in master_string or 'string2' in master_string or 'string3' in master_string): or if any(item in master_string for item in ['string1', 'string2', 'string3']): For all: if ('string1' in master_string and 'string2' in master_string and'string3' in master_string): or if all(item in master_string for item in ['string1', 'string2', 'string3']): I suggest adding some "sugar" to make it more readable by adding contains_any_in and contains_all_in to look like this For any: if master_string contains_any_in ['string1', 'string2', 'string3']: For all: if master_string contains_all_in ['string1', 'string2', 'string3]: What do you think? Thanks, JM

Here comes funcoperators again : if master_string -contains_any_in- ['string1', 'string2', 'string3']: Given from funcoperators import infix @infix def contains_any_in(string, iterable): return any(item in string for item in iterable) pip install funcoperators https://pypi.org/project/funcoperators/ robertvandeneynde.be Le mar. 23 avr. 2019 à 22:39, João Matos <jcrmatos@gmail.com> a écrit :

On 4/23/2019 4:39 PM, João Matos wrote:
Trivial with re module, which will answer the question in one pass.
Tougher. Are the strings guaranteed to not be prefixes of each other? Do you want to allow overlaps? Can do in one pass by compiling a new re every time an item is found. If overlaps not wanted, re.iterfind will find all occurrence of any, so feed to set and see if all found. -- Terry Jan Reedy

On 4/24/2019 12:50 PM, João Matos wrote:
The objective of the proposal is to increase readability.
Relational expressions are powerful and flexible and yes, people find them hard to read until they really learn the sublanguage. The solution is to isolate application-specific regexes in functions that you then use in the application.
IMO using re is even more unreadable than the and/or or any/all I mentioned.
Your multiple scan str-method solutions can also be wrapped if they are sufficient for an application. The stdlib provides basic building blocks. If we endlessly add simple functions to the stdlib, it will become harder and harder to learn.
quarta-feira, 24 de Abril de 2019 às 05:47:04 UTC+1, Robert Vanden Eynde escreveu:
[Me] Trivial with re module, which will answer the question in one pass.
For those who might find it non trivial.
re.search('|'.join(map(re.escape, ['string1', 'string2', 'string3'])), master_string)
This should be wrapped in an application-specific function, with whatever name and parameters one prefers. If this particular re example is not in Regular Expression HOWTO, it could be added. It is easy to forget the need to apply re.escape to general, non-identifier strings. -- Terry Jan Reedy

On 23/04/2019 21:39, João Matos wrote:
They sound more like string methods to me, by analogy with startswith() and endswith(): if master_string.contains_any('string1', 'string2', 'string3'): etc. The only question is whether this is a common enough requirement to justify their existence. I don't remember our recent discussion on suffices coming to much of a conclusion about that. Anyone? -- Rhodri James *-* Kynesim Ltd

Here comes funcoperators again : if master_string -contains_any_in- ['string1', 'string2', 'string3']: Given from funcoperators import infix @infix def contains_any_in(string, iterable): return any(item in string for item in iterable) pip install funcoperators https://pypi.org/project/funcoperators/ robertvandeneynde.be Le mar. 23 avr. 2019 à 22:39, João Matos <jcrmatos@gmail.com> a écrit :

On 4/23/2019 4:39 PM, João Matos wrote:
Trivial with re module, which will answer the question in one pass.
Tougher. Are the strings guaranteed to not be prefixes of each other? Do you want to allow overlaps? Can do in one pass by compiling a new re every time an item is found. If overlaps not wanted, re.iterfind will find all occurrence of any, so feed to set and see if all found. -- Terry Jan Reedy

On 4/24/2019 12:50 PM, João Matos wrote:
The objective of the proposal is to increase readability.
Relational expressions are powerful and flexible and yes, people find them hard to read until they really learn the sublanguage. The solution is to isolate application-specific regexes in functions that you then use in the application.
IMO using re is even more unreadable than the and/or or any/all I mentioned.
Your multiple scan str-method solutions can also be wrapped if they are sufficient for an application. The stdlib provides basic building blocks. If we endlessly add simple functions to the stdlib, it will become harder and harder to learn.
quarta-feira, 24 de Abril de 2019 às 05:47:04 UTC+1, Robert Vanden Eynde escreveu:
[Me] Trivial with re module, which will answer the question in one pass.
For those who might find it non trivial.
re.search('|'.join(map(re.escape, ['string1', 'string2', 'string3'])), master_string)
This should be wrapped in an application-specific function, with whatever name and parameters one prefers. If this particular re example is not in Regular Expression HOWTO, it could be added. It is easy to forget the need to apply re.escape to general, non-identifier strings. -- Terry Jan Reedy

On 23/04/2019 21:39, João Matos wrote:
They sound more like string methods to me, by analogy with startswith() and endswith(): if master_string.contains_any('string1', 'string2', 'string3'): etc. The only question is whether this is a common enough requirement to justify their existence. I don't remember our recent discussion on suffices coming to much of a conclusion about that. Anyone? -- Rhodri James *-* Kynesim Ltd
participants (4)
-
João Matos
-
Rhodri James
-
Robert Vanden Eynde
-
Terry Reedy