bananaappletw / python-package-example

Collection of minimal examples of python package

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-package-example Build Status

Collection of minimal examples of python package

Overview

Interface

All the examples provide Person class with instance method hello()

class Person:
def hello(self):
    return 'hello'

from xxx_package import Person
Person().hello()

pure python package

pure python implementation

cython pure cpp package

pure cpp implementation package is compiled into single .so file

cython pure python package

pure python implementation package is compiled into single .so file

swig python package

pure cpp implementation package is compiled into single .so file

Personal opinion

C/C++ Extension

I perfer Cython to other implementations

It's more flexible than swig

swig currently have limited C++11 standard support

For example, smart pointer

Only shared_ptr supported

Besides, there is a slight difference between c++ and python

For example, you want to access property name from Person class

Cython

use C++ getter to access private member and encapsulated as Python property

cdef class Person:
cdef CPPPerson *thisptr
def __cinit__(self):
    self.thisptr = new CPPPerson()
@property
def name(self):
    return self.thisptr.getName().decode()

Swig

interface directly inherit from c++ class, need to do something nasty

Test

For the test, tox and pytest

tox create virtualenv then install the package and test.

Suitable for C/C++ Extension

distutils vs setuptools

Use setuptools instead

About

Collection of minimal examples of python package

License:MIT License


Languages

Language:Python 81.7%Language:C++ 15.7%Language:SWIG 2.6%