<s> from __future__ import absolute_import , print_function <EOL> import token <EOL> import symbol <EOL> import parser <EOL> def issequence ( t ) : <EOL> return isinstance ( t , ( list , tuple ) ) <EOL> def int_to_symbol ( i ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return symbol . sym_name [ i ] <EOL> except KeyError : <EOL> return token . tok_name [ i ] <EOL> def translate_symbols ( ast_tuple ) : <EOL> """<STR_LIT>""" <EOL> new_list = [ ] <EOL> for item in ast_tuple : <EOL> if isinstance ( item , int ) : <EOL> new_list . append ( int_to_symbol ( item ) ) <EOL> elif issequence ( item ) : <EOL> new_list . append ( translate_symbols ( item ) ) <EOL> else : <EOL> new_list . append ( item ) <EOL> if isinstance ( ast_tuple , tuple ) : <EOL> return tuple ( new_list ) <EOL> else : <EOL> return new_list <EOL> def ast_to_string ( ast_seq ) : <EOL> """<STR_LIT>""" <EOL> output = '<STR_LIT>' <EOL> for item in ast_seq : <EOL> if isinstance ( item , str ) : <EOL> output = output + item <EOL> elif issequence ( item ) : <EOL> output = output + ast_to_string ( item ) <EOL> return output <EOL> def build_atom ( expr_string ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( expr_string , str ) : <EOL> ast = parser . expr ( expr_string ) . totuple ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> else : <EOL> ast = parser . expr ( repr ( expr_string ) ) . totuple ( ) [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> return ast <EOL> def atom_tuple ( expr_string ) : <EOL> return build_atom ( expr_string ) <EOL> def atom_list ( expr_string ) : <EOL> return tuples_to_lists ( build_atom ( expr_string ) ) <EOL> def find_first_pattern ( ast_tuple , pattern_list ) : <EOL> """<STR_LIT>""" <EOL> found , data = <NUM_LIT:0> , { } <EOL> if not isinstance ( pattern_list , list ) : <EOL> pattern_list = [ pattern_list ] <EOL> for pattern in pattern_list : <EOL> found , data = match ( pattern , ast_tuple ) <EOL> if found : <EOL> break <EOL> if not found : <EOL> for item in ast_tuple : <EOL> if issequence ( item ) : <EOL> found , data = find_first_pattern ( item , pattern_list ) <EOL> if found : <EOL> break <EOL> return found , data <EOL> name_pattern = ( token . NAME , [ '<STR_LIT>' ] ) <EOL> def remove_duplicates ( lst ) : <EOL> output = [ ] <EOL> for item in lst : <EOL> if item not in output : <EOL> output . append ( item ) <EOL> return output <EOL> reserved_names = [ '<STR_LIT>' ] <EOL> def remove_reserved_names ( lst ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> for item in lst : <EOL> if item not in reserved_names : <EOL> output . append ( item ) <EOL> return output <EOL> def harvest_variables ( ast_list ) : <EOL> """<STR_LIT>""" <EOL> variables = [ ] <EOL> if issequence ( ast_list ) : <EOL> found , data = match ( name_pattern , ast_list ) <EOL> if found : <EOL> variables . append ( data [ '<STR_LIT>' ] ) <EOL> for item in ast_list : <EOL> if issequence ( item ) : <EOL> variables . extend ( harvest_variables ( item ) ) <EOL> variables = remove_duplicates ( variables ) <EOL> variables = remove_reserved_names ( variables ) <EOL> return variables <EOL> def match ( pattern , data , vars = None ) : <EOL> """<STR_LIT>""" <EOL> if vars is None : <EOL> vars = { } <EOL> if isinstance ( pattern , list ) : <EOL> vars [ pattern [ <NUM_LIT:0> ] ] = data <EOL> return <NUM_LIT:1> , vars <EOL> if not isinstance ( pattern , tuple ) : <EOL> return ( pattern == data ) , vars <EOL> if len ( data ) != len ( pattern ) : <EOL> return <NUM_LIT:0> , vars <EOL> for pattern , data in zip ( pattern , data ) : <EOL> same , vars = match ( pattern , data , vars ) <EOL> if not same : <EOL> break <EOL> return same , vars <EOL> def tuples_to_lists ( ast_tuple ) : <EOL> """<STR_LIT>""" <EOL> if not issequence ( ast_tuple ) : <EOL> return ast_tuple <EOL> new_list = [ ] <EOL> for item in ast_tuple : <EOL> new_list . append ( tuples_to_lists ( item ) ) <EOL> return new_list <EOL> """<STR_LIT>""" </s>
