[PYTHON] Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-

Module written in C / C ++ language double someFunc(double x); Let's say there is.

In this article I'll show you a convenient way to use this on a Python interpreter.


** Why decide to run on the Python interpreter? ** **

・ Python is easier to do trial and error. -In the framework on Python, trial and error can be performed without affecting the existing C / C ++ implementation. ・ By trial and error, it is possible to determine what the appropriate operation is.

** And why are you trying to use the IPython console in the Spyder integrated environment? ** **

・ A calculator is already available. -The history function is already prepared.

In [1]: a=1+2

In [2]: b= a +2

In [3]: history a=1+2 b= a +2 history

In [4]:

-In addition, there is a function to save the history to a file in the standard library. readline.write_history_file("history.py")

Use this If you run the completed history.py file It becomes easy to reproduce the result of execution once.

above double someFunc(double x); If you make a wrapper for Here, if wrapper is also named someFunc (x),

> y1 = someFunc(100.0) > y2 = someFunc(100.0+10.0) It becomes possible to execute such as.

> help(someFunc) By You can see help for the wrapper part of python.

If the feature you are trying to achieve is essentially interactive Rather than building an upper layer in the C / C ++ language By substituting the upper layer with the IPython interpreter The scope of development could be significantly reduced.

How easy it is to create the details of the function to be created by trial and error It works for the speed of development.

** How to easily make your own C / C ++ language library available from Python **

SomeFunc.exe with its features in the C / C ++ language as a single program Let's create it like this. Decide how to input / output to that command os.system ("someFunc.exe argument> result file") To be able to execute like. And the return value of the function should be written to the result file. The result is read from a file This is probably the easiest way to start writing [Note 1].

Then, let's write a python function that uses the self-made command.

.py:someFunc.py


def someFunc(x):
Execute the self-made command here
Let's write the code to assign the return value to r.
    return r

Even with such a writing style, you should be able to achieve the desired work.

** Other benefits **

--Easy to automate the input to the function. --For p in glob.glob ("* .png "): or for root, dirs, files in os.walk ('.'): It is easy to process multiple files.   --Easy to process the output of that function. --It makes it easier to graph the results using matplotlib. --For errors that occur in a higher hierarchy than the function implemented in C ++ (the part implemented in python), the call tree is displayed by python itself, so it is very easy to understand where the error occurred. .. --Since you can easily use data structures such as dictionaries and lists, you can use the part equivalent to STL (map, vector) in C ++ more easily. --You can easily save and read complex data structures by using pickle library, so you can save and read data. There is almost no need to create your own library for this. -[Spyder Integrated Environment](https://ja.wikipedia.org/wiki/Spyder_(%E3%82%BD%E3%83%95%E3%83%88%E3%82%A6%E3%82%] With A7% E3% 82% A2)), you can easily see the data in the memory.

When an error occurs while processing on the IPython interpreter, the memory at that time is saved on the IPython interpreter. It's easy to refer to a variable as is or resume another process. When you write a program in the compiler language, in most cases, when an error occurs, the program ends abnormally and there is no way to restart it. Using the Python interpreter as a foundation is a very easy way to achieve a user-friendly environment.

Mastering the rich features of IPython

[Incorporating Python as a scripting environment](https://ja.wikipedia.org/wiki/Python%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6% E3% 81% 84% E3% 82% 8B% E8% A3% BD% E5% 93% 81% E3% 81% 82% E3% 82% 8B% E3% 81% 84% E3% 81% AF% E3% 82% BD% E3% 83% 95% E3% 83% 88% E3% 82% A6% E3% 82% A7% E3% 82% A2% E3% 81% AE% E4% B8% 80% E8% A6% A7)

Note 1: os.system() It's better to use os.popen (), subprocess than to use Extending Python with C and C ++ It would be better to use.

Recommended Posts

Use a scripting language for a comfortable C ++ life 4-Use your own C ++ library from a scripting language-
Use a scripting language for a comfortable C ++ life
Let's use a scripting language for a comfortable C ++ life 2 Automatically generate C ++ source
Use a scripting language for a comfortable C ++ life 3-Leave graphing to matplotlib-
Let's use a scripting language for a comfortable C ++ life 6-How about developing a program as a library for Python?
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Let's use a scripting language for a comfortable C ++ life C ++ implementation after verification with python
Use a scripting language for a comfortable C ++ life 5 --Use the Spyder integrated environment to check numerical data-
Call your own C language shared library from Python using ctypes
Linux C / C ++ Build your own library creation environment
Created a header-only library management tool for C / C ++
Wrap C with Cython for use from Python
Wrap C ++ with Cython for use from Python
python: Use your own class for numpy ndarray
Call your own C library with Go using cgo
Let's call your own C ++ library with Python (Preferences)
Use IvyFEM (Finite Element Method Library for .NET) from Python
A note for embedding the scripting language in a bash script
Note 2 for embedding the scripting language in a bash script
Let's make a number guessing game in your own language!
Scripting Language C ——How a text file without a shebang is executed
Cross-compile for Raspberry Pi Zero on Debian-Create your own shared library