[Python-ideas] dir with a glob?

Nick Coghlan ncoghlan at gmail.com
Thu Jun 30 15:34:00 CEST 2011


On Thu, Jun 30, 2011 at 9:38 PM, Sturla Molden <sturla at molden.no> wrote:
> Often when exploring an object with the 'dir' function, particularly in
> large packages like SciPy, I find that I need to filter the outout. Since a
> dir reminds me of a dos 'dir' or linux 'ls', a glob feels like the most
> natural to use.
>
> For example, none of these would work:
>
>>>> dir(sp.fft.i*)  # syntax error
>>>> dir('sp.fft.i*') # returns the attributes a string

import fnmatch
def glob_dir(obj, pattern):
    return fnmatch.filter(dir(obj), pattern)

>>> print(glob_dir(str, "c*"))
['capitalize', 'center', 'count']

If it's something you use often, drop it into a utility module or PYTHONSTARTUP

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list