steve at pearwood.info
Wed May 17 21:00:40 EDT 2017
On Wed, May 17, 2017 at 12:14:05PM -0400, Alex Walters 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.
At the cost of a slight inefficiency, you could use the pure Python
equivalent given in the docs:
Return the subset of the list of names that match pattern. It is
the same as [n for n in names if fnmatch(n, pattern)], but implemented
So your filter_false is:
[n for n in names if not fnmatch(n, pattern)]
which avoids the need for the copy-and-paste anti-pattern.
Otherwise, I would support:
- or a glob symbol to reverse the sense of the test, e.g. ~ or !
as the first character;
but I dislike functions that take boolean arguments to change their
behaviour. It's not a 100% hard and fast rule, but in general I prefer
to avoid functions that take a constant bool argument:
# rather than this:
# use this:
(although there may be exceptions).
More information about the Python-ideas