Let's make a module for Python using SWIG

I tried using SWIG. I remember using it for morphological analysis with Human before, but I haven't investigated it properly, so I made a note for myself.

manual http://www.swig.org/Doc1.3/Python.html#Python_nn4

Follow the README for installation.

$./configure
$make
$su
$make install

After the installation is complete, play around with the sample in the manual.

The contents of the .i file http://blog.livedoor.jp/yoshichi9/archives/21911908.html参考

module module name
%{
include header file name
%}
Function enumeration(Or.h file)
#define(Macro definition etc.)

It seems.

Now, let's create an extension module that returns values that follow a normal distribution.

First, prepare various c source files.

normaldist.h


double normal_distribution(double myu, double sigma, double x);

normaldist.c


#include "normaldist.h"
#include <math.h>

double normal_distribution(double myu, double sigma, double x){

  double d;
  double coefficient, in_exp;

  coefficient = sqrt(2 * M_PI * sigma); //coefficient
  in_exp = exp( -pow( x - myu, 2 ) / (2 * sigma ));  //The one on the shoulder of exp

  d = in_exp / coefficient;

  return d;
}

Then the .i file for swig

normaldist.i


%module normaldist

%{
#define SWIG_FILE_WITH_INIT
#include "normaldist.h"
#include <math.h>
  %}

double normal_distribution(double myu, double sigma, double x);

最後にsetup.py

setup.py


from distutils.core import setup, Extension

module1 = Extension('_normaldist',
                    sources = ['normaldist_wrap.c', 'normaldist.c'])

setup (name = 'normaldist',
       version = '1.0',
       description = 'This is a normaldist package',
       ext_modules = [module1],
       py_modules = ["normaldist"],)

All you have to do is type the commands in order.

$swig -python normaldist.i
$python setup.py build_ext --inplace

There should be _normaldist.so in the same directory.

Recommended Posts

Let's make a module for Python using SWIG
[For play] Let's make Yubaba a LINE Bot (Python)
Let's make a GUI with python.
Let's make a graph with python! !!
Let's make a shiritori game with Python
Make a relation diagram of Python module
Let's create a virtual environment for Python
Make Qt for Python app a desktop app
Let's make a multilingual site using flask-babel
Let's make a web framework with Python! (1)
Let's make a combination calculation in Python
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's make a Backend plugin for Errbot
Let's make a web chat using WebSocket with AWS serverless (Python)!
Create a Python module
[Python] Let's execute the module regularly using schedule
Let's replace UWSC with Python (5) Let's make a Robot
Let's analyze Covid-19 (Corona) data using Python [For beginners]
Try to make a Python module in C language
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
How to make a Python package using VS Code
[Super easy] Let's make a LINE BOT with Python.
Memo for building a machine learning environment using Python
Let's easily make a math gif using Google Colaboratory
Let's make a Discord Bot.
Make a bookmarklet in Python
Make a fortune with Python
Let's make a rock-paper-scissors game
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module
Let's make a websocket client with Python. (Access token authentication)
Let's create a function for parametrized test using frame object
Try a similar search for Image Search using the Python SDK [Search]
Experiment to make a self-catering PDF for Kindle with Python
How to make a Python package (written for an intern)
I tried to make a stopwatch using tkinter in python
Let's make a LINE bot using various services [ngrok edition]
I got "ModuleNotFoundError: No module named'azure'" when running a program using Azure SDK for Python
Let's make a remote rumba [Software]
I made a Line-bot using Python!
Create a python GUI using tkinter
Make a Tweet box for Pepper
Drawing a silverstone curve using python
Let's make a spot sale service 2
Make a face recognizer using TensorFlow
Let's make a spot sale service 1
python / Make a dict from a list.
[Python] Make the function a lambda function
Make a recommender system with python
Use blender as a python module
[TouchDesigner] Tips for for statements using python
Try using the Python Cmd module
Let's make a supercomputer with xCAT
[Python] Reasons for overriding using super ()
Looking for location for mercurial python module
Let's make a spot sale service 3
Let's make a WEB application for phone book with flask Part 1
Build a local development environment for Lambda + Python using Serverless Framework
I tried to make a regular expression of "amount" using Python
Let's make a WEB application for phone book with flask Part 2