Change language

Problems Using C Code in Python | Set 2

The DoubleArrayType class can handle a Python situation that has various forms such as array, numy array, list, tuple. This class defines a single from_param () method. This method takes one parameter and narrows it down to a compatible ctypes object (in ctypes.c_double a pointer to ctypes.c_double ).

In the code below, the parameter type name is extracted and used to send to a more specialized method. For example, if a list is passed, a name like — it is a list, and the from_list () method is from_list () . For lists and tuples, the from_list() method converts to a ctypes array object.

Code # 1:

nums = [ 1 , 2 , 3 ]

a = (ctypes.c_double * len (nums)) ( * nums)

print ( "a:" , a)

 

print ( "a [0]:" , a [ 0 ])

  

print ( "a [1 ]: " , a [ 1 ])

 

print ( "a [2]:" , a [ 2 ])

Output:

 a: "__ main __. C_double_Array_3 object at 0x10069cd40" a [0]: 1.0 a [1]: 2.0 a [2]: 3.0 

The from_array() method retrieves the base memory pointer and converts it into a ctypes pointer object for array objects.

Code # 2:

import array

arr = array.array ( ’ d’ , [ 1 , 2 , 3 ])

print ( "arr:" , arr)

 

ptr_ =   a.buffer_info ()

print ( "ptr:" , ptr)

  

print ( "" , ctypes.cast (ptr, ctypes.POINTER (ctypes.c_double)))

Output:

 arr: array (’d’, [1.0, 2.0, 3.0]) ptr: 4298687200 "__ main__. LP_c_double object at 0x10069cd40" 

The from_ndarray() function shows the comparable conversion code for numeric arrays. By defining the class DoubleArrayType and using it in a signature like avg () , the function can take various input, similar to arrays.

Code # 3:

# libraries

import sample

import array

import numpy

 

print ( " Average of list: " , sample.avg ([ 1 , 2 , 3 ]) )

  

print ( " Average of tuple: " , sample.avg (( 1 , 2 , 3 )))

 

print (Average of array: ", sample.avg (array.array ( ’ d’ , [ 1 , 2 , 3 ])))

 

print (Average of numpy array: ", sample.avg (numpy.array ([ 1.0 , 2.0 , 3.0 ])))

Output:

 Average of list: 2.0 Average of tuple: 2.0 Average of array: 2.0 Average of numpy array: 2.0 

To work with a simple C structure, simply define a class that contains the appropriate fields and types, as shown in the code below. Once defined, you can use the class in type signatures, as well as in code that needs to instantiate and work with structs.

Code # 4:

class Point (ctypes.Structure):

_ fields_ = [( ’x’ , ctypes.c_double),

  ( ’y’ , ctypes.c_double)]

 

point1 = sample.Point ( 1 , 2 )

point2 = sample .Point ( 4 , 5 )

 

print ( "pt1 x:" , point1.x)

 

print ( "pt1 y:" , point1.y)

 

print ( "Distance between pt1 and pt2 : " ,

  sample.distance (point1, point2))

Output:

 pt1 x: 1.0 pt1 y: 2.0 Distance between pt1 and pt2: 4.242640687119285 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically