def grep( matchf, afile, header="", indent="", trailer="", out=sys.stdout ):
    """ print lines matching matchf ("re" / re / func / dict) + header and trailer
        -> nmatch
        out None: just return 1 on first match / 0
    """
    matchf = matcher( matchf )
    if isinstance( afile, basestring ):
        afile = open( afile )  # IOError: [Errno 2] No such file or directory
    nmatch = 0
    for line in afile:
        if matchf( line ):
            if out is None:
                return 1
            if header:
                print >>out, header
                header = None
            print "%s%s" % (indent, line)
            nmatch += 1
    if nmatch and out and trailer:
        print >>out, trailer.rstrip( " " )
    return nmatch
 print lines matching matchf ("re" / re / func / dict) + header and trailer
        -> nmatch
        out None: just return 1 on first match / 0
    
recipes/Python/576741_uniform_matcher_re_pattern__re__func__dict_/recipe-576741.py