Author: Timo Paulssen <timona...@perpetuum-immobile.de> Branch: numpy-data-buffer Changeset: r47794:fb2e36c041b7 Date: 2011-10-03 16:36 +0200 http://bitbucket.org/pypy/pypy/changeset/fb2e36c041b7/
Log: implement using separators for fromstring. diff --git a/lib_pypy/numpy/__init__.py b/lib_pypy/numpy/__init__.py --- a/lib_pypy/numpy/__init__.py +++ b/lib_pypy/numpy/__init__.py @@ -113,7 +113,26 @@ def fromstring(s, dtype=float, count=-1, sep=''): if sep: - raise NotImplementedError("Cannot use fromstring with a separator yet") + import numpy as np + dtype = np.dtype(dtype) + + parts = s.split(sep) + clean_parts = [part for part in parts if part] + if count >= 0: + clean_parts = clean_parts[:count] + + if dtype.kind == "f": + cast_func = float + elif dtype.kind == "i": + cast_func = int + else: + raise TypeError("Can only read int-likes or float-likes from strings.") + + result = empty(len(clean_parts), dtype=dtype) + for number, value in enumerate(clean_parts): + result[number] = cast_func(value) + + return result return __from_buffer_or_datastring(s, dtype, count) diff --git a/lib_pypy/pypy_test/test_numpy.py b/lib_pypy/pypy_test/test_numpy.py --- a/lib_pypy/pypy_test/test_numpy.py +++ b/lib_pypy/pypy_test/test_numpy.py @@ -97,6 +97,34 @@ for i in range(4): assert a[i] == i + data = "0, 1, 2, 3, 4, 5, 6" + a = fromstring(data, dtype="i", sep=",") + assert len(a) == 7 + assert list(a) == range(7) + + data = "0,1,2,3,4,5,6" + a = fromstring(data, dtype="i", sep=",") + assert len(a) == 7 + assert list(a) == range(7) + + data = "0, 1, 2, 3, 4, 5, 6" + a = fromstring(data, dtype="i", sep=",") + assert len(a) == 7 + assert list(a) == range(7) + + data = "0 X 1 X 2 X 3 X 4 X 5 X 6" + a = fromstring(data, dtype="i", sep="X", count=4) + assert len(a) == 4 + assert list(a) == range(4) + + fdata = [f / 5.0 for f in range(10)] + data = ",".join(str(f) for f in fdata) + a = fromstring(data, dtype=float, sep=",") + assert list(a) == fdata + + a = fromstring(data, dtype=float, sep=",", count=3) + assert list(a) == fdata[:3] + def test_frombuffer(self): from numpy import frombuffer import struct _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit