User Tools

Site Tools


python

This is an old revision of the document!


Python C/C++ bindng

간단한 cad를 만들어 보는게 목표 - glfw, glew을 binding 하여 써보자. - geode 분석 - cygl 분석 https://github.com/pupil-labs/pyglui

Cython helloworld

Reference: https://media.readthedocs.org/pdf/cython/latest/cython.pdf

setup.py # build script

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = "My hello app",
    ext_modules = cythonize('hello.pyx'),  # accepts a glob pattern
)

hello.pyx # cython script

def say_hello_to(name):
	print("Hello %s!" % name)

hello.pyd 생성하기. mingw32가 설치되고 패스가 걸려 있어야 함.

python setup.py build_ext --inplace --compiler=mingw32

hello.c만 생성하고 싶은 경우

cython -e hello.pyx

를 해서 커맨드라인에서 hello.c를 생성할 수 있다.


질문 - pyx, pyd, pyi 의 역활 구분.


Cython Compiler

win32에서 cythonize할때 사용하는 컴파일러는 mingw32와 ms의 visual c++이 있는데, ms에서 python용으로 vc를 준다.

Microsoft Visual C++ Compiler for Python 2.7


Cython Tutorial

# Cython Tutorial 2015-1-1

install

Dockerfile

FROM debian:sid MAINTAINER Donghee Park dongheepark@gmail.com

RUN apt-get update CMD echo “updated” RUN apt-get install vim python openssh-server -y CMD echo “vim and ssh package installed” RUN echo “Asia/Seoul” > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata RUN echo 'root:docker' | chpasswd RUN /etc/init.d/ssh start RUN apt-get install build-essential python-pip python-dev -y RUN pip install cython

CMD [“/bin/bash”]

#

Makefile

cython helloworld.pyx gcc -g -O2 -fpic `python-config –cflags` -c helloworld.c -o helloworld.o gcc -shared -o helloworld.so helloworld.o `python-config –libs`

#

mycode.c

``` #include <stdio.h>

int myfunc(int a, int b) {

printf("look we are within your c code!! \n");
return a+b;

} ```

mycode.h

``` #ifndef MYCODE_H #define MYCODE_H extern int myfunc (int, int); #endif MYCODE_H ```

mycodecpy.pyx

``` cdef extern from “mycode.h”

cdef int myfunc (int, int)

def callCfunc ():

print myfunc (1,2)

```

cython mycodecpy.pyx gcc -g -O2 -fpic -c mycode.c -o mycode.o gcc -g -O2 -fpic -c mycodecpy.c -o mycodecpy `python-config –cflags` gcc -shared -o mycodecpy.so mycode.o mycodecpy.o `python-config –libs`


xdress 사용하기

c++ class 'A' python으로 binding 하기

jehsrc/hoover.h ``` #if !defined(HOOVER) #define HOOVER namespace hoover { class A { public:

 float a;
 double power(int n=1);

};}; #endif ```


jehsrc/hoover.cpp ``` #include “hoover.h” double hoover::A::power(int n){

int i = 1;
double val = a;
while (i < n){val *= a;};
  return val;

}; ```


jeh_xdressrc.py ``` package = 'jedgar' # top-level python package name packagedir = 'jedgar' # location of the python package builddir = 'jehbuild'

classes = [('A', 'jehsrc/hoover.*')] ```

xdress –rc jeh_xdressrc.py python3 jeh_setup.py build > /dev/null 2>&1


Hoover Example

``` from jedgar.hoover import A

a = A() a.a = 10 a.power(42) ```

python.1420119995.txt.gz · Last modified: 2018/07/18 14:09 (external edit)