Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail 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; 'attribute': 0.07; 'column': 0.07; 'tries': 0.07; 'subject:help': 0.08; 'arrays': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:instance': 0.09; 'width': 0.09; 'def': 0.12; 'creates': 0.14; 'array.': 0.16; 'ctypes.': 0.16; 'debugging,': 0.16; 'example).': 0.16; 'integer.': 0.16; 'integers.': 0.16; 'internally': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'truncate': 0.16; 'index': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'basically': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'separate': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'suggested': 0.26; 'pass': 0.26; '(for': 0.26; 'header:X-Complaints-To:1': 0.27; 'array': 0.29; 'subject:please': 0.30; 'code': 0.31; '"",': 0.31; 'ctypes': 0.31; 'file': 0.32; 'class': 0.32; 'lists': 0.32; 'url:python': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'subject:the': 0.34; "i'd": 0.34; 'created': 0.35; 'something': 0.35; 'convert': 0.35; 'building': 0.35; 'there': 0.35; 'adjust': 0.36; 'instances': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'url:library': 0.38; 'to:addr:python- list': 0.38; 'rather': 0.38; 'recent': 0.39; 'aside': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'space': 0.40; 'subject:Can': 0.60; 'helps': 0.61; 'details.': 0.61; 'advanced': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'here': 0.66; 'subject:skip:A 10': 0.78; 'absolutely': 0.87 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Can anyone please help me in resolving the error => AttributeError: Array instance has no attribute '__trunc__' Date: Tue, 09 Apr 2013 09:57:23 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084adfe.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365494162 news.xs4all.nl 6926 [2001:888:2000:d::a6]:45040 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43134 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 "", line 1, in 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).