I find the following 2 functions useful when programming, when browsing objects interactively like one would do with dir.
Importing this module allows one to restrict the output from dir to what's required. Note also the standard pprint module may be useful also.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | """I find this useful when programming, for browsing objects interactively
like one would do with dir. This module allows one to
restrict the output from dir to what's required.
You may find the standard pprint module of use also."""
def dire(o, pat=None):
"""like dir, but can filter results with re pat"""
names = dir(o)
if not pat:
return names
import re
pat = re.compile(pat)
def match(name, fn=pat.search):
return fn(name) is not None
return filter(match, names)
def ls(o, pat=None):
"""like dir, but can filter results with glob pat"""
names = dir(o)
if not pat:
return names
import fnmatch
def match(name, fn=fnmatch.fnmatch, pat=pat):
return fn(name, pat)
return filter(match, names)
|
Tags: filter, interpolation