def type_encode(arg): "Encode objects as lists in a way that can be decoded by 'type_decode'" if isinstance(arg, (list, tuple)): return [type_encode(a) for a in arg] elif is_class_instance(arg, 'AbstractGameState'): return ['AGS', map(type_encode, encode_AGS(arg))] elif is_class_instance(arg, 'ConnectFourBoard'): return ['C4B', encode_C4B(arg)] elif is_class_instance(arg, 'ToyTree'): return ['ToyTree', encode_ToyTree(arg)] elif is_class_instance(arg, 'AnytimeValue'): return ['AnytimeValue_history', type_encode(arg.history)] elif callable(arg): fn_name = arg.__name__ if fn_name == '<lambda>': print(' ** Note: Unfortunately, the online tester is unable to ' + 'accept lambda functions. To pass the online tests, use ' + 'named functions instead. **') elif fn_name not in function_dict: print( 'Error: function', fn_name, 'cannot be transmitted ' + 'to server. Please use a pre-defined function instead.') return ['callable', arg.__name__] else: return arg
def type_encode(arg): "Encode classes as lists in a way that can be decoded by 'type_decode'" if isinstance(arg, (list, tuple)): return [type_encode(a) for a in arg] elif is_class_instance(arg, 'AbstractGameState'): return ['AGS', map(type_encode, encode_AGS(arg))] elif is_class_instance(arg, 'ConnectFourBoard'): return ['C4B', encode_C4B(arg)] elif is_class_instance(arg, 'ToyTree'): return ['ToyTree', encode_ToyTree(arg)] elif is_class_instance(arg, 'AnytimeValue'): return ['AnytimeValue_history', type_encode(arg.history)] elif callable(arg): fn_name = arg.__name__ if fn_name == '<lambda>': print (' ** Note: Unfortunately, the online tester is unable to ' +'accept lambda functions. To pass the online tests, use ' +'named functions instead. **') elif fn_name not in function_dict: print ('Error: constraint function', fn_name, 'cannot be transmitted ' +'to server. Please use a pre-defined constraint function instead.') return ['callable', arg.__name__] else: return arg