[pypy-commit] pypy python-numpy: add test, implementation of fromiter, needs more tests

mattip noreply at buildbot.pypy.org
Sat Aug 11 22:48:34 CEST 2012


Author: mattip <matti.picus at gmail.com>
Branch: python-numpy
Changeset: r56706:3b1313ffa0b4
Date: 2012-08-11 23:45 +0300
http://bitbucket.org/pypy/pypy/changeset/3b1313ffa0b4/

Log:	add test, implementation of fromiter, needs more tests

diff --git a/lib_pypy/numpypy/multiarray/__init__.py b/lib_pypy/numpypy/multiarray/__init__.py
--- a/lib_pypy/numpypy/multiarray/__init__.py
+++ b/lib_pypy/numpypy/multiarray/__init__.py
@@ -54,5 +54,22 @@
         a = ndarray(a)
     if dtype is None:
         dtype = a.dtype
+    if order != 'K' and order != 'C':
+        raise ValueError('not implemented yet')
     #return zeros(a.shape, dtype=dtype, order=order, subok=subok)
     return zeros(a.shape, dtype=dtype)
+
+def fromiter(iterable, dtype, count=-1):
+    if count > 0:
+        retVal = ndarray(count, dtype=dtype)
+    else:
+        retVal = ndarray(1, dtype=dtype)
+    for i,value in enumerate(iterable):
+        if i>=count and count>0:
+            break
+        if i>= retVal.size:
+            tmp = ndarray(retVal.size*2, dtype = dtype)
+            tmp[:i] = retVal[:i]
+            retVal = tmp
+        retVal[i] = value
+    return retVal[:i+1]
diff --git a/lib_pypy/numpypy/test/test_multiarray.py b/lib_pypy/numpypy/test/test_multiarray.py
--- a/lib_pypy/numpypy/test/test_multiarray.py
+++ b/lib_pypy/numpypy/test/test_multiarray.py
@@ -1,24 +1,34 @@
 try:
     import _numpypy as np
     import numpypy.multiarray as multiarray
+    numpypy = True
 except:
     import numpy as np
     from numpy.core import multiarray
+    numpypy = False
 
 from py.test import raises
 
 def test_count_nonzero():
-   a = np.array([[1, 1], [1, 1]])
-   assert multiarray.count_nonzero(a) == 4
-   assert multiarray.count_nonzero('a') == 1
-   assert multiarray.count_nonzero(('a',2)) == 2 
+    a = np.array([[1, 1], [1, 1]])
+    assert multiarray.count_nonzero(a) == 4
+    assert multiarray.count_nonzero('a') == 1
+    assert multiarray.count_nonzero(('a',2)) == 2 
 
 def test_empty_like():
-   a = np.array([[1, 1], [1, 1]])
-   b = multiarray.empty_like(a)
-   b[0,0] = 100
-   assert b[0,0] != a[0,0]
-   assert b.shape == a.shape
-   assert b.dtype == a.dtype
-   b = multiarray.empty_like(a, dtype=float)
-   assert b.dtype == np.dtype(float)
+    a = np.array([[1, 1], [1, 1]])
+    b = multiarray.empty_like(a)
+    b[0,0] = 100
+    assert b[0,0] != a[0,0]
+    assert b.shape == a.shape
+    assert b.dtype == a.dtype
+    b = multiarray.empty_like(a, dtype=float)
+    assert b.dtype == np.dtype(float)
+    if numpypy:
+        raises(ValueError, multiarray.empty_like, a, order='F')
+
+def test_fromiter():
+    iterable = (x*x for x in range(5))
+    b = multiarray.fromiter(iterable, np.dtype(float))
+    assert b.dtype == np.dtype(float)
+    assert all(b == [0., 1., 4., 9., 16.]) == True


More information about the pypy-commit mailing list