[Python-ideas] Allow filtered dir built in
steve at pearwood.info
Thu Jun 14 08:33:50 EDT 2018
On Thu, Jun 14, 2018 at 09:27:05AM +0000, Steve Barnes wrote:
> What I would like to suggest is extending the dir built-in to allow an
> optional filter parameter that takes fnmatch type wild card as an
> optional filter. Then I could use:
> >>> dir(mpmath, "*sin*")
> To narrow down the candidates.
I have exactly that in my Python startup file. It monkey-patches the
builtin dir with a custom wrapper function that has signature:
edir( [object, [glob='',]] *, meta=False, dunder=True, private=True)
For the glob, I support the following metacharacters:
- Reverse matching: if the glob begins with '!' or '!=', the
sense of the match is reversed to "don't match".
- Case-sensitive matching: if the glob begins with '=' or '!=',
perform a case-sensitive match. Otherwise filters are case-
insensitive by default.
- Wildcards: '?' to match a single character, '*' to match
zero or more characters.
- Character sets: e.g. '[abc]' to match any of 'a', 'b', 'c',
or '[!abc]' to match any character except 'a', 'b', 'c'.
If the glob argument contains no metacharacters apart from the ! and =
flags, a straight substring match is performed. So there's no need to
match on a glob "*foo*", I can just use "foo".
If there is interest in this, I will clean it up for public consumption,
and publish it somewhere as a third-party module, and for consideration
for the stdlib.
More information about the Python-ideas