[Python-ideas] Allow filtered dir built in

Steve Barnes gadgetsteve at live.co.uk
Thu Jun 14 09:32:28 EDT 2018



On 14/06/2018 13:33, Steven D'Aprano wrote:
> 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.
> 
> 
> 
Obviously, I personally would be interested (probably goes without saying).
-- 
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

---
This email has been checked for viruses by AVG.
https://www.avg.com



More information about the Python-ideas mailing list