Commits

mattip committed 3b1313f

add test, implementation of fromiter, needs more tests

  • Participants
  • Parent commits 35ce341
  • Branches python-numpy

Comments (0)

Files changed (2)

lib_pypy/numpypy/multiarray/__init__.py

         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]

lib_pypy/numpypy/test/test_multiarray.py

 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