Django Architecture Approaches

I’ve just had a very interesting conversation with my colleague Marco about different approaches to the organization of code inside a Django application.

As you might know (and if you don’t I’ll tell you anyway), Django’s views (somehow occupying the “Controller” level in an MVC architecture) must take (at least) an HttpRequest instance as a parameter and must return an HttpResponse instance. That’s how it goes in Django, this is the law. This means that you must be sure that the last instruction in your request processing code (in whichever way you’ve organized it) must return an HttpResponse instance, usually calling the HttpResponse() constructor (or of any of its useful subclasses), or by calling the django.shortcuts.render_to_response() function, or something similar.

This has, in my opinion, a major drawback: it might limit code reuse and it increases the coupling in the code. Everything’s not lost, however. Continue reading

Screen Savers for the Mac using Flash

This is an evening project that turned out to be really cool. Let’s say that you’re a Macromedia Flash designer, and your clients ask you to bundle your nice Flash movie as a screen saver. What to do? For Windows there are free utilities to convert a SWF into a screen saver, but not for the Mac – and the first commercial one costs around USD 200!

I propose here a simple solution for this problem:

  1. Using Xcode, you can create screen savers (basically, Cocoa apps).
  2. Cocoa applications can host a WebKit component (basically, Safari).
  3. Safari can show local HTML pages (basically, “file:///” stuff).
  4. And HTML pages can show Flash movies (basically, <OBJECT> and <EMBED>)

The idea, then, is to bundle your own page, with your own movie, inside the screen saver bundle, and show the Flash movie this way. Easy said, easy done.

It all goes nice until… you try this solution :) The problem is, Flash movies loaded from “file:///” URLs are blocked by the built-in security mechanisms of Adobe… and you have to find a workaround for that. Mine was to follow the instructions on how to bypass the Flash security mechanism, and then create an installer package that will do what’s needed for you to enjoy the screen saver.

You can get both the project and the installer package in my projects section. I’ve tested the installer in three different machines, and it worked, so I hope it’ll work for you too :) Enjoy! As always, try this at your own risk. Murphy says that things can go wrong, so watch out.

SQLite Blessing

I just found this in the SQLite source code, just fantastic:

[source:c::firstline(30)] /************** Begin file sqliteInt.h ***************************************/ /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. **


** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.658 2008/01/30 16:14:23 drh Exp $ */

ifndef SQLITEINT_H

define SQLITEINT_H

[/source]

How to count words in LaTeX files?

I am a big LaTeX fan, mostly thanks to my friend Cedric who introduced me to it ;) And I don’t regret it at all; there is simply no better way to create long, beautiful PDF documents, particularly during these times of dissertation writing! I’m in my last step towards the Master’s degree I’ve been working on for the last two years, and creating documents is an important part of that.

LaTeX works for me, because:

  1. It’s cross-platform (and I need that for my project!);
  2. It’s text based (I can edit the files with any decent editor; personally I use and TexShop and sometimes TextMate);
  3. I can generate PDF, plain text, RTF, and much more from the same source;
  4. I can split my documents in several others and work separately in each;
  5. I can generate meaningful diffs using Subversion (to see what I’ve changed in every revision);
  6. I can manage the bibliography for my papers easily (using the awesome BibDesk tool);
  7. I don’t have to cope with a buggy text editor that crashes every so often!
  8. I can generate gorgeous, absolutely beautiful documents. Easily.

For my last document, the dissertation, I have a numeric limit in the number of words (~ 10K to 15K words) and I need to count the number of words in the documents I generate. Since I’m not using Word, nor KOffice nor OpenOffice, this simple requirement becomes more complex to fulfill. But working in a Unix environment has its benefits; first I found this solution:

$ detex file.tex | wc -w

This command provides a first approach to the problem; however, it just strips off the LaTeX commands, even those that generate content in the final document. For example, if you have a macro that puts in bold the name of your project, those words will not appear in the final calculation even if they do appear in the final document. Clearly not acceptable. Googling a bit more, I found what I was looking for:

$ ps2ascii file.pdf | wc -w

In this case we’re working on the final PDF document, and of course the final result is much, much more interesting.

Happy typesetting!

That nice freedom of modifying software

One of the best learning tools I have found in my career is to take someone else’s code, and to modify it slightly to see what happens, to play with it, and eventually to release that code in this blog, or send it to the original author, fixing it somehow or adding some feature:

Continue reading

Install MySQL_python in Leopard

I wanted to make my Django blog engine work on Leopard using MySQL as a database engine. I had a hard time making it work, partially because of my lack of knowledge of Python, partially because I am using MAMP instead of a “/usr/local/mysql”-like MySQL installation, partially because of Leopard itself.

The problem is, basically, that if you try to do the “easy_install MySQL_python” thing, it won’t work in Leopard (the compilation of the native code fails). Here’s how I made it work, following the instructions in this post in the MySQL forums, and doing some tweaking manually. Continue reading

git

If you haven’t tried git, you should. Git is a “distributed version control” system, that is, similar to Subversion with the big difference that… you do not need a server. There are only clients, any of them, and you can pull and push changes to and from other repositories from your project colleagues. The git Wikipedia entry does a much better job than me to introduce the subject :)

In Mac OS X, I’ve just downloaded the source code tarball with the latest snapshot of the code, and it compiled out of the box. Just the classical operations:

./configure
make
sudo make install

Then I went through the tutorial, and frankly, I loved it. The Everyday GIT Guide is excellent too to understand the idea of this beautiful piece of code.

It’s too cool, really. Lightweight, and damn fast. No wonder why everyone’s talking about it lately!

Anarquismo y Software Libre

Anarquismo Triunfante: Software Libre y la Muerte del Copyright, por Eben Moglen (1999), profesor de ley e historia legal de la Universidad de Columbia, USA:

Oscar Wilde dijo alguna vez que el problema con el socialismo es que ocupa demasiadas noches. Los problemas del anarquismo como un sistema social estan tambien relacionados con los costos de transaccion. Pero la revolucion digital altero dos aspectos de la politica economica que de otra manera permanecieron invariantes durante la historia humana.Todo software tiene un costo marginal nulo en el mundo de la red, mientras que los costos de coordinacion social han sido reducidos de tal manera que permiten la formacion y disolucion rapidas de groupos sociales altamente diversos y de gran escala sin importar las limitaciones geograficas. Un cambio tan fundamental en las circunstancias materiales de la vida necesariamente implican cambios igualmente importantes en la cultura.

Tremendo articulo, lamentablemente en ingles pero que merece una lectura atenta.