예제 #1
0
def csv_to_numpy(string_like, dtype=None):  # type: (str) -> np.array
    """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
예제 #2
0
def array_to_csv(
        array_like):  # type: (np.array or Iterable or int or float) -> str
    """Convert an array like object to CSV.

    To understand better what an array like object is 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 test_install_fails(check_error, entry_point_type_module):
    check_error.side_effect = _errors.ClientError()
    with pytest.raises(_errors.ClientError):
        entry_point.install('git://aws/container-support', 'script')
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 fail():
     raise _errors.ClientError(os.errno.ENOENT, 'No such file or directory')