For when you need to import lots of data programmatically from a CSV

Example usage:

./manage.py message_script yourscriptname.py yourcsv.csv
· · · ◊ ◊ ◊ · · ·

Clearfix

06 Mar 2010

· · · ◊ ◊ ◊ · · ·

Sanitize WYSIWYG

05 Mar 2010

Sometimes the WYSIWYG fills your page with inline styles and odd class attributes. This Django filter removes them:

An example use case is:

{{ self.content|cleanhtml|safe }}
· · · ◊ ◊ ◊ · · ·

It can be handy to create an external link in the content:

More information about MessageCMS

· · · ◊ ◊ ◊ · · ·

This script uses the Haversine formula to find the distance between a starting point and an array of points, and then sorts the array to return the point that is the shortest distance from the starting point.

· · · ◊ ◊ ◊ · · ·
$('.no-download a').bind("contextmenu", function(e){
    e.preventDefault();
});
· · · ◊ ◊ ◊ · · ·

I recently needed to programmatically darken dynamically generated colors using JavaScript. To do this I used the Underscore.js library. In Gecko and Webkit, $(‘elem’).css(‘background-color’) returns an RGB value, but in IE it returns a Hexadecimal value. This function checks for each and uses Underscore’s map function to quickly convert into a darker version of itself. Pass a color (c) and a decimal value (v) into darken and receive a darker RGB value.

function darken (color, value) {
    var rgb;
    if (/^rgb/.test(color)) {
         // If darkening an RGB color
        rgb = _.map(color.match(/\((\d+), ?(\d+), ?(\d+)\)/).slice(1), function(num){
            return parseInt(num, 10);
        });
    } else if (/^#/.test(color)) {
         // If darkening a Hexadecimal color
        rgb = _.map(color.match(/^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})/i).slice(1), function(num){
             return parseInt(num, 16)
        });
    } else {
        return 'Invalid color type.'
    };
    var result = _.map(rgb, function(item) { return Math.floor(value*item) });
    return 'rgb('+result.join(',')+')';
}
· · · ◊ ◊ ◊ · · ·

The radix parameter is the second parameter passed into JavaScript’s parseInt function. In most cases, you should pass in 10 to prevent possible mistakes.

>>> parseInt("8")
8
>>> parseInt("08")
0
>>> parseInt("010") // A juicy mistake, octal numbers.
8
>>> parseInt("0x10") // Probably rare, but possible, hexadecimals.
16
>>> parseInt("08", 10) // Prevent problems, use the radix.
8
>>> parseInt("010", 10)
10

Reference: The Client Side

· · · ◊ ◊ ◊ · · ·
Using Image Capture to retrieve iPhone video files

Image Capture app

Having owned an iPhone 3GS for a while, I wanted to pull all the videos from the phone to my Mac harddrive for archival. After fiddling with iPhoto and other applications and finding them frustrating, I’ve found the best way to do this is to open Image Capture.app, select all media, and transfer.

· · · ◊ ◊ ◊ · · ·

When iterating over objects in a Python list in a Django template, it is nice to output a comma-separated list. This method also ensures that if only one object is output, it will not have a comma.

{% for item in list %}
{% if not forloop.first %}, {% endif %}
{{ item }}
{% endfor %}

If you had list = ['one'], this would output:

one

If you had list = ['one','two','three'], this would output:

one, two, three
· · · ◊ ◊ ◊ · · ·