"""
Data transfer methods
"""

import numpy as np

import cupy as cp


def _get_xp_args(ndarray_instance, to_xp, arg):
    """
    Converts ndarray_instance type object to target object using to_xp.
    ndarray_instance: numpy.ndarray, cupy.ndarray or utils.ndarray

    Args:
        (ndarray_instance, tuple, list, dict): These will be returned by
        either converting the object or it's elements if object is iterable.
        Everything else is returned as it is.

    Returns:
        Return data structure will be same as before after converting ndarrays.
    """

    if isinstance(arg, ndarray_instance):
        return to_xp(arg)

    if isinstance(arg, tuple):
        return tuple([_get_xp_args(ndarray_instance, to_xp, x) for x in arg])

    if isinstance(arg, dict):
        return {x_name: _get_xp_args(ndarray_instance, to_xp, x)
                for x_name, x in arg.items()}

    if isinstance(arg, list):
        return [_get_xp_args(ndarray_instance, to_xp, x) for x in arg]

    return arg


def _get_cupy_result(numpy_res):
    return _get_xp_args(np.ndarray, cp.array, numpy_res)


def _get_numpy_args(args, kwargs):
    return _get_xp_args(cp.ndarray, cp.asnumpy, (args, kwargs))

@@ -192,16 +192,31 @@
  to_xp.%0A
+%0A    Args:%0A    
     ndar
@@ -227,18 +227,18 @@
 instance
-:
  
+(
 numpy.nd
@@ -277,29 +277,189 @@
 rray
-%0A%0A    Args:%0A        (
+):%0A        Objects of type %60ndarray_instance%60 will be converted using %60to_xp%60.%0A        to_xp (FunctionType): Method to convert ndarray_instance type objects.%0A        arg (object): %60
 ndar
@@ -474,35 +474,58 @@
 ance
+%60
 , 
+%60
 tuple
+%60
 , 
+%60
 list
-, dict): These
+%60 and %60dict%60 type objects%0A       
  wil
@@ -540,24 +540,16 @@
 urned by
-%0A       
  either 
@@ -586,16 +586,24 @@
 elements
+%0A       
  if obje
@@ -621,32 +621,31 @@
 ble.
-%0A        Everything else
+ Objects of other types
  is 

Update _get_xp_args doc