def traverse_data(obj, is_numpy=is_numpy, use_numpy=True):
 is_numpy = (is_numpy and use_numpy)
 if (is_numpy and all((isinstance(el, np.ndarray) for el in obj))):
  return [transform_array(el) for el in obj]
 obj_copy = []
 for item in obj:
  if isinstance(item, (list, tuple)):
   obj_copy.append(traverse_data(item))
  elif isinstance(item, float):
   if np.isnan(item):
    item = 'NaN'
   elif np.isposinf(item):
    item = 'Infinity'
   elif np.isneginf(item):
    item = '-Infinity'
   obj_copy.append(item)
  else:
   obj_copy.append(item)
 return obj_copy

def traverse_data ( obj , is_numpy = is_numpy , use_numpy = True ) : is_numpy = ( is_numpy and use_numpy ) if ( is_numpy and all ( ( isinstance ( el , np . ndarray ) for el in obj ) ) ) : return [ transform_array ( el ) for el in obj ] obj_copy = [ ] for item in obj : if isinstance ( item , ( list , tuple ) ) : obj_copy . append ( traverse_data ( item ) ) elif isinstance ( item , float ) : if np . isnan ( item ) : item = 'NaN' elif np . isposinf ( item ) : item = 'Infinity' elif np . isneginf ( item ) : item = '-Infinity' obj_copy . append ( item ) else : obj_copy . append ( item ) return obj_copy
recursively traverse an object until a flat list is found .