EuroPython 2013: Add Music to Python

I gave a talk about Libspotify at the EuroPython 2013 conference in Florence, Italy this week.

The talk shows you how to build cool stuff around music in your Python applications.

  • Libspotify to access the Spotify music streaming service: High-quality streaming, playlist management, music metadata
  • Echo Nest for recommendations, musical analysis, mashups
  • Last.fm for scrobbling and building a musical user profile

The video of the talk is available on YouTube.

Slides and sample code are also online.

I am planning to put up an article about the talk later, but you know how it is with finding the time to write articles…

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())
        {
            break;
        }
        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.

. . . → Read More

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.

. . . → Read More

Python Training – Part 5

Part 1 | Part 2 | Part 3 | Part 4

This is part 5 of a Python training that I gave while I was working at SPIELO International. My manager kindly gave me permission to publish the material. The material is Copyright © 2008-2011 SPIELO International, licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

. . . → Read More

Python Training – Part 4

Part 1 | Part 2 | Part 3 || Part 5

This is part 4 of a Python training that I gave while I was working at SPIELO International. My manager kindly gave me permission to publish the material. The material is Copyright © 2008-2011 SPIELO International, licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

. . . → Read More

Python Training – Part 3

Part 1 | Part 2 || Part 4 | Part 5

This is part 3 of a Python training that I gave while I was working at SPIELO International. My manager kindly gave me permission to publish the material. The material is Copyright © 2008-2011 SPIELO International, licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

. . . → Read More

Python Training – Part 2

Part 1 || Part 3 | Part 4 | Part 5

This is part 2 of a Python training that I gave while I was working at SPIELO International. My manager kindly gave me permission to publish the material. The material is Copyright © 2008-2011 SPIELO International, licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

. . . → Read More

Python Training – Part 1

Part 2 | Part 3 | Part 4 | Part 5

This is the course material for a Python training that I gave while I was working at SPIELO International. My manager kindly gave me permission to publish the material. The material is Copyright © 2008-2011 SPIELO International, licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

The target audience for the trainings were software developers, testers, and mathematicians. Each training session took 3 to 4 hours and consisted mostly of interactive programming, roughly (but not slavishly) following the outline on this page. Depending on the prior programming experience of each group, I made small changes to the trainings as I went along.

Each participant received this tutorial printed to a booklet (12 or 16 pages). Even though I would usually deviate a lot from the exact examples in the handouts, having the handouts frees the participants from taking their own notes and allows them to actively participate in the classroom. I also gave out little exercises at the end of each session to be completed by the participants until the following week.

. . . → Read More

EuroPython 2011 Notes

These are some of my notes from EuroPython 2011. I mostly collect projects and tools that are of immediate interest to me in my own work. If you’re interested in the complete list of talks and would like to download the slides or watch the talks on video, you can find those on the EuroPython talks page. Also, Julie Pichon has summaries of some interesting talks on her site.

EuroPython Bag

Python Environment

At our company, we deploy non-interactive Python tools to our internal users. So far, we deploy these tools into the users’ local Python installations. Problems arise when the package versions that our tools require are different from the versions that the user has installed, or when updates to our tools require new dependencies to be installed. To avoid such problems, the “virtualenv” package allows you to keep separate Python environments for different tools. The “pip” package can then be used to automatically install dependencies into these environments. Alex Clemesha has an article up on his blog about the Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip.

Another useful addition to the Python toolbox is the “nose” unit-testing package, which extends the Python “unittest” module. It has advanced support for test fixtures, generated test cases, running test batteries, and more. More at the “nose” project homepage.

. . . → Read More

Continued Fractions for Representing Real Numbers

This is something I learned at EuroPython 2011. I think it came up in a lightning talk by Alex Martelli, but I don’t recall exactly.

Continued fractions are a representation of real numbers that allows for arbitrary-precision arithmetic.

If you’ve worked with floating-point numbers in Python (or most any other programming language, for that matter), you are aware of precision problems like this:

x = 1.0 / 3.0
total = 0.0
for i in range(300):
    total += x
print repr(total)
print total == 100.0

This prints 99.99999999999966, not 100.0. The reason is that the IEEE 754 floating-point representation of 1/3 isn’t exact, and this (initially small) error accumulates 300 times.

. . . → Read More

Ads