articles filed under python

Mutable default parameter values in Python
posted on 07/22/08 at 10:09 a.m. | |

In Python, everything is an object. Variables are technically names for references to objects. Therefore, when passing an argument to a function, what is in fact being passed is the value of the reference. This leads to some useful techniques.

Extending Python with (almost) anything
posted on 03/24/08 at 2:42 p.m. | |

Among the various methods of extending Python about which I have written, Python 2.5's ctypes module is possibly the easiest.

Threading in Django
posted on 03/07/08 at 10:46 a.m. | |

Profitable use of threading in web development is rare, particularly when contending with Python's global interpreter lock. There are a few notable exceptions to this.

Psyco and Django
posted on 02/15/08 at 4:25 p.m. | |

Psyco is a module that optimizes Python applications on the fly. Numerous resources online describe how to use psyco in a Django-powered application to speed it up.

Per-user caching in Django
posted on 02/12/08 at 3:22 p.m. | |

Django comes with an easy-to-use caching framework. With a few simple decorators, an application's views are cached. Decorators can even be used to control upstream caches, such as those maintained by ISPs. Nevertheless, if a rendered view is customized with information individual to a user, these caching options cease to be useful. Django has several solutions for this scenario.

Partial application and currying
posted on 12/04/07 at 12:33 p.m. | |

Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters. For example, a function, sum, might be used to compose a new function called "plus_one" by currying it with the value of one. The composed function is not evaluated; it is returned as a function object which may then be applied to other parameters.

Multi-threading in Python
posted on 11/30/07 at 10:02 a.m. | |

Programming with threads is one of the more difficult tasks in programming. The Python threading and Queue modules make this significantly easier, but it still takes some deliberation to use threads in an efficient way.

Python's thread and threading libraries use POSIX threads. The threading library is the higher level of the two and is therefore the one to use in your typical programming tasks. The Queue module provides a thread-safe mechanism for communicating between threads, like a combination list and semaphore.

Dynamic URLs in Django
posted on 11/12/07 at 3:40 p.m. | |

I long ago switched my company's web applications from Code Igniter to Django. The main reasons were Django's more powerful database API and Python's maintainability and scalability over PHP. The only feature of Code Igniter that I really miss in Django is the ability to add a page without first registering the url, formulating a regular expression to describe the parameters and variations of the view function, and then creating the view and template.

For a production website, this is a better method than Code Igniter's strategy of http://domain.com/class/method/arg/arg/.../, but CI's is much easier during development. My urls may change half a dozen (or more) times in the course of the project, especially if marketing or advertising is involved. Fortunately, there is an easy fix.

Extending Python with Pyrex
posted on 10/23/07 at 1:20 p.m. | |

In a previous entry, I showed how to extend Python without directly using the Python C using SWIG. SWIG parses an interface file written by the programmer and then outputs a wrapper module. A more elegant, more pythonic solution might be a parser which allows a C module to be written directly in Python. Pyrex lets you write your extension module in a Python-like syntax that compiles to C. The C module can then by compiled using distutils (or gcc, et al) to a binary Python module.

Writing Python modules in C with SWIG
posted on 08/14/07 at 9:53 a.m. | |

The Python C API, while very well put together, is still C and therefore requires a lot of time an effort to understand, let alone use. But every now and then, a programmer comes across something that is either impossible in Python or so prohibitively expensive performance-wise that it is worth dusting off the old C textbook.

Contextual programming
posted on 05/15/07 at 4:25 p.m. | |

When working on a large project, it is usually advisable to break your code up into smaller portions. In many languages, these are called shared libraries: smaller chunks of code that can be reused as needed throughout this and other programs.

Sequential permutations in Python
posted on 05/14/07 at 8:03 a.m. | |

I recently needed the functionality in Python to take a series of lists and find all possible combinations of one item from each list. For example, take the following lists that, together, make up my dinner: