Unleash Unleash the the power of C++ power of C++ in in Python - - PowerPoint PPT Presentation

unleash
SMART_READER_LITE
LIVE PREVIEW

Unleash Unleash the the power of C++ power of C++ in in Python - - PowerPoint PPT Presentation

Unleash Unleash the the power of C++ power of C++ in in Python Python A guide through the bindings generation process A guide through the bindings generation process Dr. Cristin Maureira-Fredes Software Engineer @ TQtC EuroPython Basel,


slide-1
SLIDE 1 EuroPython Basel, 2019 | @cmaureir

Unleash Unleash the the power of C++ power of C++ in in Python Python

A guide through the bindings generation process A guide through the bindings generation process

  • Dr. Cristián Maureira-Fredes

Software Engineer @ TQtC

slide-2
SLIDE 2 EuroPython Basel, 2019 | @cmaureir

Source and slides Source and slides

github.com/cmaureir/unleash_cpp

slide-3
SLIDE 3 EuroPython Basel, 2019 | @cmaureir

C++ C++

General purpose Multi paradigm Statically typed Compiled Provides low-level memory manipulation Code readability ???

Python Python

General purpose Multi paradigm Dynamically typed Interpreted Automatic memory management Code readability

slide-4
SLIDE 4 EuroPython Basel, 2019 | @cmaureir

if __name__ == "__main__": print("Hello EuroPython 2019")

slide-5
SLIDE 5 EuroPython Basel, 2019 | @cmaureir

#include <iostream> int main() { std::cout << "Hello EuroPython 2019"; return 0; }

slide-6
SLIDE 6 EuroPython Basel, 2019 | @cmaureir

Compile-time if

(╯°□°)╯︵ ┻━┻

template <bool C, typename TR, typename FR> class if_; template <typename TR, typename FR> struct if_<true, TR, FR>{ typedef TR result;}; template <typename TR, typename FR> struct if_<false, TR, FR> { typedef FR result;}; int main() { typename if_<true, int, void*>::result n(3); typename if_<false, int, void*>::result p(&n); typedef typename if_<(sizeof(void *) > sizeof(uint32_t)), uint64_t, uint32_t>::result i_ptr_t; i_ptr_t c_p = reinterpret_cast<i_ptr_t>(p); } Template Meta Programming (Wikibooks)

slide-7
SLIDE 7 EuroPython Basel, 2019 | @cmaureir

C++11: auto and decltype C++11: auto and decltype

const std::vector v(1); auto a = v[0]; / / a: type int auto c = 0; / / c: type int auto d = c; / / d: type int decltype(c) e; / / e: type int, from c decltype((c)) f = c; / / f: type int&, (c) is an lvalue decltype(0) g; / / g: type int, 0 is an rvalue https:/ /gcc.gnu.org/projects/cxx-status.html#cxx11

slide-8
SLIDE 8 EuroPython Basel, 2019 | @cmaureir

C++11: for loops C++11: for loops

std::vector<int> v {1, 2, 3, 4, 5}; / / Old way for (int i = 0; i < v.size(); i++) x += v[i]; / / or with an iterator... / / getting ints from v for (int &i : v) x += i; / / using type inference for (auto &i : v) x += i; https:/ /gcc.gnu.org/projects/cxx-status.html#cxx11

slide-9
SLIDE 9 EuroPython Basel, 2019 | @cmaureir

C++11: lambda functions C++11: lambda functions

/ / [](int x, int y) -> int { return x + y; } / / [&x](int i) -> int { x += i; } std::vector<int> v{ 1, 2, 3, 4, 5 }; int x = 0; std::for_each(begin(v), end(v), [&x](int i) { x += i; });

https:/ /gcc.gnu.org/projects/cxx-status.html#cxx11

slide-10
SLIDE 10 EuroPython Basel, 2019 | @cmaureir

C++20 and Python: for C++20 and Python: for

/ / ranges-v3 #include <range/v3/all.hpp> using namespace std; namespace v = ranges::view; for (auto i : v::ints(0, 5)) cout << i << endl;

for i in range(0, 5): print(i) https:/ /gcc.gnu.org/projects/cxx-status.html#cxx2a

slide-11
SLIDE 11 EuroPython Basel, 2019 | @cmaureir

C++20 and Python: palindrome C++20 and Python: palindrome

#include <range/v3/all.hpp> namespace r = ranges; namespace v = r::view; bool is_palindrome(std::string_view word) { return r::equal(word, v::reverse(word)); }

def is_palindrome(word): #return word == word[::-1] return word == "".join(reversed(word))

slide-12
SLIDE 12 EuroPython Basel, 2019 | @cmaureir
slide-13
SLIDE 13 EuroPython Basel, 2019 | @cmaureir
slide-14
SLIDE 14 EuroPython Basel, 2019 | @cmaureir

Extending Python with C++ Extending Python with C++

slide-15
SLIDE 15 EuroPython Basel, 2019 | @cmaureir

Looking under Looking under Python Python

/ / Include/object.h typedef struct _object { _PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt; struct _typeobject *ob_type; } PyObject;

slide-16
SLIDE 16 EuroPython Basel, 2019 | @cmaureir
slide-17
SLIDE 17 EuroPython Basel, 2019 | @cmaureir

Creating a module Creating a module

Let's look at the code.

slide-18
SLIDE 18 EuroPython Basel, 2019 | @cmaureir

so... so...

What is What is Qt Qt?

slide-19
SLIDE 19 EuroPython Basel, 2019 | @cmaureir

Qt Qt /kjut/ /kjut/

Cross platform C++ framework, for UI and more. ...but what about Python?

slide-20
SLIDE 20 EuroPython Basel, 2019 | @cmaureir

It's around 2007... It's around 2007...

Which Which options

  • ptions do we have?

do we have? Raw CPython SWIG - Boost::Python - swig.org boost.org

slide-21
SLIDE 21 EuroPython Basel, 2019 | @cmaureir

2008 2008

Qt4 Development (PySide)

2015 2015

Qt5 Port (PySide2)

2018 2018

Released (Qt for Python)

The story of PySide The story of PySide

2016 2016

Back to the Qt Project

slide-22
SLIDE 22 EuroPython Basel, 2019 | @cmaureir

How do we do it? How do we do it?

slide-23
SLIDE 23 EuroPython Basel, 2019 | @cmaureir

Shiboken Shiboken 死某剣 死某剣

doc.qt.io/qtforpython/shiboken2

slide-24
SLIDE 24 EuroPython Basel, 2019 | @cmaureir

Other nice options Other nice options

pybind11 - cffi - cppyy - sip - pybind11.readthedocs.io cffi.readthedocs.io cppyy.readthedocs.io riverbankcomputing.com/software/sip

slide-25
SLIDE 25 EuroPython Basel, 2019 | @cmaureir

Creating a Creating a more useful more useful module module

Let's look at the code.

slide-26
SLIDE 26 EuroPython Basel, 2019 | @cmaureir

Summary Summary

Type C++ Python License Support boost::python Interface C++11+ 2.7, 3.0 BSL-1 Boost SWIG Code gen C++11+ 1.5+ GPL3

  • shiboken

Code gen C++11 (*) 2.7, 3.5+ LGPLv3 Qt sip Code gen C++11 (*) 3.5+ GPLv3 Riverbank pyBind11 Interface C++11 (*) 2.7, 3.x BSD-3

  • cffi

Interface C89, C99 (*) 2.6+, 3.0+ MIT PyPy cppyy Interface C++11+ 2 and 3 UC

slide-27
SLIDE 27 EuroPython Basel, 2019 | @cmaureir

Q&A

Qt for Python @cmaureir pyside.org maureira.xyz

slide-28
SLIDE 28 EuroPython Basel, 2019 | @cmaureir

Support Support your local your local groups groups!