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

Building SWIG Python Extensions on GNU/Linux

Originally published: May 27, 2005

Table of Contents

  1. Abstract
  2. Building Manually on the Command Line
  3. Adding SWIG to Autoconf
  4. Adding SWIG to Automake
  5. An Example Project

Abstract

In the article Python Extensions In C++ Using SWIG, I describe how to extend Python with C++ code. That article also contains instructions for building SWIG extension DLLs on Windows. This article contains instructions for using GNU Automake and Autoconf to build SWIG extensions as shared objects on GNU/Linux.

. . . → Read More

Introduction To New-Style Classes In Python

Orignally published: May 8, 2005

Table of Contents

  1. Why New-Style Classes?
  2. Properties
  3. Static Methods
  4. Class Methods
  5. Descriptors
  6. Attribute Slots
  7. The Constructor __new__
  8. Cooperative Super Call
  9. Conclusion
  10. References

Why New-Style Classes?

New-style classes are part of an effort to unify built-in types and user-defined classes in the Python programming language. New-style classes have been around since Python 2.2 (not that new anymore), so it’s definitely time to take advantage of the new possibilities.

. . . → Read More

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

Abstract

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

. . . → Read More

My Buzz