Re: Can anyone please help me in resolving the error =>

Discussion in 'Python' started by Peter Otten, Apr 9, 2013.

  1. Peter Otten

    Peter Otten Guest

    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 = 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).
    Peter Otten, Apr 9, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. senthil
    Replies:
    1
    Views:
    3,246
    Egbert Molenkamp
    Feb 27, 2004
  2. KK
    Replies:
    2
    Views:
    516
    Big Brian
    Oct 14, 2003
  3. michael potter

    resolving a warning error

    michael potter, Oct 28, 2004, in forum: C Programming
    Replies:
    11
    Views:
    944
    Dan Pop
    Oct 29, 2004
  4. Dylan Evans
    Replies:
    0
    Views:
    98
    Dylan Evans
    Apr 8, 2013
  5. Steven D'Aprano
    Replies:
    0
    Views:
    138
    Steven D'Aprano
    Apr 9, 2013
Loading...

Share This Page