Simon Willison’s Weblog

Subscribe

1,025 items tagged “python”

The Python programming language.

2003

Python for teaching mathematics

Kirby Urner provides some great examples of how Python can be used as an aid to understanding mathematics on the marketing-python mailing list. I particularly liked this demonstration of Pascal’s triangle using Python generators:

[... 139 words]

Python Client Libraries

Three really useful looking Python modules: ClientForm, ClientTable and ClientCookie. ClientForm looks like it provides similar functionality to the form handling part of the WWW::Mechanize perl module, discussed previously. It essentially provides a very simple interface for loading an HTML page, parsing out the form information then filling in the form and submitting it back to the server. The author recommends it for automated testing (I’ve always had trouble figuring out how to link unit testing in to web applications) but I’m sure it could be useful for screen scraping tools as well. ClientTable is an early beta of a powerful looking table parser, and ClientCookie sits on top of the standard urllib library and transparently persists cookies in between requests.

Installing PySQLite

Techno Weenie has a detailed guide to setting up PySQLite on boxes you don’t have root access to. SQLite looks ideal for small to medium sized applications so I can see this being really useful should I ever write something that uses it.

SQLObject

My new favourite toy is SQLObject, an object-relational mapper which makes heavy use of Python’s special method names to create objects which can be used to transparently access and modify data in a relational database. I tried to write something like this in PHP once before and failed miserably, but SQLObject has such an elegant design that I’m just annoyed I didn’t find out about it sooner. Here’s some example code, adapted from the SQLOBject site:

[... 249 words]

On mod_python

So, I’m getting stuck in to mod_python in a pretty big way at the moment. I’ve never even used mod_perl before, so coming from PHP this is turning out to be a real eye opener.

[... 332 words]

Python never copies implicitly

10 Python pitfalls by Hans Novak (via Simon Brunning) is essential reading for anyone with more than a passing interest in Python. Python never copies implicitly. If only that were true of PHP.

Python script shell integration

In another silly Python/Windows hack, Hans Nowak shows how a simple Python script to move a file up to its parent directory can be added to the contextual menu for all Windows files. I can think of all kinds of useful tricks that can be enabled using this tool: instant uploading of a file to an online Photo Gallery for example.

Code personalities

Danny O’Brien compares Perl with Python. Best observation: Python code just doesn’t have much personality compared to Perl.

Python 2.3

After numerous alphas and betas, Python 2.3 has been released. Python.org has highlights of the release, while A.M. Kuchling’s What’s New in Python 2.3 goes in to a bit more detail. There’s some great new stuff, but the feature that particularly caught my eye is this:

[... 147 words]

Ludicrously simple templates with Python

A long, long time ago I wrote my first ever PHP templating system. It was pretty simple; it consisted of a function that took two arguments: the name of a template file, and an associative array of replacements to make on that file.

[... 251 words]

Python Advocacy from Bruce Eckel

Bruce Eckel is turning in to the world’s number one Python advocate. He explains his views on Python on his Weblog in Python Answers, elaborates further on the Python productivity boost in the fourth part of his Artima.com conversation, and discusses Python (amongst other topics) in an interview on the Borland Developer Network. In the latter, he has this to say about Python in education:

[... 281 words]

Bruce Eckel on Python as a teaching language

From the latest Artima interview:

[... 241 words]

Jython as a learning tool

In Jython Is Just Too Useful, Joey Gibson shows how Jython can be used to quickly demonstrate Java class libraries interactively, including using Python’s dir() builtin to inspect available methods of Java classes. I used Jython last year while learning Swing for a piece of University coursework and found that being able to interactively create and manipulate Swing components (and see them appear on the screen as I typed) sped up the learning process a great deal.

Origin of “list comprehension”

Via Jarno Virtanen, a comp.lang.python post explaining the origin of the term “list comprehension”, Python’s clever alternative syntax for filtering lists (see this chapter of Dive Into Python). The term comes from set theory; it’s nice to know that stuff was worth learning after all ;)

Python generators for database result sets

I’ve read several articles on Python generators now, and I had almost got my head around them, but then I read this: Iterators and Databases by Andy Todd, which demonstrates a simple but intuitive way of using generators to iterate through rows from a database query without having to load all of the rows in to a list in memory first. Brilliant.

[... 165 words]

Learning Python, second edition

Mark Lutz has announced that he is working on a second edition of Learning Python. The first edition of Learning Python is probably the best learn-programming-language-X title I have ever read. It’s nice and short, but covers the whole core Python language comprehensively with excellent examples and a well targetted learning curve. I’ve lent my copy to several people, all of whom found it very helpful and one of whom went on to buy a copy themselves. I’ll probably be buying a copy of the second edition just so I can use it on my constant mission to get more people on my course to give Python a go.

Programming by Contract in Python

Programming by Contract seems to be a sister technique to unit testing—instead of (or as well as) writing a set of tests for a piece of code you write a set of pre- and post-conditions for the data being processed which are then checked whenever the program is run in debugging mode. Contracts for Python discusses the technique, providing a reference implementation and a PEP suggesting inclusion of support for the technique in the core language. Programming by contract was first demonstrated in the Eiffel language, and there’s a good introduction to it in Eiffel.com as well.

PyMeld

PyMeld is a concrete implementation of something I’ve been thinking about for months: A template system that takes an XHTML page as a template and manipulates it based on cloning and modifying elements within the template identified using their ID attribute. It’s a very elegant solution that makes good use of Python’s object overloading support to make manipulating templates as intuitive as possible. Maybe the same thing will be possible in PHP once the new overloading functions become part of the standard package.

[... 180 words]

In praise of functional programming

Via Joe Gregorio, Functional programming in Python Part 1 and Part 2. I’m reading Structure and Interpretation of Computer Programs at the moment (available for free online, but I’ve got a library copy) and it has been getting me seriously interested in the functional programming paradigm. It’s also by far the most enlightening (in terms of “wow, that really makes sense”) computer science text book I’ve ever read. There’s something naturally elegant about the functional style, probably thanks to the encapsulation encouraged by the lack of global variables and the extensive use of recursion in functional code examples. It’s definitely true that exposure to a variety of programming styles encourages you to think about problems in different ways.

Fixed Point Arithmetic in Python

The Python Tutorial now includes a new appendix on the limitations of floating point arithmetic. Via Simon Brunning, who also linked to the lengthier What Every Computer Scientist Should Know About Floating-Point Arithmetic almost exactly a year ago.

Python 2.2 Quick Reference

The Python 2.2 Quick Reference, via Michael Twomey. All the essential bits of Python on a single (if quite long) HTML page. Magic.

Python Roundup

No blogging for a while; I’m getting stuck in to a whole load of University coursework. In the meantime though here are some Python links I’ve been hoarding for a few days on my private wiki:

Easy Python Cryptography

Via Garth Kidd’s Python cryptography roundup, ezPyCrypto is a cryptography API so simple even I can use it. Unfortunately the example code is only available in the download archive (not on the web site) but here’s an overview of how it works:

[... 205 words]

Verbose Regular Expressions

Ned Batchelder describes Verbose Python regular expressions. This is one of the things I’ve known about (as in known that they exist) for ages but have never got around to using. I’ve been working with some pretty heavy regular expressions recently that could really do with the clarity of being defined in verbose format with comments.

[... 96 words]

Views on Python

Two views on Python, from Kevin Altis and NTK respectively:

[... 56 words]

Accessible Python

A post on Bryan Richard’s blog about Safari leads to a query about whether or not Py (a Python print magazine) should move to PDF, which in turn leads to a fascinating discussion about the accessibility of both websites and Python source code itself. Well worth taking the time to read.

Useful python resources

A bunch of useful Python resources:

[... 68 words]

Python and micropayments

Fredrik Lundh has started posting his book The Standard Python Library online, in response to O’Reilly’s decision not to publish a second edition of the book. I’d never read it before, but having sampled the first two chapters I’m hooked. It works a bit like a “cookbook”, with a plethora of code samples explained in detail accompanied by tips and tricks relating to the language. The Lazy Import class, which loads a module only when an attribute of the module is called for the first time, is a classic example:

[... 209 words]

Python power

Sam Ruby’s ultra-simple 3-paned aggregator is a great example of the power of high level scripting languages. Using the wxPython cross-platform GUI toolkit and Mark Pilgrim’s ultra-liberal RSS Parser it provides a full application in a mere 107 lines of (highly readable and maintainable) code.

[... 127 words]

Python for Java programmers

Python and Jelly: Scripting Power for Java and XML incorporates an excellent introduction to Python and Jython for Java programmers, with a whole bunch of comparative code samples and comprehensive coverage of differences between the two languages.