<s> import sys <EOL> from xdress . utils import flatten <EOL> if sys . version_info [ <NUM_LIT:0> ] > <NUM_LIT:2> : <EOL> basestring = str <EOL> class MatchAny ( object ) : <EOL> """<STR_LIT>""" <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def __hash__ ( self ) : <EOL> return hash ( repr ( self ) ) <EOL> MatchAny = MatchAny ( ) <EOL> def matches ( pattern , t ) : <EOL> """<STR_LIT>""" <EOL> tm = TypeMatcher ( pattern ) <EOL> return tm . matches ( t ) <EOL> class TypeMatcher ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> self . _pattern = pattern <EOL> @ property <EOL> def pattern ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pattern <EOL> def __hash__ ( self ) : <EOL> return hash ( self . pattern ) <EOL> def matches ( self , t ) : <EOL> """<STR_LIT>""" <EOL> pattern = self . pattern <EOL> if pattern is MatchAny : <EOL> return True <EOL> if t is pattern : <EOL> return True <EOL> if pattern is None : <EOL> return False <EOL> if t == pattern : <EOL> return True <EOL> if isinstance ( pattern , basestring ) : <EOL> return False <EOL> if isinstance ( t , basestring ) or isinstance ( t , bool ) or isinstance ( t , int ) or isinstance ( t , float ) : <EOL> return False <EOL> if len ( t ) != len ( pattern ) : <EOL> return False <EOL> submatcher = TypeMatcher ( None ) <EOL> for subt , subpattern in zip ( t , pattern ) : <EOL> submatcher . _pattern = subpattern <EOL> if not submatcher . matches ( subt ) : <EOL> return False <EOL> return True <EOL> def flatmatches ( self , t ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if self . matches ( t ) : <EOL> return True <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> if isinstance ( t , basestring ) : <EOL> return self . matches ( t ) <EOL> elif isinstance ( t , ( tuple , list ) ) : <EOL> return any ( [ self . matches ( i ) for i in flatten ( t ) ] ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , TypeMatcher ) : <EOL> return self . _pattern == other . _pattern <EOL> else : <EOL> return self . _pattern == other <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , self . _pattern ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . __class__ . __name__ , self . _pattern ) </s>
