Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__'

bhk755 at gmail.com bhk755 at gmail.com
Tue Apr 9 00:01:04 EDT 2013


On Monday, April 8, 2013 3:37:38 PM UTC+5:30, bhk... at gmail.com wrote:
> I am trying to create 2D arrays without using advanced features like numpy, for this I have created 2 separate modules arrays.py and array2D.py. Here's the code for that:
> 
> 
> 
> arrays.py module:
> 
> ==================
> 
> import ctypes
> 
> 
> 
> class Array:
> 
> 
> 
> #Creates an array with size elements.
> 
>     def __init__( self, size ):
> 
>         assert size > 0, "Array size must be > 0"
> 
> 	self._size = size
> 
> 	print "sixe is %s" %self._size
> 
> 		
> 
>  # Create the array structure using the ctypes module.
> 
> 	PyArrayType = ctypes.c_int * size
> 
> 	self._elements = PyArrayType()
> 
> 	print "type is e", type(self._elements)
> 
> 	#self._elements = ctypes.c_int * size
> 
> 	
> 
> 	print "Elements are self.element %s" % self._elements
> 
> # Initialize each element.
> 
> 	#for i in range(self._size):
> 
> 	#	self.clear( i )
> 
> 			
> 
> 				
> 
> # Returns the size of the array.
> 
>     def __len__( self ):
> 
>         return self._size
> 
> 
> 
> # Gets the contents of the index element.
> 
>     def __getitem__( self, index ):
> 
>         assert index >= 0 and index < len(self), "Array subscript out of range"
> 
> 	return self._elements[ index ]
> 
> 
> 
> # Puts the value in the array element at index position.
> 
>     def __setitem__( self, index, value ):
> 
>         assert index >= 0 and index < len(self), "Array subscript out of range"
> 
> 	print "Type is ", type(index)
> 
> 	self._elements[ index ] = value
> 
> 
> 
> # Clears the array by setting each element to the given value.
> 
>     def clear( self, value ):
> 
>         for i in range( len(self) ) :
> 
> 	self._elements[i] = value
> 
> 
> 
> # Printing the arrays:
> 
>     def __str__(self):
> 
>         return self._elements
> 
> 
> 
> 			
> 
> 			
> 
> array2D.py module
> 
> ==================
> 
> 
> 
> 		
> 
> import arrays
> 
> 
> 
> class Array2D :
> 
>     # Creates a 2-D array of size numRows x numCols.
> 
>     def __init__( self, numRows, numCols ):
> 
>     # Create a 1-D array to store an array reference for each row.
> 
> 		
> 
>         self._theRows = arrays.Array( numRows )
> 
> 	# Create the 1-D arrays for each row of the 2-D array.
> 
> 	print "Num of Cloumns is", numCols
> 
> 		
> 
> 	for i in range( numRows ) :
> 
> 	    self._theRows[i] = arrays.Array( numCols )
> 
> 
> 
>     # Returns the number of rows in the 2-D array.
> 
>     def numRows( self ):
> 
>         return len( self._theRows )
> 
> 
> 
>     # Returns the number of columns in the 2-D array.
> 
>     def numCols( self ):
> 
>         return len( self._theRows[0] )
> 
> 
> 
>     # Clears the array by setting every element to the given value.
> 
>     def clear( self, value ):
> 
>         for row in range( self.numRows() ):
> 
>             row.clear( value )
> 
> 
> 
>     # Gets the contents of the element at position [i, j]
> 
>     def __getitem__( self, ndxTuple ):
> 
>         assert len(ndxTuple) == 2, "Invalid number of array subscripts."
> 
>         row = ndxTuple[0]
> 
>         col = ndxTuple[1]
> 
>         assert row >= 0 and row < self.numRows() \
> 
>             and col >= 0 and col < self.numCols(), \
> 
>                 "Array subscript out of range."
> 
>         the1dArray = self._theRows[row]
> 
>         return the1dArray[col]
> 
> 
> 
>     # Sets the contents of the element at position [i,j] to value.
> 
>     def __setitem__( self, ndxTuple, value ):
> 
>         #assert len(ndxTuple) == 3, "Invalid number of array subscripts."
> 
>         row = ndxTuple[0]
> 
>         col = ndxTuple[1]
> 
>         assert row >= 0 and row < self.numRows() \
> 
>             and col >= 0 and col < self.numCols(), \
> 
>                 "Array subscript out of range."
> 
>         the1dArray = self._theRows[row]
> 
>         the1dArray[col] = value
> 
> 		
> 
> 		
> 
> arr = Array2D(2,4)
> 
> 
> 
> print "arr is %s" %arr
> 
> 
> 
> 
> 
> Traceback is :
> 
> 
> 
> sixe is 2
> 
> type is e <class 'arrays.c_long_Array_2'>
> 
> Elements are self.element <arrays.c_long_Array_2 object at 0x00AA7F80>
> 
> Cols in 4
> 
> Num of Cloumns is 4
> 
> !!!!!!!!!!!!!! i is 0
> 
> sixe is 4
> 
> type is e <class 'arrays.c_long_Array_4'>
> 
> Elements are self.element <arrays.c_long_Array_4 object at 0x00B60210>
> 
> Type is  <type 'int'>
> 
> Traceback (most recent call last):
> 
>   File "C:\Python27\Lib\array2D.py", line 53, in <module>
> 
>     arr = Array2D(2,4)
> 
>   File "C:\Python27\Lib\array2D.py", line 16, in __init__
> 
>     self._theRows[i] = arrays.Array( numCols )
> 
>   File "C:\Python27\Lib\arrays.py", line 36, in __setitem__
> 
>     self._elements[ index ] = value
> 
> AttributeError: Array instance has no attribute '__trunc__'


Hi Dylan,

Thank you for the alternative solution. I will look into that. 



More information about the Python-list mailing list