Fetching contributors…
Cannot retrieve contributors at this time
166 lines (150 sloc) 4.71 KB

Python extensions tutorial

Day schedule

1215Lunch break165
1600Tea break (10 minutes)165
Number of days2
Total minutes750
Total hours12.5

Course schedule

SlotTime (hours)Area
Day 1 morning165Introduction and basics
Day 1 afternoon255
Day 2 morning165
Day 2 afternoon255Packaging and distribution

Once registered

  • Contact me for system setup if you need help +919880240460
  • Prerequisites should be done before the training

Course outline

Aims of the course

  • Familiarise with Python/C API
  • Innards of the interpreter

Introduction to extensions

  • Why
  • When
  • How

Structure of the course

  • What we aim to do (libcsv)
  • TDD (using py.test)
  • Packaging
  • Live sessions


Using libcsv

  • Quick overview of how to use libcsv to parse a “large” CSV file and print out fields.
  • Compile and run a few programs to display information as well as print data


  • and with the latter for import.
  • Directories and stuff

Structure of a Python module written in C

  • Basics
    • Various structures and what they represent
    • Reference counts
  • Custom types (new classes)
    • Creating a new class
    • Class methods
    • Objects of the class
    • Object methods
    • Exposing elements of the class

Wrapping libcsv

  • Create a file like object with some extra methods
    • Number of lines, column headers etc.
  • Need to redo this based on what’s provided by the libcsv

Wrapping libcsv

  • Remaining parts


  • Distutils
  • Packaging the module

Open session

Work notes

C program using libcsv

Things to include

  • Options to the cvs_init function
    • Intialisation and csv_set_opts
  • Callback based parsing. Pass Python functions in here.
  • Changing options
  • Error handling!


  • Implement writing CSV files

Overall module design.

  • Initialise file like parser object with file name
  • We should be able to do a for i in fp and get back rows.
  • We can change parameters on the fly with a set_something (or descriptors - even better).

Notes on stages


  • Set an exception in a global variable.

Mention -fPIC

Mention csv_fini

Mention problems with the way we’re executing the callback functions

Spend some time on Reference counts.