Working around Django's ORM to do interesting things with GFKs May 03, 2012 00:05 / 0 comments

In this post I want to discuss how to work around some of the shortcomings of djangos ORM when dealing with Generic Foreign Keys (GFKs).

At the end of the post I'll show how to work around django's lack of correctly CAST-ing when the generic foreign key is of a different column type than the objects it may point to.


Micawber, a python library for extracting rich content from URLs April 19, 2012 11:13 / 0 comments

A while ago I wrote about an awesome API for retrieving metadata about URLs called oembed. I'm writing to announce a new project I've been working on called micawber, which is very similar but with a cleaner API and not restricted to django projects.


Using Redis Pub/Sub and IRC for Error Logging with Python April 15, 2012 11:23 / 0 comments

I recently rewrote my personal site using flask and peewee, breaking a good amount of stuff in the process. I was trying to track down the errors by tailing log files, but that didn't help alert me to new errors that someone visiting the site might stir up. I thought about setting up error emails a-la django, which is a tried and true method...but then I happened on a different approach. I won't say it's the most elegant solution, but it was a quick hack and the results have been awesome. I wrote a custom logging handler that pushes JSON-encoded log record data to a redis pub/sub channel. I then have an IRC bot that subscribes to this channel and when it receives a message generates a paste of the traceback and pings me with a link to the traceback.


Nautilus script to push files to S3 in python April 09, 2012 16:58 / 0 comments

Sometimes I want to push a file on my harddrive to S3 for safe keeping. I wrote a little script for nautilus which appears in the context menu to push files to a specific S3 bucket.


Building a bookmarking service with python and phantomjs March 29, 2012 19:16 / 1 comments

Using python and phantomjs, a headless webkit browser, it is a snap to build a self-hosted bookmarking service that can capture images of entire pages. Combine this with a simple javascript bookmarklet and you end up with a really convenient way of storing bookmarks. The purpose of this post will be to walk through the steps to getting a simple bookmarking service up and running.

http://media.charlesleifer.com/images/photos/import_playground-182916.png


Model code generation with peewee March 20, 2012 15:03 / 0 comments

For fun I put together a small script that is capable of introspecting databases and generating peewee models. I borrowed the crucial bits from django's codebase, which has methods for introspecting column types and foreign key constraints.

The code is hopefully rather straightforward - it simply grabs the list of tables, column type information which is then mapped to peewee field types, then finally resolves foreign keys. The generated models are then dumped to standard out, along with a database declaration.


So long, djangosnippets, and thanks for all the fish March 06, 2012 09:38 / 0 comments

After two years of maintaining djangosnippets.org, I am pleased to announce that the guys from django-de are going to be taking over and you can expect to see some real improvements.


Huey, a lightweight task queue for python February 02, 2012 15:22 / 0 comments

At my job we've been doing a quarterly hackday for almost a year now. My coworkers have made some amazing stuff, and its nice to have an entire day dedicated to hacking on ... well, whatever you want. Tomorrow marks the 4th hackday and I need to scrounge up a good project, but in the meantime I thought I'd write a post about what I did last time around -- a lightweight python task queue that has an API similar to celery.

I've called it huey (which also turns out to be the name of my kitten).

Design goals

The goal of the project was to keep it simple while not skimping on features. At the moment the project does the following:

Backend storages implement a simple API, currently the only implementation uses Redis but adding one that uses the database would be a snap.

The other main goal of the project was to have it work easily for any python application (I've been into using flask lately), but come with baked-in support for django. Because of django's centralized configuration and conventions for loading modules, the django API is simpler than the python one, but hopefully both are reasonably straightforward.


Building a markov-chain IRC bot with python and Redis January 24, 2012 22:59 / 0 comments

As an IRC bot enthusiast and tinkerer, I would like to describe the most enduring and popular bot I've written, a markov-chain bot. Markov chains can be used to generate realistic text, and so are great fodder for IRC bots. The bot I am writing of has been hanging out in my town's channel for the past year or so and has amassed a pretty awesome corpus from which it generates messages. Here are few of his greatest hits:


Updates to peewee, including atomic updates, select related and basic transactions December 29, 2011 13:20 / 1 comments

Over the last two months I've spent a lot of time working on improvements to peewee, a lightweight ORM written in python.

Some of these features are present in Django and were added for better parity, some I found a need for while working on other projects, and others were requested by opening an issue on GitHub or bringing it up on IRC (#peewee on freenode). If you're interested in trying peewee out, it ships with an example app which is described here.

Here's a rundown on what has been added recently:

  • atomic updates
  • select related models
  • basic transaction support

Don't sweat the small stuff - use flask blueprints October 30, 2011 15:29 / 2 comments

For a change, I've been doing all of my new app development using flask, a python web framework built atop the werkzeug WSGI toolkit. Having used django for the last two years it's been fun to do something different, but at the same time stick with python.

In this post I'd like to show a couple of the small projects I've written using flask over the past few weeks.


Redesign of flask-peewee admin October 28, 2011 15:44 / 0 comments

Recently I stumbled across the twitter bootstrap project, which is a set of cross-browser compliant stylesheets and scripts. I liked them so much that I've ported the admin templates to use bootstrap. Here's a little screenshot of the design refresh taken from the example app:

http://media.charlesleifer.com/images/photos/flask-peewee-admin.jpg

I hope this will make the admin easier to work with in the long-run!


Integrating the flask microframework with the peewee ORM September 27, 2011 10:52 / 5 comments

I'd like to write a post about a project I've been working on for the past month or so. I've had a great time working on it and am excited to start putting it to use. The project is called flask-peewee -- it is a set of utilities that bridges the python microframework flask and the lightweight ORM peewee. It is packaged as a flask extension and comes with the following batteries included:


peewee now supports postgresql (and mysql and sqlite) July 24, 2011 10:07 / 2 comments

Over the past month I've been working on adding support for both MySQL and PostgreSQL to peewee. I'm happy to say that after a couple weekend hack sessions all tests are now passing.


Quick update on django-relationships July 07, 2011 11:29 / 0 comments

With the 0.3.0 release of django-relationships, I've made a couple backwards-incompatible changes which I thought I'd mention.


Suggesting tags with django-taggit and jQuery UI June 29, 2011 12:20 / 2 comments

One of the problems mentioned by a couple people when I asked for suggestions on improving djangosnippets.org was the proliferation of tags. This is a well-known problem on sites that allow users to enter their own tags, where misspellings are frequent and its sometimes unclear whether a tag should be plural or singular.

To try and reduce the amount of different tags on djangosnippets I ended up using the jQuery UI autocomplete tools to provide users with hints when they enter tags for their snippets.


Django snippets site improvements, what would you like to see? June 27, 2011 13:04 / 12 comments

Describing some of the improvements made to the django snippets site over the past couple weeks and asking for user feedback on additional improvements they'd like to see.


Solr on Ubuntu, revisited June 17, 2011 17:11 / 1 comments

It's been a while since I first wrote about setting up Solr on Ubuntu. Since then I've opted for a different approach that is both simpler and lighter-weight. This post describes briefly the steps to setting up Solr on Ubuntu.


Updates to peewee, now supports MySQL June 08, 2011 15:56 / 0 comments

I'm pleased to announce that I've added support for MySQL to peewee. All tests are now passing. In the process I uncovered a few small bugs which have also been fixed.

I also added some new reference documentation which describes succinctly how to do basic configuration and querying with peewee.


Munin-like dashboard for Django websites June 04, 2011 01:37 / 0 comments


Updates to the django-utils task queue June 01, 2011 11:40 / 0 comments

After several months of running the task queue bundled with django-utils, I decided to re-evaluate certain aspects of the design. This post describes those changes.


A small update on django-completion May 01, 2011 01:10 / 1 comments

Just a quick heads-up to anyone out there using django-completion, I've released a couple important updates this weekend and you may be interested in updating your checkouts. These changes are purely additive, so don't worry about having to update your own code.

There are three important updates:

  1. Bug: the (experimental) Redis backend could fail to return results under certain conditions and, if data were removed frequently, leak memory all over the place
  2. Bug: partial titles generated for the Redis and Database backends were too restrictive, i.e. "testing python code" would not complete on "python" unless the AUTOCOMPLETE_MAX_WORDS setting was 2 (or 1)
  3. Feature: it is now possible to restrict suggestions to certain models by passing in a list of acceptable models to the suggest() method

A simple botnet written in Python April 20, 2011 16:54 / 2 comments

As of this week we instituted a regular "hackday" at my office -- anything goes, you can work on whatever you like, so at 11:30 the night before the hackday started I decided on writing a simple IRC-powered botnet.


Writing a spider with a django-powered web interface April 06, 2011 11:15 / 2 comments

I'd been scrounging around for a smallish project, when I happened on the idea of writing a spider with a simple web interface. I had recently released a task queue, so I wanted to incorporate that to do the actual crawling, while a django view served up the results as they arrived in the database. The end result is a new project I'm calling django-spider, you can check it out on GitHub. This post will discuss some of the aspects of the design.


A lightweight task queue for Django March 25, 2011 15:07 / 0 comments

It's quite common when building out a website to trigger actions during the normal request/response cycle that may be time-consuming. Examples of these actions might be:

  • sending email, for example a contact form
  • checking a comment for spam by sending to Akismet
  • thumbnailing an image

I remember last year about this time my coworkers and I got pretty excited about Celery, a distributed task queue, that provided a really nice API for executing tasks out-of-process. Basically just decorate functions with the @task decorator and so long as everything is configured properly, they will execute out of process. Celery is an actively-developed project with great documentation and an incredibly rich feature-set, but all those features come with the added cost of lots of configuration and the need for integration with a number of projects (celery, django-celery, kombu, django-kombu, pyparsing, mailer).

I needed a lightweight task queue for some side-projects and rather than trying to integrate all the various celery dependencies (and pinning all the correct versions) I did what anyone would do: rolled my own.


Connecting anything to anything with Django February 17, 2011 19:18 / 0 comments

I'm writing this post to introduce a new project I've released, django-generic-m2m, which as its name would indicate is a generic ManyToMany implementation for django models. The goal of this project was to provide a uniform API for both creating and querying generically-related content in a flexible manner. One use-case for this project would be creating semantic "tags" between diverse objects in the database.


Autocompletion for Django models using Solr, Redis or SQL December 30, 2010 18:08 / 2 comments

One of the nicest UI's around when dealing with a large dataset is a good autocomplete. Facebook's search is a great example, same for Netflix, and recently Google launched "Google Instant", which returns search results as you type. Autocomplete can really complement hierarchical drill-down search (which is useful for discovery), as the goal of autocomplete is more for helping users find something they already know about with a minimum of effort.


Peewee, a lightweight Python ORM November 28, 2010 15:01 / 15 comments

For the past month or so I've been working on writing my own ORM in Python. The project grew out of a need for a lightweight persistence layer for use in Flask web apps. As I've grown so familiar with the Django ORM over the past year, many of the ideas in Peewee are analagous to the concepts in Django. My goal from the beginning has been to keep the implementation simple without sacrificing functionality, and to ultimately create something hackable that others might be able to read and contribute to.


OEmbed your site's Static Media November 08, 2010 23:54 / 0 comments

You may not know it, but djangoembed can be used to OEmbed your own site's static media. We use it at work to allow users to embed photos they upload through the site.


Even more Canvas fun - Tetris in JavaScript November 05, 2010 14:56 / 1 comments

Tetris in JavaScript using the Canvas element, 'nuff said!


More fun with Canvas - a JavaScript Starfield! November 04, 2010 17:44 / 7 comments

The canvas element is awesome. JavaScript is fast enough that you can run some pretty computationally intensive stuff (I've seen 3D games, a NES emulator, and much more all done with JS!). This script shouldn't push your CPU to the limit, but it does show how easy it is to create cool effects with just a small amount of code.


Nokia Snake with JavaScript + Canvas November 03, 2010 23:35 / 4 comments

Keeping with the theme of yesterday's post - "a stroll down memory lane" - I thought I'd re-create the Nokia Snake game (a distant relative of Nibbles) using JavaScript and the canvas element.


A Stroll Down Memory Lane: Scripting AOL November 02, 2010 23:53 / 14 comments

When I started working at my current job I was surprised to see that everyone used IRC as their primary means of communication - much more so than email or IM. I recently wrote a small irc bot library in python - it was a ton of fun and reminded me of some of the first programs I wrote that were bots and scripts for America Online.


Search on djangosnippets.org November 01, 2010 21:07 / 0 comments

Users of djangosnippets.org may have noticed the addition of a few search-related features over the past several months. I'd like to highlight some of the additions that have been made and show how you can implement similar functionality on your sites. All of djangosnippet's search leans on Apache Solr, a powerful search engine built on top of Apache Lucene (full-text search). Haystack is the search solution for Django apps - it provides a querying interface similar to Django's ORM, handles indexing your models for you, and supports advanced features like "more-like-this" and faceting.


Django Patterns: Model Inheritance October 09, 2010 14:41 / 5 comments

This post discusses the two flavors of model inheritance supported by Django, some of their use-cases as well as some potential gotchas.


Writing a real-time chat app using Hookbox and Flask October 02, 2010 17:07 / 0 comments

In this post I'll show how I used Hookbox, a comet server/message queue, and Flask, a lightweight python web framework, to create a simple real-time chat app. This post will walk through creating a bare-bones example, then discuss ways to add additional functionality.


Django Patterns: View Decorators September 23, 2010 01:25 / 5 comments

Sites often have many views that operate with a similar set of assumptions. Maybe there are entire areas that the user must be logged-in to visit, or there is some repetitive boilerplate functionality that a group of views shares like being rate-limited. This post looks at ways to make this kind of functionality less repetitive by using a common Django pattern, view decorators.


Django Patterns: Pluggable Backends September 15, 2010 11:20 / 5 comments

As the first installment in a series on common patterns in Django development, I'd like to discuss the Pluggable Backend pattern. The pattern addresses the common problem of providing extensible support for multiple implementations of a lower level function, be it caching, database querying, etc.


Using markItUp to implement a simple browser-based reST editor August 23, 2010 23:53 / 0 comments

All about the lightweight jQuery-powered markup editing toolkit markItUp, which I used recently to implement basic reStructuredText markup support.


Announcing djangoembed, rich media consuming and providing with Django July 27, 2010 18:08 / 5 comments

I'm pleased to announce the release of djangoembed, a django app for consuming and providing rich media.

What is OEmbed?

OEmbed is a format for allowing a rich representation of a url. If you've used Facebook you've probably seen this feature before -- linking a YouTube video will embed an actual video player in the news feed, automatically. The player is represented by some HTML, plus there may be additional metadata like the author, a link to their channel, the title of the video, or even a thumbnail.


Using virtualenv, pip and django-site-gen to quickly start new Django projects July 03, 2010 10:51 / 4 comments

Last week, after several false starts, I moved all the sites I maintain into virtualenvs, with their own pip requirements files. My reasons for doing so are pretty simple:

  • Experimenting with new/different versions of software is a pain in the ass without isolation
  • A pip requirements file for each site is a very nice thing to behold
  • I symlink 3rd party apps into a custom directory on my PYTHONPATH and it was getting huge
  • Profit

There are quite a few great tutorials out there for getting started with these tools. I will only discuss how I got over some of the hurdles involved in using these tools, as well as a tool for automating the creation of "skeleton" django sites.


How to set up Solr on Ubuntu 10.04 (or whatever) June 19, 2010 00:32 / 0 comments

Apache Solr is a fast, open-source search solution. People are doing some very cool things with Solr. I personally have only begun to scratch the surface of what is possible with Solr, but have seen amazing returns with a relatively small investment (thanks entirely to Daniel Lindsley's excellent search framework, django-haystack). There are instructions for getting up and running with Solr + Jetty -- the purpose of this blog entry is to walk through setting up multi-core Solr with Apache Tomcat.


Quick shell command to add CSRF token June 16, 2010 23:18 / 2 comments

To get the benefit of Django 1.2's new CSRF protection, all POST forms will need a special token. Here is a quick command that runs through templates adding the token:

find . -type f -name "*.html" -exec sed -i \
's|\(<form[^>]*method="post"[^>]*>\)\({% csrf_token %}\)\?|\1{% csrf_token %}|g' \
{} \;

Look at all your different colored hats! May 28, 2010 10:54 / 0 comments

This post will be very brief, but I want to show a little trick I'm using on my different servers so I can tell them apart at a glance. I use a custom bash prompt which gives the hostname of each server a different color:

[charles@alpha ~] $
[charles@beta ~] $

Generating aggregate data across generic relations May 22, 2010 19:22 / 7 comments

Aggregation support was added to django's ORM in version 1.1, allowing you to generate Sums, Counts, and more without having to write any SQL. According to the docs aggregation is not supported for generic relations. This entry describes how to work around this using the .extra() method.


django-site-gen, a tool for automating site creation May 03, 2010 18:14 / 2 comments

Most django sites I create have quite a lot in common. Beyond the handful of files generated by django-admin startproject, my projects all have a database, wsgi file, apache and nginx confs, static media and templates. All these building blocks of a site vary very little from project-to-project. django-site-gen allows you to automate the creation of the stuff that doesn't vary.


Idea for a simple task queue May 01, 2010 12:45 / 1 comments

cue is a simple queue abstraction along the lines of the command pattern, and provides django apps with a way to decouple the creation of a request from its execution.


A handful of snippets April 24, 2010 14:36 / 4 comments

A collection of a few snippets I've made use of recently.


Topeka and Google April 01, 2010 10:02 / 0 comments

Topeka, the state capitol of Kansas, is about 30 minutes down the road from Lawrence, KS where I work. In a recent publicity stunt, they've been talking about renaming the capitol to 'Google' in order to bring Google fiber-optics to the city. Here's Google's response.


Announcing django-relationships March 27, 2010 19:00 / 7 comments

I recently posted on writing an app that allows you to create flexible and descriptive relationships between Django's built-in auth.users. django-relationships is the result.