1,060 items tagged “python”
The Python programming language.
2003
mod_python introduction
Introducing mod_python by Gregory Trubetskoy. One of my biggest problems with mod_python is that documentation outside of the mod_python manual is pretty hard to come by. This article is more of an executive overview than a tutorial, but anything that adds to the overall body of knowledge out there concerning mod_python has to be a good thing. I’m hoping to write some material on mod_python best practises at some point in the near future, but I have to work out what they are first. Luckily the project has an active and very helpful mailing list.
Dive Into Python reborn
Sweet. Mark Pilgrim is working on Dive Into Python again, funded by a dead tree publisher for publication in 2004 (hopefully). The free version will stay available as well. I’ve always preferred reading paper to reading a screen so I’m definitely down for a copy.
[... 135 words]“sexeger”[::-1]
Via Ned Batchelder, an article on Reversing Regular Expressions from Perl.com. Otherwise known as Sexeger, these offer a performance boost over normal regular expressions for certain tasks. The basic idea is pretty simple: searching backwards through a string using a regular expression can be a messy business, but by reversing both the string and the expression, running it, then reversing the result far better performance can be achieved (reversing a string is a relatively inexpensive operation). The example code is in Perl, but I couldn’t resist trying it in Python. The challenge is to find the last number occurring in a string.
[... 384 words]Google conspiracy theories
Microdoc News have a poorly researched story suggesting that Google have been engineering their search results to favour their own properties:
[... 582 words]Interactive Python
I adore the Python interactive interpreter. I use it for development (it’s amazing how many bugs you can skip by testing your code line by line in the interactive environment), I use it for calculations, but recently I’ve also found myself using it just as a general tool for answering questions.
[... 983 words]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]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]