X-Received: by 10.224.160.65 with SMTP id m1mr7339097qax.2.1365480065348; Mon, 08 Apr 2013 21:01:05 -0700 (PDT) X-Received: by 10.50.101.101 with SMTP id ff5mr1380270igb.11.1365480065312; Mon, 08 Apr 2013 21:01:05 -0700 (PDT) Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!t2no46720324qal.0!news-out.google.com!ef9ni18755qab.0!nntp.google.com!ca1no37216920qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Mon, 8 Apr 2013 21:01:04 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=125.22.43.11; posting-account=0usDrQoAAADJM5SVChr943kcqPHAKQZz NNTP-Posting-Host: 125.22.43.11 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <2ebc5368-8a73-41ed-b6f4-a2a39aae642f@googlegroups.com> Subject: Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' From: bhk755@gmail.com Injection-Date: Tue, 09 Apr 2013 04:01:05 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: csiph.com comp.lang.python:43110 On Monday, April 8, 2013 3:37:38 PM UTC+5:30, bhk...@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 > > Elements are self.element > > Cols in 4 > > Num of Cloumns is 4 > > !!!!!!!!!!!!!! i is 0 > > sixe is 4 > > type is e > > Elements are self.element > > Type is > > Traceback (most recent call last): > > File "C:\Python27\Lib\array2D.py", line 53, in > > 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.