Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43054 > unrolled thread
| Started by | bhk755@gmail.com |
|---|---|
| First post | 2013-04-08 03:07 -0700 |
| Last post | 2013-04-09 01:48 -0700 |
| Articles | 11 — 4 participants |
Back to article view | Back to comp.lang.python
Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 03:07 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Dylan Evans <dylan@dje.me> - 2013-04-08 22:40 +1000
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:15 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:15 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 21:01 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-09 05:14 +0000
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-08 23:21 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Peter Otten <__peter__@web.de> - 2013-04-09 09:57 +0200
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:50 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:50 -0700
Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' bhk755@gmail.com - 2013-04-09 01:48 -0700
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-08 03:07 -0700 |
| Subject | Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <c1cc2d05-e718-4414-a7f3-c1e03621c4f5@googlegroups.com> |
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__'
[toc] | [next] | [standalone]
| From | Dylan Evans <dylan@dje.me> |
|---|---|
| Date | 2013-04-08 22:40 +1000 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <mailman.287.1365424868.3114.python-list@python.org> |
| In reply to | #43054 |
[Multipart message — attachments visible in raw view] — view raw
On Mon, Apr 8, 2013 at 8:07 PM, <bhk755@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__'
> --
> 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
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-08 23:15 -0700 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <edf132c9-b6b2-429c-940f-ca25d824dc59@googlegroups.com> |
| In reply to | #43068 |
Hi Dylan, Thank you for the alternative solution. I will look into that.
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-08 23:15 -0700 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <mailman.315.1365488164.3114.python-list@python.org> |
| In reply to | #43068 |
Hi Dylan, Thank you for the alternative solution. I will look into that.
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-08 21:01 -0700 |
| Message-ID | <2ebc5368-8a73-41ed-b6f4-a2a39aae642f@googlegroups.com> |
| In reply to | #43054 |
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 <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.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-04-09 05:14 +0000 |
| Message-ID | <5163a39e$0$29977$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #43110 |
On Mon, 08 Apr 2013 21:01:04 -0700, bhk755 wrote: [snip over 260 lines of unnecessary quoted text] > Hi Dylan, > > Thank you for the alternative solution. I will look into that. Please trim your replies. There's absolutely no reason to expect people to scroll through almost FOUR PAGES of quoted text just to get to a two line response. Thank you. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-08 23:21 -0700 |
| Message-ID | <a4345c6f-7481-4178-b373-781a21022c99@googlegroups.com> |
| In reply to | #43114 |
Thanks Steven for pointing that out. This is my first topic in Google Groups. So, I did not notice that the previous contents will also be taken in the new post.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-04-09 09:57 +0200 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <mailman.326.1365494162.3114.python-list@python.org> |
| In reply to | #43054 |
bhk755@gmail.com wrote: > I am trying to create 2D arrays without using advanced features like > numpy, I'd say that using ctypes is a bit more "advanced" than *using* numpy because with ctypes it helps to know C. > for this I have created 2 separate modules arrays.py and > array2D.py. Here's the code for that: - You should absolutely go with numpy - If you insist on building your own use lists of lists as suggested - If you need something more space efficient than lists have a look at http://docs.python.org/2/library/array.html rather than ctypes. Now on to some debugging, without looking into the details. > 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 ) Here you create an instance of your array of integers... > # 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 ) ...and here you are stuffing arrays.Array instances into that array of integers. In nuce: >>> import ctypes >>> class C: pass # classic class ... >>> items = (ctypes.c_int * 3)() >>> items[0] = C() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: C instance has no attribute '__trunc__' ctypes unhelpfully tries to truncate the value to convert it into an integer. As an aside here's an example of a successful conversion: >>> class C2: ... def __trunc__(self): return 42 ... >>> items[0] = C2() >>> items[0] 42 There are basically two resolutions: - adjust the type of the outer array or - use a single 1D array internally and calculate the index as row * width + column (for example).
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-09 01:50 -0700 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <716e69a5-4e11-4b15-8e6b-40fd5cd09a0a@googlegroups.com> |
| In reply to | #43134 |
Awesome Peter!!!. Thanks a lot for detailed explanation...
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-09 01:50 -0700 |
| Subject | Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' |
| Message-ID | <mailman.331.1365497445.3114.python-list@python.org> |
| In reply to | #43134 |
Awesome Peter!!!. Thanks a lot for detailed explanation...
[toc] | [prev] | [next] | [standalone]
| From | bhk755@gmail.com |
|---|---|
| Date | 2013-04-09 01:48 -0700 |
| Message-ID | <3db23f44-218d-4a09-b28a-bca304b363c9@googlegroups.com> |
| In reply to | #43054 |
Awesome!!! Thanks a lot Peter.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web