<div dir="ltr"><div>Doesn't</div><div><br></div><b>all(map(list_of_filters, value))</b><div><br></div><div>do what we want here, then? Maybe <b>imap</b> if you want the early bailout behavior. </div><div><br></div><div>
I'm all for having <b>all</b>, <b>map</b>, <b>any</b>, etc. be methods rather than top-level-functions (yay, less namespace pollution!), but if we're talking about a list of functions, it seems we can do exactly what we want very concisely using normal list- and function- operations.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jan 20, 2014 at 6:30 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Mon, Jan 20, 2014 at 05:51:36PM -0800, Haoyi Li wrote:<br>
> > What if you want to filter according to multiple conditions?<br>
><br>
> What's wrong with<br>
><br>
> lambda fname: func1(fname) and func2(fname) and func3(fname)<br>
<br>
</div>That is a single compound condition, not multiple conditions.<br>
<br>
Think about a GUI application with a file selection dialog box, or a<br>
search utility. You might offer a rich set of filters, all optional, all<br>
selectable by the user at runtime:<br>
<br>
<br>
[x] Hidden dot files .foo<br>
[ ] Backup files foo~<br>
[x] File extensions:<br>
[ ] Images<br>
[x] Text files<br>
[ ] Java code<br>
[x] Custom: [ zip,tar,foo,bar,baz ]<br>
[x] File owner: [ steve ]<br>
[ ] Group: [ ]<br>
[ ] Modified date between: [ ] and [ ]<br>
<br>
<br>
etc. It's not practical to create one single giant filter function that<br>
looks like this:<br>
<br>
def filter(name):<br>
head, ext = os.path.splitext(name)<br>
return (<br>
(show_hidden_dot_files and name.startswith('.'))<br>
and (show_backup_tilde_files and name.endswith('~'))<br>
and (show_images and ext in list_of_image_extensions)<br>
and ...<br>
)<br>
<br>
<br>
It would be a pain to maintain and extend, and testing would be<br>
horrible. Better to have each setting provide a single filter function,<br>
then combine the active filters into a list:<br>
<br>
def filter(name, list_of_filters):<br>
for f in list_of_filters:<br>
if not f(name):<br>
return False<br>
return True<br>
<br>
One might even use a class to represent the list of filters, and give it<br>
"all" and "any" methods, and allow multiple lists to combine so you can<br>
say things like:<br>
<br>
"show the file if *all* of these conditions are true, or if *any* of<br>
these different conditions are true, but not if *any* of these<br>
conditions are true"<br>
<br>
which of course is terribly overkill for a simple file selection dialog<br>
box, but might be useful for a more complex search engine.<br>
<br>
None of this should be read as supporting the original request to add<br>
PredicateSet into the standard library. But I encourage the OP to write<br>
his own library and put it on PyPI.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
--<br>
Steven<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>