msl.examples.loadlib.cpp64 module

Communicates with cpp_lib32 via the Cpp32 class.

Example of a module that can be executed within a 64-bit Python interpreter which can communicate with a 32-bit library, cpp_lib32, that is hosted by a 32-bit Python server, cpp32. A 64-bit process cannot load a 32-bit library and therefore inter-process communication is used to interact with a 32-bit library from a 64-bit process.

Cpp64 is the 64-bit client and Cpp32 is the 32-bit server for inter-process communication.

class msl.examples.loadlib.cpp64.Cpp64

Bases: Client64

Communicates with a 32-bit C++ library, cpp_lib32.

This class demonstrates how to communicate with a 32-bit C++ library if an instance of this class is created within a 64-bit Python interpreter.

add(a, b)

Add two integers.

See the corresponding 32-bit add() method.

Parameters:
  • a (int) – First integer.

  • b (int) – Second integer.

Returns:

The sum of a and b.

Return type:

int

subtract(a, b)

Subtract two floating-point numbers (‘float’ refers to the C++ data type).

See the corresponding 32-bit subtract() method.

Parameters:
  • a (float) – First floating-point number.

  • b (float) – Second floating-point number.

Returns:

The difference between a and b.

Return type:

float

add_or_subtract(a, b, do_addition)

Add or subtract two floating-point numbers (‘double’ refers to the C++ data type).

See the corresponding 32-bit add_or_subtract() method.

Parameters:
  • a (float) – First double-precision number.

  • b (float) – Second double-precision number.

  • do_addition (bool) – Whether to add or subtract the numbers.

Returns:

a+b if do_addition is True else a-b.

Return type:

float

scalar_multiply(a, xin)

Multiply each element in an array by a number.

See the corresponding 32-bit scalar_multiply() method.

Parameters:
Returns:

A new array with each element in xin multiplied by a.

Return type:

list[float]

reverse_string_v1(original)

Reverse a string (version 1).

In this method Python allocates the memory for the reversed string and passes the string to C++.

See the corresponding 32-bit reverse_string_v1() method.

Parameters:

original (str) – The original string.

Returns:

The string reversed.

Return type:

str

reverse_string_v2(original)

Reverse a string (version 2).

In this method C++ allocates the memory for the reversed string and passes the string to Python.

See the corresponding 32-bit reverse_string_v2() method.

Parameters:

original (str) – The original string.

Returns:

The string reversed.

Return type:

str

distance_4_points(points)

Calculates the total distance connecting 4 Point’s.

See the corresponding 32-bit distance_4_points() method.

Parameters:

points (FourPoints) – The points to use to calculate the total distance. Since points is a struct that is a fixed size we can pass the ctypes.Structure object directly from 64-bit Python to the 32-bit Python. The ctypes module on the 32-bit server can load the pickle’d ctypes.Structure.

Returns:

The total distance connecting the 4 points.

Return type:

float

circumference(radius, n)

Estimates the circumference of a circle.

This method calls the distance_n_points function in cpp_lib32.

See the corresponding 32-bit circumference() method.

Parameters:
  • radius (float) – The radius of the circle.

  • n (int) – The number of points to use to estimate the circumference.

Returns:

The estimated circumference of the circle.

Return type:

float