HTML — Non Destructive HTML Tabifier

For whatever reason, if you need a non destructive HTML tabifier, I’ve found a great one. It will NOT modify your code.

http://tools.arantius.com/tabifier

I’m working in a shopping cart environment that can only handle one template file for the product pages and it must be < 64kb, meaning I've started to remove all whitespace to fit in my templates.

You really can't argue that the size should be below 64kb, if you have one template to handle infinite product types.

Python — If Else Condition in Lambda Expression

I was writing a lambda function to decode unicode into UTF8 but needed a conditional as some items would be None.

The syntax for inline if else is..

'result' if 'condition' else 'else result'
map(lambda x: x.encode('UTF8') if isinstance(x, unicode) else x.__str__(), mylist)

Django — Ordering Admin / ModelAdmin Inlines

Ordering admin inlines is a common problem with a not so documented fix that I created. Even the BaseInlineFormSet.get_queryset() isn’t documented. By the way, it’s ever so slightly confusing that managers use `get_query_set` and this uses `get_queryset`.

To order admin inlines, specify the FormSet used by the Inline and override get_queryset() in your formset definition with ordering.

from django.forms.models import BaseInlineFormSet

class OrderedFormSet(BaseInlineFormSet):
    def get_queryset(self):
        return super(OrderedFormset, self).get_queryset().order_by('-sortfield')

class MyInline(admin.TabularInline):
    model = Item
    formset = OrderedFormSet

Django Docs Down

The django docs are down!

It occurs to me I’ve been preaching about how amazing the docs are. If I lost access to them, they wouldn’t be so useful.

I’ll go look into downloading them. Apparently they are in the release folder. I’ve never looked 🙂

WordPress — Disable Proofreading

I put this one off for months because I could never find the settings and google searched didn’t return very helpful results.

Proofreading settings live in Users > Personal Settings

Programmatically log into a website with Python and urllib, urllib2. This one for my django.

"""
Log into Django Site
====================


By Yuji Tomita
2/22/2011
"""

import urllib
import urllib2
import contextlib


def login(login_url, username, password):
    cookies = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookies)
    urllib2.install_opener(opener)
    
    opener.open(login_url)
    
    try:
        token = [x.value for x in cookies.cookiejar if x.name == 'csrftoken'][0]
    except IndexError:
        return False, "no csrftoken"
    
    params = dict(username=username, password=password, \
        this_is_the_login_form=True,
        csrfmiddlewaretoken=token,
         )
    encoded_params = urllib.urlencode(params)
    
    with contextlib.closing(opener.open(login_url, encoded_params)) as f:
        html = f.read()
        
        print html
    # we're in.

cProfile Example and iPython Magic Shortcut %prun

Running cProfile is a bit of a pain.

You have to pass your function and all its scope as a string to cProfile, which is.. just a pain.

cProfile.run('''
def test(n):
    x = 1
    for i in range(n):
        x = x * 2
''')

iPython has a magic function that lets you use the current scope.

def test(n):
    x = 1
    for i in range(n):
        x = x * 2
 %prun test(100000)

         4 function calls in 0.753 CPU seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.752    0.752    0.753    0.753 :1(test)
        1    0.002    0.002    0.002    0.002 {range}
        1    0.000    0.000    0.753    0.753 :1()
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Jquery — Fire event when all images have loaded within element

I needed a smooth fadeIn effect after all images had been loaded in a specific element.

I solved the problem by incrementing a load counter and executing a function when the counter reaches the number of elements to load.

load_counter = 0;
$.each(my_images, function(i, item) {
    $(item).load(function() {
        load_counter ++;
        if (load_counter == my_images.length) {
            // all items have loaded
            $("#my_element").fadeIn();
        }
    })
})