Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; '16,': 0.03; '"the': 0.07; 'attribute': 0.07; 'element': 0.07; 'elements.': 0.07; 'initialize': 0.07; 'puts': 0.07; 'subject:help': 0.08; '%s"': 0.09; '__init__': 0.09; 'arrays': 0.09; 'rows': 0.09; 'subject:instance': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; 'suggest': 0.14; 'creates': 0.14; '36,': 0.16; 'array.': 0.16; 'clears': 0.16; 'col': 0.16; 'columns': 0.16; 'command,': 0.16; 'element.': 0.16; 'it."': 0.16; 'len(self)': 0.16; 'elements': 0.16; 'index': 0.16; 'skip:= 10': 0.16; 'wrote:': 0.18; 'users.': 0.18; 'bit': 0.19; 'module': 0.19; 'trying': 0.19; '<': 0.19; 'value.': 0.19; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'simpler': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'skip:" 20': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'unix': 0.29; 'andrew': 0.30; 'sets': 0.30; 'subject:please': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'assert': 0.31; 'ctypes': 0.31; 'index,': 0.31; 'skip:! 10': 0.31; 'file': 0.32; 'class': 0.32; 'url:python': 0.33; '(most': 0.33; 'position.': 0.33; 'skip:# 10': 0.33; 'skip:& 30': 0.33; '"the': 0.34; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'problem': 0.35; 'created': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'skip:& 10': 0.38; 'pm,': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; 'structure': 0.39; 'sure': 0.39; 'url:mail': 0.40; 'subject:Can': 0.60; 'advanced': 0.63; 'nobody': 0.68; 'subject:skip:A 10': 0.78; 'to:none': 0.92; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:x-originating-ip:in-reply-to:references :date:message-id:subject:from:cc:content-type:x-gm-message-state; bh=KFs0WowX2fJR/6wnwu+v9062Wkvcm+Wc63TjkAn0i/I=; b=m6GUQ4DJJ0QXTa7yV9ojXg2HXjEOCVinkBlks4qejWrteWy9qkuyll6SFmh+LgR7wh SDEhk41AydXsFOfi/HIzVki3nM3c8Lpm3cdy+ZcHMMxyqiHaRaV6aGF6hqbdzFo6fvTO eclFxwqWAM67CsuDtS1U2yhwaB0YxGSpsO3XhhNZDI6thFIv5izWz8SB/lTH8ceqb1rl 9C9iZaX2RcPHw2/RrxX3CAxRg9I2e3o07C5m2LRL76n6HjJh5fnOBoX896MlKKpjiW2c VHv3IPUEReN8OIiSnBDGZmjcLjaxhWxDKW8444DbHnHBFxks0LhW+1HZTLRnx70KAgXR G0fQ== MIME-Version: 1.0 X-Received: by 10.60.133.4 with SMTP id oy4mr15525367oeb.56.1365424858594; Mon, 08 Apr 2013 05:40:58 -0700 (PDT) X-Originating-IP: [220.245.216.139] In-Reply-To: References: Date: Mon, 8 Apr 2013 22:40:58 +1000 Subject: Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' From: Dylan Evans Cc: python-list Content-Type: multipart/alternative; boundary=047d7b471fe2aa5ce704d9d8bf8b X-Gm-Message-State: ALoCoQnW/Kw+I984Yh4lFMhCibyXNAqGUT+nHEWS4/DDKjD/VLOEACR6qKn0133XO4R9PGJMWZI2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 326 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365424868 news.xs4all.nl 6845 [2001:888:2000:d::a6]:59521 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:43068 --047d7b471fe2aa5ce704d9d8bf8b Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 8, 2013 at 8:07 PM, 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__' > -- > http://mail.python.org/mailman/listinfo/python-list > Not sure about the __trunc__ problem but i can suggest this alternative which is a bit simpler def array2D(x, y, val=None): return [[val for col in xrange(x)] for row in xrange(y)] -- "The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum --047d7b471fe2aa5ce704d9d8bf8b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Mon, Apr 8, 2013 at 8:07 PM, <bhk755@gmail.= com> wrote:
I am trying to create 2D arrays without using advanced fea= tures like numpy, for this I have created 2 separate modules arrays.py and = array2D.py. Here's the code for that:

arrays.py module:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
import ctypes

class Array:

#Creates an array with size elements.
=A0 =A0 def __init__( self, size ):
=A0 =A0 =A0 =A0 assert size > 0, "Array size must be > 0" =A0 =A0 =A0 =A0 self._size =3D size
=A0 =A0 =A0 =A0 print "sixe is %s" %self._size

=A0# Create the array structure using the ctypes module.
=A0 =A0 =A0 =A0 PyArrayType =3D ctypes.c_int * size
=A0 =A0 =A0 =A0 self._elements =3D PyArrayType()
=A0 =A0 =A0 =A0 print "type is e", type(self._elements)
=A0 =A0 =A0 =A0 #self._elements =3D ctypes.c_int * size

=A0 =A0 =A0 =A0 print "Elements are self.element %s" % self._elem= ents
# Initialize each element.
=A0 =A0 =A0 =A0 #for i in range(self._size):
=A0 =A0 =A0 =A0 # =A0 =A0 =A0 self.clear( i )


# Returns the size of the array.
=A0 =A0 def __len__( self ):
=A0 =A0 =A0 =A0 return self._size

# Gets the contents of the index element.
=A0 =A0 def __getitem__( self, index ):
=A0 =A0 =A0 =A0 assert index >=3D 0 and index < len(self), "Arra= y subscript out of range"
=A0 =A0 =A0 =A0 return self._elements[ index ]

# Puts the value in the array element at index position.
=A0 =A0 def __setitem__( self, index, value ):
=A0 =A0 =A0 =A0 assert index >=3D 0 and index < len(self), "Arra= y subscript out of range"
=A0 =A0 =A0 =A0 print "Type is ", type(index)
=A0 =A0 =A0 =A0 self._elements[ index ] =3D value

# Clears the array by setting each element to the given value.
=A0 =A0 def clear( self, value ):
=A0 =A0 =A0 =A0 for i in range( len(self) ) :
=A0 =A0 =A0 =A0 self._elements[i] =3D value

# Printing the arrays:
=A0 =A0 def __str__(self):
=A0 =A0 =A0 =A0 return self._elements



array2D.py module
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


import arrays

class Array2D :
=A0 =A0 # Creates a 2-D array of size numRows x numCols.
=A0 =A0 def __init__( self, numRows, numCols ):
=A0 =A0 # Create a 1-D array to store an array reference for each row.

=A0 =A0 =A0 =A0 self._theRows =3D arrays.Array( numRows )
=A0 =A0 =A0 =A0 # Create the 1-D arrays for each row of the 2-D array.
=A0 =A0 =A0 =A0 print "Num of Cloumns is", numCols

=A0 =A0 =A0 =A0 for i in range( numRows ) :
=A0 =A0 =A0 =A0 =A0 =A0 self._theRows[i] =3D arrays.Array( numCols )

=A0 =A0 # Returns the number of rows in the 2-D array.
=A0 =A0 def numRows( self ):
=A0 =A0 =A0 =A0 return len( self._theRows )

=A0 =A0 # Returns the number of columns in the 2-D array.
=A0 =A0 def numCols( self ):
=A0 =A0 =A0 =A0 return len( self._theRows[0] )

=A0 =A0 # Clears the array by setting every element to the given value.
=A0 =A0 def clear( self, value ):
=A0 =A0 =A0 =A0 for row in range( self.numRows() ):
=A0 =A0 =A0 =A0 =A0 =A0 row.clear( value )

=A0 =A0 # Gets the contents of the element at position [i, j]
=A0 =A0 def __getitem__( self, ndxTuple ):
=A0 =A0 =A0 =A0 assert len(ndxTuple) =3D=3D 2, "Invalid number of arra= y subscripts."
=A0 =A0 =A0 =A0 row =3D ndxTuple[0]
=A0 =A0 =A0 =A0 col =3D ndxTuple[1]
=A0 =A0 =A0 =A0 assert row >=3D 0 and row < self.numRows() \
=A0 =A0 =A0 =A0 =A0 =A0 and col >=3D 0 and col < self.numCols(), \ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "Array subscript out of range." =A0 =A0 =A0 =A0 the1dArray =3D self._theRows[row]
=A0 =A0 =A0 =A0 return the1dArray[col]

=A0 =A0 # Sets the contents of the element at position [i,j] to value.
=A0 =A0 def __setitem__( self, ndxTuple, value ):
=A0 =A0 =A0 =A0 #assert len(ndxTuple) =3D=3D 3, "Invalid number of arr= ay subscripts."
=A0 =A0 =A0 =A0 row =3D ndxTuple[0]
=A0 =A0 =A0 =A0 col =3D ndxTuple[1]
=A0 =A0 =A0 =A0 assert row >=3D 0 and row < self.numRows() \
=A0 =A0 =A0 =A0 =A0 =A0 and col >=3D 0 and col < self.numCols(), \ =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "Array subscript out of range." =A0 =A0 =A0 =A0 the1dArray =3D self._theRows[row]
=A0 =A0 =A0 =A0 the1dArray[col] =3D value


arr =3D 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 =A0<type 'int'>
Traceback (most recent call last):
=A0 File "C:\Python27\Lib\array2D.py", line 53, in <module>=
=A0 =A0 arr =3D Array2D(2,4)
=A0 File "C:\Python27\Lib\array2D.py", line 16, in __init__
=A0 =A0 self._theRows[i] =3D arrays.Array( numCols )
=A0 File "C:\Python27\Lib\arrays.py", line 36, in __setitem__
=A0 =A0 self._elements[ index ] =3D value
AttributeError: Array instance has no attribute '__trunc__'
--
http://mail.python.org/mailman/listinfo/python-list


Not sure about the __trunc__ = problem but i can suggest this alternative which is a bit simpler

def array2D(x, y, val=3DNone):
=A0 =A0 return [[val for col = in xrange(x)] for row in xrange(y)]


--
"The UNIX system has a co= mmand, nice ... in order to be nice to the other users. Nobody ever uses it= ." - Andrew S. Tanenbaum
--047d7b471fe2aa5ce704d9d8bf8b--