[pypy-commit] pypy numpy-back-to-applevel: Start working towards ravel and applevel array_string, nothing works!
fijal
noreply at buildbot.pypy.org
Sat Jan 21 23:11:16 CET 2012
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-back-to-applevel
Changeset: r51613:eb5dcdf724f3
Date: 2012-01-21 22:44 +0200
http://bitbucket.org/pypy/pypy/changeset/eb5dcdf724f3/
Log: Start working towards ravel and applevel array_string, nothing
works!
diff --git a/lib_pypy/numpypy/core/fromnumeric.py b/lib_pypy/numpypy/core/fromnumeric.py
--- a/lib_pypy/numpypy/core/fromnumeric.py
+++ b/lib_pypy/numpypy/core/fromnumeric.py
@@ -1054,8 +1054,9 @@
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
"""
- raise NotImplementedError('Waiting on interp level method')
-
+ if not hasattr(a, 'ravel'):
+ a = numpypy.array(a)
+ return a.ravel(order=order)
def nonzero(a):
"""
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -280,6 +280,11 @@
"len() of unsized object"))
def descr_repr(self, space):
+ cache = get_appbridge_cache(space)
+ if cache.w_array_repr is None:
+ return space.wrap(self.dump_data())
+ return space.call_function(cache.w_array_repr, self)
+
res = StringBuilder()
res.append("array(")
concrete = self.get_concrete_or_scalar()
@@ -302,11 +307,26 @@
res.append(")")
return space.wrap(res.build())
+ def dump_data(self):
+ concr = self.get_concrete()
+ i = concr.create_iter()
+ first = True
+ s = StringBuilder()
+ s.append('array([')
+ while not i.done():
+ if first:
+ first = False
+ else:
+ s.append(', ')
+ s.append(concr.dtype.itemtype.str_format(concr.getitem(i.offset)))
+ i = i.next(len(concr.shape))
+ s.append('])')
+ return s.build()
+
def descr_str(self, space):
cache = get_appbridge_cache(space)
if cache.w_array_str is None:
- raise OperationError(space.w_RuntimeError, space.wrap(
- "str function not set"))
+ return space.wrap(self.dump_data())
return space.call_function(cache.w_array_str, self)
ret = StringBuilder()
@@ -546,6 +566,12 @@
return space.wrap(W_NDimSlice(concrete.start, strides,
backstrides, shape, concrete))
+ def descr_ravel(self, space, w_order=None):
+ if not space.is_w(w_order, space.w_None):
+ raise OperationError(space.w_NotImplementedError, space.wrap(
+ "order not implemented"))
+ return self.descr_reshape(space, [space.wrap(-1)])
+
def descr_get_flatiter(self, space):
return space.wrap(W_FlatIterator(self))
@@ -1251,6 +1277,7 @@
T = GetSetProperty(BaseArray.descr_get_transpose),
flat = GetSetProperty(BaseArray.descr_get_flatiter),
+ ravel = interp2app(BaseArray.descr_ravel),
mean = interp2app(BaseArray.descr_mean),
sum = interp2app(BaseArray.descr_sum),
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -1382,6 +1382,14 @@
assert array(x, copy=False) is x
assert array(x, copy=True) is not x
+ def test_ravel(self):
+ from _numpypy import arange
+ assert (arange(3).ravel() == arange(3)).all()
+ assert (arange(6).reshape(2, 3).ravel() == arange(6)).all()
+ print arange(6).reshape(2, 3).T.ravel()
+ assert (arange(6).reshape(2, 3).T.ravel() == [0, 3, 1, 4, 2, 5]).all()
+
+
class AppTestSupport(BaseNumpyAppTest):
def setup_class(cls):
import struct
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_fromnumeric.py
@@ -137,3 +137,6 @@
# x = ones((1, 2, 3))
# assert transpose(x, (1, 0, 2)).shape == (2, 1, 3)
+ def test_ravel(self):
+ from numpypy import ravel
+
diff --git a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
--- a/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
+++ b/pypy/module/test_lib_pypy/numpypy/core/test_numeric.py
@@ -19,3 +19,8 @@
assert base_repr(-12, 10) == '-12'
assert base_repr(-12, 10, 4) == '-000012'
assert base_repr(-12, 4) == '-30'
+
+class AppTestRepr(BaseNumpyAppTest):
+ def test_repr(self):
+ from numpypy import array
+ assert repr(array([1, 2, 3, 4])) == 'array([1, 2, 3, 4])'
More information about the pypy-commit
mailing list