Happy PI day!

PI from Wikipedia

I’ve got to learn my decimals again, shame on me!

Posted in en, geek, math | Leave a comment

Compare files in two directories

This is how you do it:

$ diff < (cd /path/one/ && find .) <(cd /path/two/ && find .)

The trick is using Sub shells and fake their output as a file. Then diff will just think you're comparing two files ;) .

You have to cd to each folder in order to avoid showing the relative path in the output.

Posted in en, geek | Tagged | Leave a comment

De vuelta del canaval de Barranco

Posted in Forgot to categorize | Leave a comment

I smashed into a timeline – My twitter background

I smashed into a timeline

Posted in geek | Leave a comment

Vim not showing colors

This is what you need, add it to your ~/.profile or any file that loads at your shell start up.

export TERM=xterm-color vim

Make syre to have syntax on in your ~/.vimrc

Posted in Forgot to categorize | Leave a comment

Django random object manager

Several times in the past I needed to fetch a list of random objects. And Today I decided to make a Mixin class to add a get_random() method to my managers:


# -*- coding: utf-8 -*-

from random import sample

class RandomObjectManager(object):
    """
    Manager Mixin to implement get_random() in your models.
    You can override get_objects to tune the queriset

    To use, define your class:

    class MyManager(models.Manager, RandomObjectManager):
        DEFAULT_NUMBER = 5 # I can change that

        def get_objects(self):
            return self.filter(active=True) # Only active models plz

    class MyModel(models.Model):
        active = models.BooleanField()
        objects = MyManager()

    Now you can do:
    MyModel.objects.get_random()

    """

    DEFAULT_NUMBER = 3

    def get_objects(self):
        return self.all()

    def get_random(self, number=DEFAULT_NUMBER):
        """
        Returns a set of random objects
        """
        ids = self.get_objects().values_list('id', flat=True)
        amount = min(len(ids), number)
        picked_ids = sample(ids, amount)
        return self.filter(id__in=picked_ids)

I’ve added this as a Django snippet and as a Gist.

Posted in django, en, Python | Tagged , , | 3 Comments

What I’m doing to the pictures I upload anywhere


$ mogrify -fill white -pointsize 16 -annotate +20+20 'If you\'re seeing this picture in Facebook, the person who uploaded it is an ass' -thumbnail 800x800 -quality 75 picture.jpg

Posted in en, geek, programacion | Tagged , , | Leave a comment

Add ‘error’ class to widgets with errors in Django Forms

Many times UI requires us to style differently widgets that have errors in a field instead or next to an error message.

Django provides a nifty .errors attribute to tell if a field of form has errors but you can’t affect the rendering of a widget with it.

And going through all your widgets can be a pain when you only need that ona extra HTML class:

<input type="text" class="errors" name="my_field" id="id_my_field"/>

I came up with this dirty snippet that gets the job done:

class MyForm(forms.Form):
    """
    Extend from this form so your widgets have an 'error' class if there's
    an error on the field.
    """
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        if self.errors:
            for f_name in self.fields:
                if f_name in self.errors:
                    classes = self.fields[f_name].widget.attrs.get('class', '')
                    classes += ' errors'
                    self.fields[f_name].widget.attrs['class'] = classes

Now you can either extend from that class or encapsulate in a method, add to all your __init__ on your forms or whatever.

Posted in css, django, web | Tagged , , , | 2 Comments

Disable Firefox toolbar animation on full screen

The title is the string I was looking for when I needed do to this.

Since I use Google reader on my Netbook, I like to use fullscreen view to maximize pixels.

I just hate how slow it takes to switch between modes because of the silly ainmation of the toolbar/awesomebar which forces the browser to redraw the content after each pixel.

Well, here’s how to disable that. As expected, go to about:config and search for the string:

browser.fullscreen.animateUp

And set it to 0.

Posted in Forgot to categorize | Leave a comment

Ubiquity script for Goo.gl

Google URL shortener logo Since google just made their URL shortener service public, I decided I would start using it :) .

Going to the page and pasting a URL is too much work, Installing a firefox extension is too much bloat. So since I already use Ubiquity I figured I’d write a command to do so.

So here it is Goo.gl ubiquity script.

Posted in en, geek, Google, javascript, tech, web | Tagged , , | Leave a comment