Top posting, apologies. I'm sure there is a better way to do it, and there is a performance hit, but its negligible. This is also a three line delta of the function.
from fnmatch import _compile_pattern, filter as old_filter import os import os.path import posixpath data = os.listdir() def filter(names, pat, *, invert=False): """Return the subset of the list NAMES that match PAT.""" result = [] pat = os.path.normcase(pat) match = _compile_pattern(pat) if os.path is posixpath: # normcase on posix is NOP. Optimize it away from the loop. for name in names: if bool(match(name)) == (not invert): result.append(name) else: for name in names: if bool(match(os.path.normcase(name))) == (not invert): result.append(name) return result if __name__ == '__main__': import timeit print(timeit.timeit( "filter(data, '__*')", setup="from __main__ import filter, data" )) print(timeit.timeit( "filter(data, '__*')", setup="from __main__ import old_filter as filter, data" )) The first test (modified code) timed at 22.492161903402575, where the second test (unmodified) timed at 19.555531892032324 > -----Original Message----- > From: tritium-l...@sdamon.com [mailto:tritium-l...@sdamon.com] > Sent: Wednesday, May 17, 2017 1:19 PM > To: python-ideas@python.org > Subject: RE: [Python-ideas] fnmatch.filter_false > > > -----Original Message----- > > From: Python-ideas [mailto:python-ideas-bounces+tritium- > > list=sdamon....@python.org] On Behalf Of Oleg Broytman > > Sent: Wednesday, May 17, 2017 12:44 PM > > To: python-ideas@python.org > > Subject: Re: [Python-ideas] fnmatch.filter_false > > > > On Wed, May 17, 2017 at 12:14:05PM -0400, Alex Walters <tritium- > > l...@sdamon.com> wrote: > > > Fnmath.filter works great. To remind people what it does, it takes an > > > iterable of strings and a pattern and returns a list of the strings that > > > match the pattern. And that is wonderful > > > > > > However, I often need to filter *out* the items that match the pattern > (to > > > ignore them). In every project that I need this I end up copying the > > > function out of the fnmatch library and adding 'not' to the test clause. > It > > > would be wonderful if there was a filter_false version in the standard > > > library. Or in inversion Boolean option. Or something, to stop from > having > > > to copy code every time I need to ignore files. > > > > Why not create a package and publish at PyPI? Then all you need is > > pip install fnmatch_filter_false > > in your virtual env. > > That is my normal thought on something like this, but in the case of adding > a Boolean argument to fnmatch.filter, it (might be) as simple as a 3 line > diff that does not break the API, and as far as I can tell, does not have > performance implications. Copying a module out of the standard library that > is identical except a 3 line diff that does not break compatibility with the > standard library... that just wreaks of something that should be in the > standard library to begin with. > > In the case of adding a separate function to fnmatch, it's still not that > big of a diff, and wouldn't have much duplicated code, at least in the way I > would implement it - it would essentially do the previous Boolean option > method, and wrap that. A filter_false function, now that I think about it, > is less ideal than just adding a keyword only Boolean option to > fnmatch.filter. > > > Oleg. > > -- > > Oleg Broytman http://phdru.name/ p...@phdru.name > > Programmers don't die, they just GOSUB without RETURN. > > _______________________________________________ > > Python-ideas mailing list > > Python-ideas@python.org > > https://mail.python.org/mailman/listinfo/python-ideas > > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/