[pypy-svn] r18593 - pypy/dist/pypy/module/Numeric
aft at codespeak.net
aft at codespeak.net
Sat Oct 15 00:54:15 CEST 2005
Author: aft
Date: Sat Oct 15 00:54:03 2005
New Revision: 18593
Modified:
pypy/dist/pypy/module/Numeric/__init__.py
pypy/dist/pypy/module/Numeric/interp_numeric.py
pypy/dist/pypy/module/Numeric/test_numeric.py
Log:
made import Numeric use new interp-space module, some simple tests to begin derivation of array type
Modified: pypy/dist/pypy/module/Numeric/__init__.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/__init__.py (original)
+++ pypy/dist/pypy/module/Numeric/__init__.py Sat Oct 15 00:54:03 2005
@@ -1,4 +1,4 @@
-from pypy.interpreter.mixedmodule import MixedModule
+from pypy.interpreter.mixedmodule import MixedModule
class Module(MixedModule):
"""An RPython reimplementation of the Numeric module
@@ -12,6 +12,8 @@
'Int' : "space.wrap('l')",
# 'array' : 'interp_numeric.w_array',
'zeros' : 'interp_numeric.w_zeros',
+ 'nzeros' : 'interp_numeric.w_nzeros',
+ 'array' : 'interp_numeric.w_array',
}
## 'CODESIZE': 'space.wrap(interp_sre.CODESIZE)',
Modified: pypy/dist/pypy/module/Numeric/interp_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/interp_numeric.py (original)
+++ pypy/dist/pypy/module/Numeric/interp_numeric.py Sat Oct 15 00:54:03 2005
@@ -1,3 +1,5 @@
+
+
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.typedef import GetSetProperty, TypeDef
from pypy.interpreter.typedef import interp_attrproperty, interp_attrproperty_w
@@ -28,21 +30,19 @@
self.strides.append( stride )
self.strides.reverse()
+ def check_space_true(self, space, w_index):
+ if not space.is_true(space.isinstance( w_index, space.w_int )):
+ raise NotImplementedError
+ idx = space.unwrap( w_index )
+ assert isinstance( idx, int )
+ return idx
+
def descr___getitem__( self, space, w_index ):
- if space.is_true(space.isinstance( w_index, space.w_int )):
- idx = space.unwrap( w_index )
- assert isinstance( idx, int )
- return self.get_single_item( space, [ idx ] )
- raise NotImplementedError
+ return self.get_single_item( space, [ self.check_space_true( space, w_index)])
def descr___setitem__( self, space, w_index, w_value ):
- if space.is_true(space.isinstance( w_index, space.w_int )):
- idx = space.unwrap( w_index )
- assert isinstance( idx, int )
- return self.set_single_item( space, [ idx ], w_value )
- raise NotImplementedError
+ return self.set_single_item( space, [ self.check_space_true( space, w_index) ], w_value )
-
def fget_shape( space, self ):
return space.newtuple( [ self.space.wrap( i ) for i in self.dims ] )
@@ -97,7 +97,7 @@
__setitem__ = descr___setitem__,
)
-W_Array_Float.typedef = TypeDef("W_Array_Float", W_Array.typedef,
+W_Array_Float.typedef = TypeDef("W_Array_Float", W_Array.typedef,
)
def w_zeros( space, w_dim_tuple, type_str ):
@@ -109,3 +109,107 @@
raise OperationError( space.w_ValueError, space.wrap('Unknown type code') )
w_zeros.unwrap_spec = [ ObjSpace, W_Root, str ]
+
+
+"""
+"""
+class W_NumericArray(Wrappable):
+
+ def __init__(self, space, dims ):
+ self.space = space
+ assert isinstance(dims, list)
+ self.dims = dims
+ self.strides = [1]
+ self.base_object = None
+ self.base_offset = 0 # needed later for offseting into a shared storage
+ stride = 1
+ for n in self.dims[:-1]:
+ stride *= n
+ self.strides.append( stride )
+ self.strides.reverse()
+
+ def check_space_true(self, space, w_index):
+ if not space.is_true(space.isinstance( w_index, space.w_int )):
+ raise NotImplementedError
+ idx = space.unwrap( w_index )
+ assert isinstance( idx, int )
+ return idx
+
+ def descr___getitem__( self, space, w_index ):
+ return self.get_single_item( space, [ self.check_space_true( space, w_index)])
+
+ def descr___setitem__( self, space, w_index, w_value ):
+ return self.set_single_item( space, [ self.check_space_true( space, w_index) ], w_value )
+
+ def fget_shape( space, self ):
+ return space.newtuple( [ self.space.wrap( i ) for i in self.dims ] )
+
+ def fset_shape( space, self, w_tuple ):
+ pass
+
+ def get_array_offset( self, idx_tuple ):
+ if len(idx_tuple)>len(self.dims):
+ # TODO raise OperationError
+ raise RuntimeError
+ idx = 0
+ for i in range(len(idx_tuple)):
+ idx += self.strides[i]*idx_tuple[i]
+ return idx
+
+
+class W_NumericArray_Float(W_NumericArray):
+
+ def __init__(self, space, dims, storage=None ):
+ W_NumericArray.__init__(self, space, dims )
+ storage_size = get_storage_size(dims)
+ self.storage = []
+ if storage is not None:
+ assert isinstance(storage, list)
+ # TODO return proper exception here
+ assert len(storage)==storage_size
+ assert isinstance(storage[0], float)
+ self.storage = storage
+ else:
+ self.storage = [0.0]*storage_size
+
+ def get_single_item( self, space, idx_tuple ):
+ if len(idx_tuple)!=len(self.dims):
+ # TODO raise OperationError or remove this and make it a pre-condition
+ raise RuntimeError
+ idx = self.get_array_offset( idx_tuple )
+ return space.wrap( self.storage[idx] )
+
+ def set_single_item( self, space, idx_tuple, w_value ):
+ idx = self.get_array_offset( idx_tuple )
+ value = space.float_w( w_value )
+ self.storage[idx] = value
+
+descr___getitem__ = interp2app( W_NumericArray.descr___getitem__, unwrap_spec=['self', ObjSpace, W_Root ] )
+descr___setitem__ = interp2app( W_NumericArray.descr___setitem__, unwrap_spec=['self', ObjSpace, W_Root, W_Root ] )
+
+
+W_NumericArray.typedef = TypeDef("W_NumericArray",
+ shape = GetSetProperty( W_NumericArray.fget_shape, cls=W_NumericArray),
+ __getitem__ = descr___getitem__,
+ __setitem__ = descr___setitem__,
+ )
+
+W_NumericArray_Float.typedef = TypeDef("W_NumericArray_Float", W_NumericArray.typedef,
+ )
+
+def w_nzeros( space, w_dim_tuple, type_str ):
+ dims = []
+ for w_int in space.unpackiterable(w_dim_tuple):
+ dims.append( space.unwrap( w_int ) )
+ if type_str == 'd':
+ return space.wrap(W_NumericArray_Float( space, dims ))
+ raise OperationError( space.w_ValueError, space.wrap('Unknown type code') )
+
+w_nzeros.unwrap_spec = [ ObjSpace, W_Root, str ]
+
+
+def w_array( space, w_dim_tuple):
+ raise OperationError( space.w_ValueError, space.wrap('Cannot create void array'))
+
+w_array.unwrap_spec = [ ObjSpace, W_Root ]
+
Modified: pypy/dist/pypy/module/Numeric/test_numeric.py
==============================================================================
--- pypy/dist/pypy/module/Numeric/test_numeric.py (original)
+++ pypy/dist/pypy/module/Numeric/test_numeric.py Sat Oct 15 00:54:03 2005
@@ -1,15 +1,45 @@
-from Numeric import zeros, Float
+from Numeric import zeros,nzeros,array
+from Numeric import Float
-a = zeros( (3,2), Float )
-print a.shape
-assert a.shape == (3,2)
+class TestArray:
+ def test(self):
+ a = zeros( (3,2), Float )
+ assert (3,2) == a.shape
-b = zeros( (8,), Float )
+ b = zeros( (8,), Float )
+ assert 0.==b[1]
+ b[1]= 1.
+ assert 1.==b[1]
-print b[1], b[2]
-b[1] = 1.
-print b[1], b[2]
+ def testZeros(self):
+ pass
+
+TestArray().test()
+TestArray().testZeros()
+#### Original test above.
+
+a=nzeros((2,7),Float)
+
+assert (2,7)== a.shape
+b=nzeros((10,),Float)
+assert 0.==b[2]
+b[3]=555
+assert b[3]==555
+
+def assertRaises(block,exception=Exception,shout='This should raise an exception'):
+ try:
+ block()
+ except exception:
+ pass
+ else:
+ assert False,shout
+
+
+assertRaises(lambda :array(()),exception=ValueError) #this should fail
+
+a=array([1])
+#assert a[0]==1 last test broken...
More information about the Pypy-commit
mailing list