SlideShare a Scribd company logo
1 of 35
Download to read offline
Python Extending/Integrating A Real World Example Tips Summary

  Python where we can,
   C++ where we must




                                                                   Source: http://xkcd.com/353/
Guy K. Kloss — Python where we can,C++ where we must                                              1/28
Python Extending/Integrating A Real World Example Tips Summary




                             Python where we can,
                             C++ where we must∗

                                           Guy K. Kloss


                                 BarCamp Auckland 2007
                                   15 December 2007


                 ∗   Quote: Alex Martelli, Senior Google Developer

Guy K. Kloss — Python where we can,C++ where we must                 2/28
Python Extending/Integrating A Real World Example Tips Summary

  Outline



     1 Python

     2 Extending/Integrating

     3 A Real World Example

     4 Tips




Guy K. Kloss — Python where we can,C++ where we must               3/28
Python Extending/Integrating A Real World Example Tips Summary

  Outline



     1 Python

     2 Extending/Integrating

     3 A Real World Example

     4 Tips




Guy K. Kloss — Python where we can,C++ where we must               4/28
Python Extending/Integrating A Real World Example Tips Summary

  What is Python?



            Object–oriented
            Dynamic
            Easy to learn syntax
            High-level data types
            Scripting language
            Embeddable in C/C++




Guy K. Kloss — Python where we can,C++ where we must               5/28
Python Extending/Integrating A Real World Example Tips Summary

  Python vs. Java (or C++)

            No compilation
            Fast edit–build–debug cycle
            Dynamic Typing
            No need to declare variables for use
            Easy Syntax
            No curly braces, no semicolons, no new . . .
            Embeddable
            Scripting support for your applications
            Interactive
            Create, view, change objects at runtime
            50% less code
            Compact and natural syntax
            300% more productive
            Closer to the way you think
Guy K. Kloss — Python where we can,C++ where we must               6/28
Python Extending/Integrating A Real World Example Tips Summary

  Why Python?

            Mixable
                    Extend Python with components
                    written in C++, Java, C
                    Embed Python into your application
                    and call it from C/C++
                    Platform independent
            Powerful
                    Ease of use of scripting language
                    Built-in object types
                    Extensive libraries
                    Automatic memory management
                    Modules, classes and exceptions


Guy K. Kloss — Python where we can,C++ where we must               7/28
Python Extending/Integrating A Real World Example Tips Summary

  What is Python used for?

            System Utilities
            System admin tools, portable shell scripts
            Internet Scripting
            CGI scripts, parse HTML, process XML, email tools
            User Interfaces (UIs) & rapid prototyping
            Component Glue
            Scripting for apps, COM scripting
            Distributed Programming
            Web Services, COM, CORBA, XML–RPC
            Database Programming
            Scientific Computing
            PyODE, NumPy, SciPy, PyMol, . . .
            Image Processing
            Python Image Library
            OpenGL Programming, Writing Games
            PyOpenGL, Panda3D, PyOgre, Py3d, VisualPython
            Artifical Intelligence
Guy K. Kloss — Python where we can,C++ where we must               8/28
Python Extending/Integrating A Real World Example Tips Summary

  Why Native Code?
  C/C++, Fortran (and Java, C#, ...)




            Performance
            Binding to legacy code
            (Existing) Applications/Libraries . . .
               ... want to be scripted
               ... want to be tested
               ... want to be re-used




Guy K. Kloss — Python where we can,C++ where we must               9/28
Python Extending/Integrating A Real World Example Tips Summary

  Outline



     1 Python

     2 Extending/Integrating

     3 A Real World Example

     4 Tips




Guy K. Kloss — Python where we can,C++ where we must               10/28
Python Extending/Integrating A Real World Example Tips Summary

  What if I could . . .



     Use this code more effectively . . . ?




     [NaSt2D demonstration (native executable)]




Guy K. Kloss — Python where we can,C++ where we must               11/28
Python Extending/Integrating A Real World Example Tips Summary

  Extending/Integration

     The Contestants:
            The “classic way” . . .
            Extending and Embedding the Python Interpreter
            The “new way” . . .
            Python ctypes
            SWIG
            Boost.Python
            Others:
                    SIP
                    Pythonizer
                    SILOON
                    (Pyrex)
            Extensions also for Java, C#, Fortran, . . . available
Guy K. Kloss — Python where we can,C++ where we must                 12/28
Python Extending/Integrating A Real World Example Tips Summary

  Extending/Integration

     The Contestants:
            The “classic way” . . .
            Extending and Embedding the Python Interpreter
            The “new way” . . .
            Python ctypes
            SWIG
            Boost.Python
            Others:
                    SIP
                    Pythonizer
                    SILOON
                    (Pyrex)
            Extensions also for Java, C#, Fortran, . . . available
Guy K. Kloss — Python where we can,C++ where we must                 12/28
Python Extending/Integrating A Real World Example Tips Summary

  Extending/Integration

     The Contestants:
            The “classic way” . . .
            Extending and Embedding the Python Interpreter
            The “new way” . . .
            Python ctypes
            SWIG
            Boost.Python
            Others:
                    SIP
                    Pythonizer
                    SILOON
                    (Pyrex)
            Extensions also for Java, C#, Fortran, . . . available
Guy K. Kloss — Python where we can,C++ where we must                 12/28
Python Extending/Integrating A Real World Example Tips Summary

  Extending/Integration

     The Contestants:
            The “classic way” . . .
            Extending and Embedding the Python Interpreter
            The “new way” . . .
            Python ctypes
            SWIG
            Boost.Python
            Others:
                    SIP
                    Pythonizer
                    SILOON
                    (Pyrex)
            Extensions also for Java, C#, Fortran, . . . available
Guy K. Kloss — Python where we can,C++ where we must                 12/28
Python Extending/Integrating A Real World Example Tips Summary

  Extending/Integration

     The Contestants:
            The “classic way” . . .
            Extending and Embedding the Python Interpreter
            The “new way” . . .
            Python ctypes
            SWIG
            Boost.Python
            Others:
                    SIP
                    Pythonizer
                    SILOON
                    (Pyrex)
            Extensions also for Java, C#, Fortran, . . . available
Guy K. Kloss — Python where we can,C++ where we must                 12/28
Python Extending/Integrating A Real World Example Tips Summary

  Boost.Python


     Thinking Hybrid with Boost.Python
            Bottom up and . . .
            Top down possible
            Develop quickly
            Resolve bottle necks
            Donald Knuth’s:
            “Premature optimisation is the root of all evil.”
            or: Don’t Optimise Now!




Guy K. Kloss — Python where we can,C++ where we must               13/28
Python Extending/Integrating A Real World Example Tips Summary

  Boost.Python


     Thinking Hybrid with Boost.Python
            Bottom up and . . .
            Top down possible
            Develop quickly
            Resolve bottle necks
            Donald Knuth’s:
            “Premature optimisation is the root of all evil.”
            or: Don’t Optimise Now!




Guy K. Kloss — Python where we can,C++ where we must               13/28
Python Extending/Integrating A Real World Example Tips Summary

  Boost.Python


     Thinking Hybrid with Boost.Python
            Bottom up and . . .
            Top down possible
            Develop quickly
            Resolve bottle necks
            Donald Knuth’s:
            “Premature optimisation is the root of all evil.”
            or: Don’t Optimise Now!




Guy K. Kloss — Python where we can,C++ where we must               13/28
Python Extending/Integrating A Real World Example Tips Summary

  Hello World
     char const* greet(unsigned x) {
         static char const* const msgs[] = {quot;helloquot;, quot;Boost.Pythonquot;,
     quot;world!quot;};
         if (x > 2) {
              throw std::range error(quot;greet: Index out of range.quot;);
         }
         return msgs[x];
     }

     #include <boost/python.hpp>
     using namespace boost::python;
     BOOST PYTHON MODULE(hello)
     {
          .def(quot;greetquot;, greet, quot;return one of 3 parts of a greetingquot;);
     }

     And here it is in action:
     >>> import hello
     >>> for x in range(3):
     ...     print hello.greet(x)
     ...
     hello
     Boost.Python
     world!

Guy K. Kloss — Python where we can,C++ where we must                     14/28
Python Extending/Integrating A Real World Example Tips Summary

  Boost.Python




            One of a few libraries that make it easy
            to integrate C++ and Python code
            How does it pull off this trick?
            Template meta–programming
            (i. e. Don’t ask!)




Guy K. Kloss — Python where we can,C++ where we must               15/28
Python Extending/Integrating A Real World Example Tips Summary

  Boost.Python




            One of a few libraries that make it easy
            to integrate C++ and Python code
            How does it pull off this trick?
            Template meta–programming
            (i. e. Don’t ask!)




Guy K. Kloss — Python where we can,C++ where we must               15/28
Python Extending/Integrating A Real World Example Tips Summary

  See it Happen



     I’m making it work for you now . . .




     [“MyClass” demonstration (MyClass.cpp, MyClass.h, mymodule.cpp)]




Guy K. Kloss — Python where we can,C++ where we must                    16/28
Python Extending/Integrating A Real World Example Tips Summary

  Outline



     1 Python

     2 Extending/Integrating

     3 A Real World Example

     4 Tips




Guy K. Kloss — Python where we can,C++ where we must               17/28
Python Extending/Integrating A Real World Example Tips Summary

  A Real World Example
  Re-visiting NaSt2D




     Wrapping NaSt2D
            Control the code




Guy K. Kloss — Python where we can,C++ where we must               18/28
Python Extending/Integrating A Real World Example Tips Summary

  NaSt2D in Python


     This is what I’m going to show you:
            Code to be wrapped
            Generated wrapper code
            Generator script
            SCons (build system)
            How it works with Python
     [Wrapped NaSt2D demonstration (Wrapper.h, nast2dmodule.cpp,
     generate bindings.py, SConstruct, demo0.py)]




Guy K. Kloss — Python where we can,C++ where we must               19/28
Python Extending/Integrating A Real World Example Tips Summary

  Extend Wrapper Class




     Inheriting from C++ classes
            Interfacing numerical values
            Change functionality
            Follow the Computation




Guy K. Kloss — Python where we can,C++ where we must               20/28
Python Extending/Integrating A Real World Example Tips Summary

  Overriding in Python



     This is what I’m going to show you:
            Overriding a native method in Python
            Native method needs to be “virtual”
            Live data plotting with GNUplot
     [NaSt2D with plotting demonstration (demo1.py, demo2.py)]




Guy K. Kloss — Python where we can,C++ where we must               21/28
Python Extending/Integrating A Real World Example Tips Summary

  Do more Computations




     Parameter Study
            Change input file
            Compute several cases
            Plot results automatically




Guy K. Kloss — Python where we can,C++ where we must               22/28
Python Extending/Integrating A Real World Example Tips Summary

  Automating in Python



     This is what I’m going to show you:
            Using a template input file
            Batch–calculating several runs
            Plotting results with GNUplot
     [NaSt2D with parameter variation demonstration (demo3.py, demo4.py)]




Guy K. Kloss — Python where we can,C++ where we must                        23/28
Python Extending/Integrating A Real World Example Tips Summary

  Outline



     1 Python

     2 Extending/Integrating

     3 A Real World Example

     4 Tips




Guy K. Kloss — Python where we can,C++ where we must               24/28
Python Extending/Integrating A Real World Example Tips Summary

  Tips


            To override C++ methods: make them virtual
            C/C++ pit fall
                    Call by reference/value
                    Solution: calling policies
            Map to “other/sane” languages
                    Java: Jython
                    C#: IronPython
                    Fortran: PyFort, Py2F
                    (Native to other: SWIG)




Guy K. Kloss — Python where we can,C++ where we must               25/28
Python Extending/Integrating A Real World Example Tips Summary

  Summary



            Why is Python good for you?
            How can performance bottle necks be resolved?
            Advantages of “Thinking Hybrid”
            Python–native wrapping using Boost.Python
            Automated wrapper generation
            SCons build system




Guy K. Kloss — Python where we can,C++ where we must               26/28
Python Extending/Integrating A Real World Example Tips Summary

  Python and the “Need for Speed”




     Cuong Do – Software Architect YouTube.com
                 “Python is fast enough for our site
           and allows us to produce maintainable features
                           in record times,
                   with a minimum of developers.”




Guy K. Kloss — Python where we can,C++ where we must               27/28
Python Extending/Integrating A Real World Example Tips Summary




     Questions?


                                                            G.Kloss@massey.ac.nz


Guy K. Kloss — Python where we can,C++ where we must                               28/28

More Related Content

What's hot

What is Python? (Silicon Valley CodeCamp 2015)
What is Python? (Silicon Valley CodeCamp 2015)What is Python? (Silicon Valley CodeCamp 2015)
What is Python? (Silicon Valley CodeCamp 2015)wesley chun
 
开源沙龙第一期 Python intro
开源沙龙第一期 Python intro开源沙龙第一期 Python intro
开源沙龙第一期 Python introfantasy zheng
 
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...Edureka!
 
Python intro
Python introPython intro
Python introrik0
 
First python project
First python projectFirst python project
First python projectNeetu Jain
 
Time travel: Let’s learn from the history of Python packaging!
Time travel: Let’s learn from the history of Python packaging!Time travel: Let’s learn from the history of Python packaging!
Time travel: Let’s learn from the history of Python packaging!Kir Chou
 

What's hot (6)

What is Python? (Silicon Valley CodeCamp 2015)
What is Python? (Silicon Valley CodeCamp 2015)What is Python? (Silicon Valley CodeCamp 2015)
What is Python? (Silicon Valley CodeCamp 2015)
 
开源沙龙第一期 Python intro
开源沙龙第一期 Python intro开源沙龙第一期 Python intro
开源沙龙第一期 Python intro
 
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
Python Scripting Tutorial for Beginners | Python Tutorial | Python Training |...
 
Python intro
Python introPython intro
Python intro
 
First python project
First python projectFirst python project
First python project
 
Time travel: Let’s learn from the history of Python packaging!
Time travel: Let’s learn from the history of Python packaging!Time travel: Let’s learn from the history of Python packaging!
Time travel: Let’s learn from the history of Python packaging!
 

Similar to Thinking Hybrid - Python/C++ Integration

Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extensionSqreen
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsHenry Schreiner
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfbhagyashri686896
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfrupaliakhute
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfsannykhopade
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes rajaniraut
 
Pythonic doesn't mean slow!
Pythonic doesn't mean slow!Pythonic doesn't mean slow!
Pythonic doesn't mean slow!Ronan Lamy
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfVisionAcademyProfSac
 
osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)Hattori Hideo
 
Python. Why to learn?
Python. Why to learn?Python. Why to learn?
Python. Why to learn?Oleh Korkh
 
A Whirlwind Tour Of Python
A Whirlwind Tour Of PythonA Whirlwind Tour Of Python
A Whirlwind Tour Of PythonAsia Smith
 

Similar to Thinking Hybrid - Python/C++ Integration (20)

Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
 
Doing the Impossible
Doing the ImpossibleDoing the Impossible
Doing the Impossible
 
PyPy London Demo Evening 2013
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdfPython_final_print_batch_II_vision_academy (1).pdf
Python_final_print_batch_II_vision_academy (1).pdf
 
Python_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdfPython_final_print_batch_II_vision_academy.pdf
Python_final_print_batch_II_vision_academy.pdf
 
Python_vision_academy notes
Python_vision_academy notes Python_vision_academy notes
Python_vision_academy notes
 
Pythonic doesn't mean slow!
Pythonic doesn't mean slow!Pythonic doesn't mean slow!
Pythonic doesn't mean slow!
 
Python_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdfPython_final_print_vison_academy_9822506209.pdf
Python_final_print_vison_academy_9822506209.pdf
 
Python
PythonPython
Python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
what is python ?
what is python ? what is python ?
what is python ?
 
Why learn python in 2017?
Why learn python in 2017?Why learn python in 2017?
Why learn python in 2017?
 
Cmpe202 01 Research
Cmpe202 01 ResearchCmpe202 01 Research
Cmpe202 01 Research
 
osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)osakapy 2014.10 LT (CI for Python Project)
osakapy 2014.10 LT (CI for Python Project)
 
Python. Why to learn?
Python. Why to learn?Python. Why to learn?
Python. Why to learn?
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
A Whirlwind Tour Of Python
A Whirlwind Tour Of PythonA Whirlwind Tour Of Python
A Whirlwind Tour Of Python
 

More from Guy K. Kloss

Kauri ID - A Self-Sovereign, Blockchain-based Identity System
Kauri ID - A Self-Sovereign, Blockchain-based Identity SystemKauri ID - A Self-Sovereign, Blockchain-based Identity System
Kauri ID - A Self-Sovereign, Blockchain-based Identity SystemGuy K. Kloss
 
Qrious about Insights -- Big Data in the Real World
Qrious about Insights -- Big Data in the Real WorldQrious about Insights -- Big Data in the Real World
Qrious about Insights -- Big Data in the Real WorldGuy K. Kloss
 
WTF is Blockchain???
WTF is Blockchain???WTF is Blockchain???
WTF is Blockchain???Guy K. Kloss
 
Building a (Really) Secure Cloud Product
Building a (Really) Secure Cloud ProductBuilding a (Really) Secure Cloud Product
Building a (Really) Secure Cloud ProductGuy K. Kloss
 
Representational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASRepresentational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASGuy K. Kloss
 
Introduction to LaTeX (For Word users)
 Introduction to LaTeX (For Word users) Introduction to LaTeX (For Word users)
Introduction to LaTeX (For Word users)Guy K. Kloss
 
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"MataNui - Building a Grid Data Infrastructure that "doesn't suck!"
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"Guy K. Kloss
 
Operations Research and Optimization in Python using PuLP
Operations Research and Optimization in Python using PuLPOperations Research and Optimization in Python using PuLP
Operations Research and Optimization in Python using PuLPGuy K. Kloss
 
Python Data Plotting and Visualisation Extravaganza
Python Data Plotting and Visualisation ExtravaganzaPython Data Plotting and Visualisation Extravaganza
Python Data Plotting and Visualisation ExtravaganzaGuy K. Kloss
 
Lecture "Open Source and Open Content"
Lecture "Open Source and Open Content"Lecture "Open Source and Open Content"
Lecture "Open Source and Open Content"Guy K. Kloss
 
Version Control with Subversion
Version Control with SubversionVersion Control with Subversion
Version Control with SubversionGuy K. Kloss
 
Beating the (sh** out of the) GIL - Multithreading vs. Multiprocessing
Beating the (sh** out of the) GIL - Multithreading vs. MultiprocessingBeating the (sh** out of the) GIL - Multithreading vs. Multiprocessing
Beating the (sh** out of the) GIL - Multithreading vs. MultiprocessingGuy K. Kloss
 
Gaining Colour Stability in Live Image Capturing
Gaining Colour Stability in Live Image CapturingGaining Colour Stability in Live Image Capturing
Gaining Colour Stability in Live Image CapturingGuy K. Kloss
 
LaTeX Introduction for Word Users
LaTeX Introduction for Word UsersLaTeX Introduction for Word Users
LaTeX Introduction for Word UsersGuy K. Kloss
 

More from Guy K. Kloss (14)

Kauri ID - A Self-Sovereign, Blockchain-based Identity System
Kauri ID - A Self-Sovereign, Blockchain-based Identity SystemKauri ID - A Self-Sovereign, Blockchain-based Identity System
Kauri ID - A Self-Sovereign, Blockchain-based Identity System
 
Qrious about Insights -- Big Data in the Real World
Qrious about Insights -- Big Data in the Real WorldQrious about Insights -- Big Data in the Real World
Qrious about Insights -- Big Data in the Real World
 
WTF is Blockchain???
WTF is Blockchain???WTF is Blockchain???
WTF is Blockchain???
 
Building a (Really) Secure Cloud Product
Building a (Really) Secure Cloud ProductBuilding a (Really) Secure Cloud Product
Building a (Really) Secure Cloud Product
 
Representational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASRepresentational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOAS
 
Introduction to LaTeX (For Word users)
 Introduction to LaTeX (For Word users) Introduction to LaTeX (For Word users)
Introduction to LaTeX (For Word users)
 
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"MataNui - Building a Grid Data Infrastructure that "doesn't suck!"
MataNui - Building a Grid Data Infrastructure that "doesn't suck!"
 
Operations Research and Optimization in Python using PuLP
Operations Research and Optimization in Python using PuLPOperations Research and Optimization in Python using PuLP
Operations Research and Optimization in Python using PuLP
 
Python Data Plotting and Visualisation Extravaganza
Python Data Plotting and Visualisation ExtravaganzaPython Data Plotting and Visualisation Extravaganza
Python Data Plotting and Visualisation Extravaganza
 
Lecture "Open Source and Open Content"
Lecture "Open Source and Open Content"Lecture "Open Source and Open Content"
Lecture "Open Source and Open Content"
 
Version Control with Subversion
Version Control with SubversionVersion Control with Subversion
Version Control with Subversion
 
Beating the (sh** out of the) GIL - Multithreading vs. Multiprocessing
Beating the (sh** out of the) GIL - Multithreading vs. MultiprocessingBeating the (sh** out of the) GIL - Multithreading vs. Multiprocessing
Beating the (sh** out of the) GIL - Multithreading vs. Multiprocessing
 
Gaining Colour Stability in Live Image Capturing
Gaining Colour Stability in Live Image CapturingGaining Colour Stability in Live Image Capturing
Gaining Colour Stability in Live Image Capturing
 
LaTeX Introduction for Word Users
LaTeX Introduction for Word UsersLaTeX Introduction for Word Users
LaTeX Introduction for Word Users
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 

Thinking Hybrid - Python/C++ Integration

  • 1. Python Extending/Integrating A Real World Example Tips Summary Python where we can, C++ where we must Source: http://xkcd.com/353/ Guy K. Kloss — Python where we can,C++ where we must 1/28
  • 2. Python Extending/Integrating A Real World Example Tips Summary Python where we can, C++ where we must∗ Guy K. Kloss BarCamp Auckland 2007 15 December 2007 ∗ Quote: Alex Martelli, Senior Google Developer Guy K. Kloss — Python where we can,C++ where we must 2/28
  • 3. Python Extending/Integrating A Real World Example Tips Summary Outline 1 Python 2 Extending/Integrating 3 A Real World Example 4 Tips Guy K. Kloss — Python where we can,C++ where we must 3/28
  • 4. Python Extending/Integrating A Real World Example Tips Summary Outline 1 Python 2 Extending/Integrating 3 A Real World Example 4 Tips Guy K. Kloss — Python where we can,C++ where we must 4/28
  • 5. Python Extending/Integrating A Real World Example Tips Summary What is Python? Object–oriented Dynamic Easy to learn syntax High-level data types Scripting language Embeddable in C/C++ Guy K. Kloss — Python where we can,C++ where we must 5/28
  • 6. Python Extending/Integrating A Real World Example Tips Summary Python vs. Java (or C++) No compilation Fast edit–build–debug cycle Dynamic Typing No need to declare variables for use Easy Syntax No curly braces, no semicolons, no new . . . Embeddable Scripting support for your applications Interactive Create, view, change objects at runtime 50% less code Compact and natural syntax 300% more productive Closer to the way you think Guy K. Kloss — Python where we can,C++ where we must 6/28
  • 7. Python Extending/Integrating A Real World Example Tips Summary Why Python? Mixable Extend Python with components written in C++, Java, C Embed Python into your application and call it from C/C++ Platform independent Powerful Ease of use of scripting language Built-in object types Extensive libraries Automatic memory management Modules, classes and exceptions Guy K. Kloss — Python where we can,C++ where we must 7/28
  • 8. Python Extending/Integrating A Real World Example Tips Summary What is Python used for? System Utilities System admin tools, portable shell scripts Internet Scripting CGI scripts, parse HTML, process XML, email tools User Interfaces (UIs) & rapid prototyping Component Glue Scripting for apps, COM scripting Distributed Programming Web Services, COM, CORBA, XML–RPC Database Programming Scientific Computing PyODE, NumPy, SciPy, PyMol, . . . Image Processing Python Image Library OpenGL Programming, Writing Games PyOpenGL, Panda3D, PyOgre, Py3d, VisualPython Artifical Intelligence Guy K. Kloss — Python where we can,C++ where we must 8/28
  • 9. Python Extending/Integrating A Real World Example Tips Summary Why Native Code? C/C++, Fortran (and Java, C#, ...) Performance Binding to legacy code (Existing) Applications/Libraries . . . ... want to be scripted ... want to be tested ... want to be re-used Guy K. Kloss — Python where we can,C++ where we must 9/28
  • 10. Python Extending/Integrating A Real World Example Tips Summary Outline 1 Python 2 Extending/Integrating 3 A Real World Example 4 Tips Guy K. Kloss — Python where we can,C++ where we must 10/28
  • 11. Python Extending/Integrating A Real World Example Tips Summary What if I could . . . Use this code more effectively . . . ? [NaSt2D demonstration (native executable)] Guy K. Kloss — Python where we can,C++ where we must 11/28
  • 12. Python Extending/Integrating A Real World Example Tips Summary Extending/Integration The Contestants: The “classic way” . . . Extending and Embedding the Python Interpreter The “new way” . . . Python ctypes SWIG Boost.Python Others: SIP Pythonizer SILOON (Pyrex) Extensions also for Java, C#, Fortran, . . . available Guy K. Kloss — Python where we can,C++ where we must 12/28
  • 13. Python Extending/Integrating A Real World Example Tips Summary Extending/Integration The Contestants: The “classic way” . . . Extending and Embedding the Python Interpreter The “new way” . . . Python ctypes SWIG Boost.Python Others: SIP Pythonizer SILOON (Pyrex) Extensions also for Java, C#, Fortran, . . . available Guy K. Kloss — Python where we can,C++ where we must 12/28
  • 14. Python Extending/Integrating A Real World Example Tips Summary Extending/Integration The Contestants: The “classic way” . . . Extending and Embedding the Python Interpreter The “new way” . . . Python ctypes SWIG Boost.Python Others: SIP Pythonizer SILOON (Pyrex) Extensions also for Java, C#, Fortran, . . . available Guy K. Kloss — Python where we can,C++ where we must 12/28
  • 15. Python Extending/Integrating A Real World Example Tips Summary Extending/Integration The Contestants: The “classic way” . . . Extending and Embedding the Python Interpreter The “new way” . . . Python ctypes SWIG Boost.Python Others: SIP Pythonizer SILOON (Pyrex) Extensions also for Java, C#, Fortran, . . . available Guy K. Kloss — Python where we can,C++ where we must 12/28
  • 16. Python Extending/Integrating A Real World Example Tips Summary Extending/Integration The Contestants: The “classic way” . . . Extending and Embedding the Python Interpreter The “new way” . . . Python ctypes SWIG Boost.Python Others: SIP Pythonizer SILOON (Pyrex) Extensions also for Java, C#, Fortran, . . . available Guy K. Kloss — Python where we can,C++ where we must 12/28
  • 17. Python Extending/Integrating A Real World Example Tips Summary Boost.Python Thinking Hybrid with Boost.Python Bottom up and . . . Top down possible Develop quickly Resolve bottle necks Donald Knuth’s: “Premature optimisation is the root of all evil.” or: Don’t Optimise Now! Guy K. Kloss — Python where we can,C++ where we must 13/28
  • 18. Python Extending/Integrating A Real World Example Tips Summary Boost.Python Thinking Hybrid with Boost.Python Bottom up and . . . Top down possible Develop quickly Resolve bottle necks Donald Knuth’s: “Premature optimisation is the root of all evil.” or: Don’t Optimise Now! Guy K. Kloss — Python where we can,C++ where we must 13/28
  • 19. Python Extending/Integrating A Real World Example Tips Summary Boost.Python Thinking Hybrid with Boost.Python Bottom up and . . . Top down possible Develop quickly Resolve bottle necks Donald Knuth’s: “Premature optimisation is the root of all evil.” or: Don’t Optimise Now! Guy K. Kloss — Python where we can,C++ where we must 13/28
  • 20. Python Extending/Integrating A Real World Example Tips Summary Hello World char const* greet(unsigned x) { static char const* const msgs[] = {quot;helloquot;, quot;Boost.Pythonquot;, quot;world!quot;}; if (x > 2) { throw std::range error(quot;greet: Index out of range.quot;); } return msgs[x]; } #include <boost/python.hpp> using namespace boost::python; BOOST PYTHON MODULE(hello) { .def(quot;greetquot;, greet, quot;return one of 3 parts of a greetingquot;); } And here it is in action: >>> import hello >>> for x in range(3): ... print hello.greet(x) ... hello Boost.Python world! Guy K. Kloss — Python where we can,C++ where we must 14/28
  • 21. Python Extending/Integrating A Real World Example Tips Summary Boost.Python One of a few libraries that make it easy to integrate C++ and Python code How does it pull off this trick? Template meta–programming (i. e. Don’t ask!) Guy K. Kloss — Python where we can,C++ where we must 15/28
  • 22. Python Extending/Integrating A Real World Example Tips Summary Boost.Python One of a few libraries that make it easy to integrate C++ and Python code How does it pull off this trick? Template meta–programming (i. e. Don’t ask!) Guy K. Kloss — Python where we can,C++ where we must 15/28
  • 23. Python Extending/Integrating A Real World Example Tips Summary See it Happen I’m making it work for you now . . . [“MyClass” demonstration (MyClass.cpp, MyClass.h, mymodule.cpp)] Guy K. Kloss — Python where we can,C++ where we must 16/28
  • 24. Python Extending/Integrating A Real World Example Tips Summary Outline 1 Python 2 Extending/Integrating 3 A Real World Example 4 Tips Guy K. Kloss — Python where we can,C++ where we must 17/28
  • 25. Python Extending/Integrating A Real World Example Tips Summary A Real World Example Re-visiting NaSt2D Wrapping NaSt2D Control the code Guy K. Kloss — Python where we can,C++ where we must 18/28
  • 26. Python Extending/Integrating A Real World Example Tips Summary NaSt2D in Python This is what I’m going to show you: Code to be wrapped Generated wrapper code Generator script SCons (build system) How it works with Python [Wrapped NaSt2D demonstration (Wrapper.h, nast2dmodule.cpp, generate bindings.py, SConstruct, demo0.py)] Guy K. Kloss — Python where we can,C++ where we must 19/28
  • 27. Python Extending/Integrating A Real World Example Tips Summary Extend Wrapper Class Inheriting from C++ classes Interfacing numerical values Change functionality Follow the Computation Guy K. Kloss — Python where we can,C++ where we must 20/28
  • 28. Python Extending/Integrating A Real World Example Tips Summary Overriding in Python This is what I’m going to show you: Overriding a native method in Python Native method needs to be “virtual” Live data plotting with GNUplot [NaSt2D with plotting demonstration (demo1.py, demo2.py)] Guy K. Kloss — Python where we can,C++ where we must 21/28
  • 29. Python Extending/Integrating A Real World Example Tips Summary Do more Computations Parameter Study Change input file Compute several cases Plot results automatically Guy K. Kloss — Python where we can,C++ where we must 22/28
  • 30. Python Extending/Integrating A Real World Example Tips Summary Automating in Python This is what I’m going to show you: Using a template input file Batch–calculating several runs Plotting results with GNUplot [NaSt2D with parameter variation demonstration (demo3.py, demo4.py)] Guy K. Kloss — Python where we can,C++ where we must 23/28
  • 31. Python Extending/Integrating A Real World Example Tips Summary Outline 1 Python 2 Extending/Integrating 3 A Real World Example 4 Tips Guy K. Kloss — Python where we can,C++ where we must 24/28
  • 32. Python Extending/Integrating A Real World Example Tips Summary Tips To override C++ methods: make them virtual C/C++ pit fall Call by reference/value Solution: calling policies Map to “other/sane” languages Java: Jython C#: IronPython Fortran: PyFort, Py2F (Native to other: SWIG) Guy K. Kloss — Python where we can,C++ where we must 25/28
  • 33. Python Extending/Integrating A Real World Example Tips Summary Summary Why is Python good for you? How can performance bottle necks be resolved? Advantages of “Thinking Hybrid” Python–native wrapping using Boost.Python Automated wrapper generation SCons build system Guy K. Kloss — Python where we can,C++ where we must 26/28
  • 34. Python Extending/Integrating A Real World Example Tips Summary Python and the “Need for Speed” Cuong Do – Software Architect YouTube.com “Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers.” Guy K. Kloss — Python where we can,C++ where we must 27/28
  • 35. Python Extending/Integrating A Real World Example Tips Summary Questions? G.Kloss@massey.ac.nz Guy K. Kloss — Python where we can,C++ where we must 28/28