
Chris Angelico writes:
On Sat, Nov 3, 2018 at 4:49 AM Stephen J. Turnbull <turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
Andre Delfino writes:
Frequently, while globbing, one needs to work with multiple extensions. I’d like to propose for fnmatch.filter to handle a tuple of patterns (while preserving the single str argument functionality, alas str.endswith),
This is one of those famous 3-line functions, though:
import fnmatch def multifilter(names, *patterns): result = [] for p in patterns: result.extend(fnmatch.filter(names, p)) return result
It's a 3-line function in 5 lines, OK, but still.
And like many "hey it's this easy" demonstrations, that isn't quite identical, as a single file can match multiple patterns
Sure. I would have written it with set.union() on general principles except I forgot how to say "union", didn't feel like looking it up, and wanted to keep the def as close to 3 lines as I could without being obfuscated (see below). I wonder how many people would fall into the trap I did. (I don't consider myself a great programmer, but maybe that's all the more reason for this? Not-so-great minds think alike? :-) I was really more interested in the second question, though. Why invent yet another interface when we already have one that is well-known and more powerful? P.S. I can't resist. This is horrible, but: def multifilter(names, *patterns): return list(set().union(*[fnmatch.filter(names, p) for p in patterns])) Who even needs a function? ;-)