Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43068
| Return-Path | <dylan@dje.me> |
|---|---|
| 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 | <c1cc2d05-e718-4414-a7f3-c1e03621c4f5@googlegroups.com> |
| References | <c1cc2d05-e718-4414-a7f3-c1e03621c4f5@googlegroups.com> |
| 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 <dylan@dje.me> |
| Cc | python-list <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.287.1365424868.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web