Joe Lanman

Web design and development when not playing computer games

Posts tagged with ‘python’

Uploading images and making thumbnails with web.py and PIL

September 27, 2008

I ran into an issue today while trying to implement image uploads. It’s a very basic implementation: taking files from a POST request, copying them to a local folder, and then making thumbnails of them.

I started with the file upload tutorial in the web.py cookbook.

The problem I was getting was that the Python Image library (PIL) was failing to make the thumbnails, with the error ‘image file is truncated’.

(more…)

1 Comment

Passing variables from a child template to a parent in Mako

September 17, 2008

After looking at Web.py’s own templating system, and considering Cheetah, I’ve ended up going with Mako for now. So far I’ve found it very straightforward, with some powerful-looking features to expand into in the future.

One of the great features is template inheritance – but I hit an issue recently with passing a variable from a child template to a parent. It’s not immediately obvious how to do this, but I found the answer on the Mako Google Groups archive (link here):

Child:


<%inherit file="meta.html"/>

<%def name="vars()">

	<% return (meta.id,) %>

</%def>

Parent:


<% (id,) = self.vars() %>

On a side note, I found out today that two of the Python libraries I’m using, Mako and SQLAlchemy, were coded by the same guy – Michael Bayer. Clever chap – not only are the libraries pretty impressive, but they are both well documented and he regularly contributes to the mailing lists for both projects. And he doesn’t even have a big internet head like some developers – though I tracked his blog down here:

http://techspot.zzzeek.org/

No Comments

Making a copy of a SQLAlchemy object

September 15, 2008

Should that be ‘a’ or ‘an’ SQLAlchemy object?

My current CMS project is based on creating templates in the back-end, and then using these to produce ‘inherited’ objects with the same attributes.

multiplicity

I quickly found that it’s surprisingly tricky to grab a template object using SQLAlchemy, and copy it into a similar, but separate, entity. Using the Python ‘copy’ function confuses SQLAlchemy – when you try to save it, it thinks it is merely a representation of the original (template) object.

Here’s how I create separate ‘clones’ from my template objects, defining a function on the object itself:

(more…)

No Comments

Python-style startswith and endswith string functions in Javascript

July 10, 2008

The Python language has useful string methods to search for prefixes and suffixes:


my_string.startswith('pre')

The same thing can be achieved in Javascript using the rather powerful search and replace methods.

(more…)

1 Comment

WP SlimStat

Asynchronous Google Analytics for WordPress plugin powered by WordPress Expert at minilibra.com.