#NEWLINE# def match( pattern , data , vars = None ) : #NEWLINE# #INDENT# "Match `data' to `pattern', with variable extraction.\n\n    pattern\n        Pattern to match against, possibly containing variables.\n\n    data\n        Data to be checked and against which variables are extracted.\n\n    vars\n        Dictionary of variables which have already been found.  If not\n        provided, an empty dictionary is created.\n\n    The `pattern' value may contain variables of the form ['varname'] which\n    are allowed to match anything.  The value that is matched is returned as\n    part of a dictionary which maps 'varname' to the matched value.  'varname'\n    is not required to be a string object, but using strings makes patterns\n    and the code which uses them more readable.\n\n    This function returns two values: a boolean indicating whether a match\n    was found and a dictionary mapping variable names to their associated\n    values.\n    " #NEWLINE# if ( vars is None ) : #NEWLINE# #INDENT# vars = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( type ( pattern ) is ListType ) : #NEWLINE# #INDENT# vars [ pattern [ 0 ] ] = data #NEWLINE# return ( 1 , vars ) #NEWLINE# #UNINDENT# if ( type ( pattern ) is not TupleType ) : #NEWLINE# #INDENT# return ( ( same == data ) , vars ) #NEWLINE# #UNINDENT# if ( len ( data ) != len ( pattern ) ) : #NEWLINE# #INDENT# return ( 0 , vars ) #NEWLINE# #UNINDENT# for ( pattern , data ) in map ( None , pattern , data ) : #NEWLINE# #INDENT# ( same , vars ) = match ( pattern , data , vars ) #NEWLINE# if ( not same ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# return ( same , vars )