Archive | Python RSS feed for this section

Automatically version your Javascript files with Subversion

17 Feb

We’ve already talked about serving static files in a good way. This post can be read as a follow-up to the Particletree article “Automatically Version Your CSS and JavaScript Files“.

A little excerpt of the article (read it!): When serving static files like .js or .css, the idea is having them cached by the client (a.k.a. browser) for a very long time (let’s say 10 years). But if we modify one of this static files, we need a way for the client to get the new version downloaded again. With that in mind we introduce the “version” of the file in the name, either with mod_rewrite (or similar), either in the querystring as a parameter. Changing the version number will get the browser think is a new file, and get it downloaded again.

But as said, read the Particletree article, is a lot better explained there.

Version

Now the thing is how to determine that version number for each static file. Obviously the idea is not having it done by hand, and that’s why in the Particletree article they use the filemtime() function, to get the last modification timestamp for the file.

Although it’s a first option, personally I don’t like it so much. That would imply a disk access each time an uncached static file is requested, and even we can avoid that disk access saving the timestamps in a include file… Why not use the repository version itself?

The staticVersions.py script

And here’s my humbly contribution. Is a Python script that scans the specified folders for static files (js and css), and returns a PHP array with the name of the files and its version in the Subversion repository, that is, when the last change happened.
[...]

Having fun with Python

23 Jul

I was doing some lightweight scripting like this:

pattern = re.compile("searchingFor", re.IGNORECASE)

tables = [];
for line in fp.readlines():
    if pattern.search(line):
        tables += line
return tables

And then I remembered how fun Python could be with things like the list comprehensions:

pattern = re.compile("searchingFor", re.IGNORECASE)
return [ line for line in fp.readlines()
                if pattern.search(line) ]

Nice, huh? If you want to find out more about mapping lists, keep reading here.

Update: Even better! Cleanear, intuitive, two liner… hell yeah!

pattern = re.compile("searchingFor", re.IGNORECASE)
return filter( pattern.search, fp.readlines() )

Reticulator

12 Apr

Rescued from the total darkness of my hard disk, a little experiment in Python I made a couple of years ago…

The Reticulator!

Basically is a web tool to make modular grids in CSS. It has some minor bugs but as said, it was a little experiment, so we’ll have to live with them. The CSS generated is mostly cross-browsing (at least, it was :D) and the grid positions are based on position:relative not position:absolute.

Source code is availablehere (it works as a command line tool also).