def matcher( x, negate=False ):
    """ matcher( "string" / compiled re / func / dict / list / tuple / set )
        -> a uniform match func, see above
    """
    if x in ( "", ".*", "*" ):  # always match
        f = lambda _: True
    elif isinstance( x, basestring ):
        if x[0] in "<=>":
            f = compare_func( x )  # ">= NJ"
        else:
            f = re.compile( x ) .search  # not BSoup lambda s: s == x
    elif isinstance( x, _re_type ):
        f = x.search
    elif callable( x ):  # e.g. re.compile().match
        f = x
    elif isinstance( x, (list, tuple) ):
        f = set( x ).__contains__
    elif hasattr( x, "__contains__" ):  # dict, set -- care if they change later ?!
        f = x.__contains__
    elif x in ( 0, 1, True, False, None ):
        f = lambda _: x
    # matcher( matcher) == matcher
    else:
        assert 0, "matcher: %s must be one of: str re callable dict list tuple" % x
    if negate:
        return lambda x: not f( x )  # match this - that, cf goopat
    return f
 matcher( "string" / compiled re / func / dict / list / tuple / set )
        -> a uniform match func, see above
    
recipes/Python/576741_uniform_matcher_re_pattern__re__func__dict_/recipe-576741.py