def csv_to_numpy(string_like, dtype=None):
    """Convert a CSV object to a numpy array.

    Args:
        string_like (str): CSV string.
        dtype (dtype, optional):  Data type of the resulting array. If None, the
                                  dtypes will be determined by the contents of
                                  each column, individually. This argument can
                                  only be used to 'upcast' the array.  For
                                  downcasting, use the .astype(t) method.
    Returns:
        (np.array): Numpy array.
    """
    try:
        stream = StringIO(string_like)
        reader = csv.reader(stream, delimiter=",", quotechar='"', doublequote=True, strict=True)
        array = np.array([row for row in reader]).squeeze()
        array = array.astype(dtype)
    except ValueError as e:
        if dtype is not None:
            raise errors.ClientError(
                "Error while writing numpy array: {}. dtype is: {}".format(e, dtype)
            )
    except Exception as e:
        raise errors.ClientError("Error while decoding csv: {}".format(e))
    return array
def array_to_csv(array_like):
    """Convert an array like object to CSV.

    To understand what an array-like object is, please see:
    https://docs.scipy.org/doc/numpy/user/basics.creation.html#converting-python-array-like-objects-to-numpy-arrays

    Args:
        array_like (np.array or Iterable or int or float): Array-like object to be converted to CSV.

    Returns:
        (str): Object serialized to CSV.
    """
    array = np.array(array_like)
    if len(array.shape) == 1:
        array = np.reshape(array, (array.shape[0], 1))  # pylint: disable=unsubscriptable-object

    try:
        stream = StringIO()
        writer = csv.writer(
            stream, lineterminator="\n", delimiter=",", quotechar='"', doublequote=True, strict=True
        )
        writer.writerows(array)
        return stream.getvalue()
    except csv.Error as e:
        raise errors.ClientError("Error while encoding csv: {}".format(e))
 def fail():
     raise errors.ClientError(errno.ENOENT, "No such file or directory")
예제 #4
0
def test_install_fails(check_error):
    check_error.side_effect = errors.ClientError()
    with pytest.raises(errors.ClientError):
        modules.install("git://aws/container-support")
예제 #5
0
def test_install_fails(check_error, prepare, entry_point_type_module):
    check_error.side_effect = errors.ClientError()
    with pytest.raises(errors.ClientError):
        entry_point.install("git://aws/container-support", "script")