Reshape, but also return scalars or empty lists.

    Identical to `numpy.reshape` except in the case where `newshape` is
    the empty tuple, in which case we return a scalar instead of a
    0-dimensional array.

    Examples
    --------
    >>> a = np.arange(6)
    >>> np.array_equal(np.reshape(a, (3, 2)), scalar_reshape(a, (3, 2)))
    True

    >>> scalar_reshape(np.array([3.14]), newshape=())
    3.14

    >>> scalar_reshape(np.array([2.71]), newshape=(1,))
    array([ 2.71])

    >>> scalar_reshape(np.array([]), newshape=(0,))
    []

def scalar_reshape(a, newshape, order='C'):
    """
    Reshape, but also return scalars or empty lists.

    Identical to `numpy.reshape` except in the case where `newshape` is
    the empty tuple, in which case we return a scalar instead of a
    0-dimensional array.

    Examples
    --------
    >>> a = np.arange(6)
    >>> np.array_equal(np.reshape(a, (3, 2)), scalar_reshape(a, (3, 2)))
    True

    >>> scalar_reshape(np.array([3.14]), newshape=())
    3.14

    >>> scalar_reshape(np.array([2.71]), newshape=(1,))
    array([ 2.71])

    >>> scalar_reshape(np.array([]), newshape=(0,))
    []
    """
    if newshape == ():
        return np.asscalar(a)

    if newshape == (0,):
        return []

    return np.reshape(a, newshape, order)