<s> from __future__ import print_function <EOL> import numpy <EOL> import os <EOL> import errno <EOL> def import_class ( module_name ) : <EOL> mod_name , class_name = module_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod = __import__ ( mod_name , fromlist = [ class_name ] ) <EOL> klass = getattr ( mod , class_name ) <EOL> return klass <EOL> def import_function ( func_name ) : <EOL> mod_name , func_name = func_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod = __import__ ( mod_name , fromlist = [ func_name ] ) <EOL> func = getattr ( mod , func_name ) <EOL> return func <EOL> def call_function ( function , args ) : <EOL> return function ( * args ) <EOL> def import_and_call_function ( function_obj ) : <EOL> func = import_function ( function_obj [ '<STR_LIT>' ] ) <EOL> args = function_obj [ '<STR_LIT:args>' ] <EOL> return call_function ( func , args ) <EOL> def list_of_lists ( a_list ) : <EOL> if isinstance ( a_list , ( list , tuple , numpy . ndarray ) ) and len ( a_list ) > <NUM_LIT:0> and all ( [ isinstance ( l , ( list , tuple , numpy . ndarray ) ) for l in a_list ] ) : <EOL> return True <EOL> return False <EOL> def call_for_each_element ( data , function , args = [ ] , data_type = '<STR_LIT>' ) : <EOL> if data_type == '<STR_LIT>' : <EOL> return function ( data , * args ) <EOL> elif data_type == '<STR_LIT>' : <EOL> assert ( list_of_lists ( data ) ) <EOL> return [ function ( d , * args ) for d in data ] <EOL> elif data_type == '<STR_LIT>' : <EOL> assert ( type ( data ) == dict ) <EOL> return { token : function ( contexts , * args ) for token , contexts in data . items ( ) } <EOL> def function_tree ( func , args ) : <EOL> for idx , arg in enumerate ( args ) : <EOL> if type ( arg ) is dict and '<STR_LIT:type>' in arg and arg [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> inner_func = import_function ( arg [ '<STR_LIT>' ] ) <EOL> args [ idx ] = function_tree ( inner_func , arg [ '<STR_LIT:args>' ] ) <EOL> return call_function ( func , args ) <EOL> def build_object ( obj_info , root_element = '<STR_LIT>' ) : <EOL> print ( "<STR_LIT>" , obj_info [ root_element ] ) <EOL> klass = import_class ( obj_info [ root_element ] ) <EOL> input_args = obj_info [ '<STR_LIT:args>' ] if '<STR_LIT:args>' in obj_info else [ ] <EOL> for idx , arg in enumerate ( input_args ) : <EOL> if type ( arg ) is dict and '<STR_LIT:type>' in arg and arg [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> func = import_function ( arg [ '<STR_LIT>' ] ) <EOL> input_args [ idx ] = function_tree ( func , arg [ '<STR_LIT:args>' ] ) <EOL> obj = klass ( * input_args ) <EOL> return obj <EOL> def build_objects ( object_list , root_element = '<STR_LIT>' ) : <EOL> objects = [ ] <EOL> for obj_info in object_list : <EOL> obj = build_object ( obj_info ) <EOL> objects . append ( obj ) <EOL> return objects <EOL> def mk_tmp_dir ( tmp_dir ) : <EOL> if tmp_dir is None : <EOL> tmp_dir = os . getcwd ( ) + '<STR_LIT>' <EOL> try : <EOL> os . makedirs ( tmp_dir ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( tmp_dir ) : <EOL> pass <EOL> else : <EOL> raise <EOL> return tmp_dir </s>
