Loading
0 comments

A few changes

I’ve just finished making a few changes to the code that runs this blog, and I’m rather happy with the results.

First, I took my own advice in the previous post and switched my custom pagination code with the PagedQuery class. It is beautiful code, and it does just what I want. I integrated it quickly and easily into my existing models, and I even created a custom Django tag to create the pagination control that appears at the bottom of the page.

Soon, I will make all of that work available on Google Code as a project called Commentable, a mixin class that allows any entity to have Comments associated with it.

Second, I integrated another great open source project from Google called Prettify. It is a JavaScript and CSS package that displays code blocks elegantly and with syntax highlighting. It is sensationally easy to integrate, and you’ll notice that difference if you look at some posts that contain code samples. You can find the project at: http://code.google.com/p/google-code-prettify/

Finally, I switched from the Dijit rich text editor to TinyMCE. I have worked with TinyMCE before; it is used in MyKidsLibrary. In ...

read more
0 comments

Do not Reinvent the Pagination Wheel
From the very day that Google released the first version of the AppEngine SDK until today, developers have been asking themselves and each other, “How can I handle paginating results from the datastore?” There were dozens of different naive attempts that failed because of the unique nature of the datastore. There were many approaches the solved only half the problem by offering forward-paging only. Still others offered good pagination while limiting the kinds of queries that could be performed.

It was an ugly and uncomfortable mess. It made everyone somewhat uneasy.

Google partially solved the problem with version 1.3.1 of the SDK which introduced query cursors, a simple, transparent and HTTP-friendly way to serialize and deserialize query states. They only provided a single-direction of query resumption, but it was a huge advance over the prior capabilities. A complete paging solution still required quite a bit of work.

I began doing that work in order to provide a new project of mine with web-standard paging. I wanted Previous and Next buttons as well as links to each page of results. Using cursors greatly simplified the code, but I was still writing a lot of code, and time that would ...
read more
3 comments

A Better Sharded Counter
My current AppEngine project was crying out for some counters to track site-wide instances of various models, and I recalled watching Brett Slatkin’s video about building highly-scalable web apps. A few seconds of quality Google time later, and I had the ShardCounter classes ready to go. The same code is available in several locations:
http://code.google.com/p/google-app-engine-samples/source/browse/trunk/sharded-counters/generalcounter.py

One thing that quickly caught my attention is that these classes only support incrementing, and while that makes sense for something like a primitive visit counter, it didn’t handle my needs very well at all. My initial attempt to simply copy the increment function and change the critical += to a -= was naive and doomed to failure, but a little tinkering with the way that counts are recorded gave me a nice working solution that completely preserves the desirable performance characteristics of this approach.

Here’s the code that I came up with. Please feel free to use it in your own projects.
from google.appengine.api import memcache
from google.appengine.ext import db
import random

# This code unabashedly stolen from Google
# http://code ...
read more