Tag Archives: featured

Embedding Python – Tutorial – Part 1

This is a follow-up to my talk at EuroPython 2012, “Supercharging C++ Code with Embedded Python“. An embedded Python interpreter allows users to extend the functionality of the program by writing Python plug-ins. In this series of tutorials, I will give you step-by-step instructions on how to use the Python/C API to do this.

I assume that you know how to write and compile C/C++ programs. If you have prior experience with writing Python extension modules, it may be helpful, although it’s not required. In my article about extending Python, you can find instructions for setting up your Makefiles/workspaces when working with the Python/C API.

The Example Program

In this part, we’re going to add Python plug-ins to a simple C++ program. The program reads lines of text from STDIN and outputs them unmodified to STDOUT.

#include <iostream>
#include <string>

int main(int argc, char* argv[])
    std::clog << "Type lines of text:" << std::endl;
    std::string input;
    while (true)
        std::getline(std::cin, input);
        if (!std::cin.good())
        std::cout << input << std::endl;
    return 0;

The user should now be able to write a Python plug-in that modifies the string before it is printed. These plug-ins will look something like this:

# elmer_fudd_filter.py
def filterFunc(s):
    return s.replace("r", "w").replace("l", "w")

# shout_filter.py
def filterFunc(s):
    return s.upper()

To make this work, we will link the program to the Python interpreter and use the Python/C API to import the plug-in module and to invoke the “filterFunc()” function inside it.

Continue reading Embedding Python – Tutorial – Part 1

Supercharging C++ Code With Embedded Python – EuroPython 2012 Talk

This is the talk that I gave at EuroPython 2012 in Florence, Italy. It was a 60-minute talk, so it’s light on technical details. I am planning to publish follow-up articles that provide step-by-step instructions along with complete code examples. The first part of the tutorial is available. If you want to know when the next part will become available, subscribe to the RSS or add me on Google+ or on Twitter.

(Watch on YouTube.)

You can download the slides in PDF format and the slide sources in SVG format. (Please note the licensing restrictions.)

BTW, our team is hiring. If you’re interested in extending/embedding Python, or just interested in Python in general, you should definitely get in touch with us. Benefits of the position include an agile development process, a variety of projects to work on…and a beach within walking distance.

About Me / About SPIELO

I work as a software architect in the mathematics department at SPIELO International in Graz, Austria.

SPIELO International designs, manufactures and distributes cabinets, games, central systems and associated software for diverse gaming segments, including distributed government-sponsored markets and commercial casino markets.

Our team is responsible for the mathematical game engine that controls all payout-relevant aspects of the game. Part of the engine is an embedded Python interpreter.

Embedded Python: What is it? When to use it?

This talk is about embedding the Python interpreter in a C/C++ program and using the Python/C API to run Python scripts inside the program.

Embedding in a nutshell: Put Python inside your app to run scripts.

Here are some examples of what you can do with this technique.

Plug-in/extension language. This is the “Microsoft Word macro” use case, in which users can extend the functionality of the program by writing their own scripts. Let’s say a users wants to apply random formatting to each word in the text. A simple macro does the trick without requiring you to add a feature that most users don’t need:

Embedding example: Scripting a word processor.

Continue reading Supercharging C++ Code With Embedded Python – EuroPython 2012 Talk

Live Desktop Streaming via DLNA on GNU/Linux

TWiT and the Ubuntu terminal on a TV set via DLNA

Many modern TVs (and set-top boxes, gaming consoles, etc.) support DLNA streaming. Suppose you have a PC that stores all your music, downloaded podcasts, video podcasts, photos, and so on. You can run some DLNA media server software on your PC and stream your entire media collection to your TV over your home network. No more carrying around USB sticks, it’s all in your home cloud.

On GNU/Linux, I am using MediaTomb as my DLNA server. It’s nothing fancy (it’s a file server, after all), and it just works.

Okay, this takes care of media files stored on your PC. But can we do more? Is it possible to stream a live capture of your desktop to the TV?

Let’s say you’re watching a Flash video in your browser, and there’s no way to download the video file. Or, you’re watching a live event being streamed via Flash or whatever. It would be kinda cool to be able to stream that to your TV via DLNA. And it’s possible—not trivial, mind you, but I’ve seen it working at least once…

Continue reading Live Desktop Streaming via DLNA on GNU/Linux

Multiple Screen Sizes With Processing for Android

Samsung Galaxy Tab (Android Virtual Device)

In my ongoing effort to port a desktop Processing application to Android, I am now trying to add support for multiple screen sizes. The goal is that the same .apk works on, say, a 3.7″ Milestone equally well as on a 7″ Galaxy Tab and on a multitude of other screens. The Android Dev Guide has a comprehensive page that explains the platform features for supporting different screens. I did my own tests and experiments to better understand the concepts. This article explains my findings and hopefully saves you some time and work.

Continue reading Multiple Screen Sizes With Processing for Android

Python Extensions In C++ Using SWIG

Originally published: Oct 8, 2003 (Last update: August 15, 2007)

Table of Contents

  1. Abstract
  2. Introduction
  3. The Python/C API
  4. SWIG Basics
    1. A Quick Example
  5. Building Extensions on GNU/Linux
  6. Building Extensions on Windows
    1. Setting Up The Environment
    2. Enabling Syntax-Highlighting For .i Files
    3. Setting Up The DLL Project
    4. SWIG As A Custom Build Step
    5. Troubleshooting
  7. A Case Study: “pymfg”
    1. Namespaces And Other Reasons Why Your Classes Aren’t Wrapped
    2. Hiding The Unwanted And Faking The Non-Existent
    3. Someone Has Done All The Work – Using STL Classes
    4. The Problem With Pointers To Pointers
  8. Conclusion


This article walks you through the process of writing a Python extension module in C++. To simplify the task, we are going to use SWIG to produce the “glue code” between Python and C++. The article presents the following concepts:

  • Converting between C++ and Python data types
  • Setting up an extension module project that uses SWIG
  • Using SWIG interface definition files
  • Exporting functions and classes
  • Exporting STL containers

Continue reading Python Extensions In C++ Using SWIG