import re


def prune( d, pattern=None, types=None ):
    """
    @author: geoff ely
    Delete dictionary keys with specified name pattern or types
    Default types are: functions and modules.
    >>> prune( {'a': 0, 'a_': 0, '_a': 0, 'a_a': 0, 'b': prune} )
    {'a_a': 0}
    """
    if pattern == None:
        pattern = '(^_)|(_$)|(^.$)'
    if types is None:
        types = type( re ), type( re.sub )
    grep = re.compile( pattern )
    d2 = d.copy()
    for k in d.keys():
        if grep.search( k ) or type( d[k] ) in types:
            del( d2[k] )
    return d2