Thursday, October 20, 2011

Madly follow PEP8 with git help

If you intended or are obliged to strictly comply to PEP8 it may be useful to have a git hook that takes care of checking your code before you have committed and prevents you from amending previous commit again and again.

So, in .git/hooks/ directory of your repository create a file with name pre-commit and put

#!/bin/sh

# comma-separated list of files and directories which are not for checking
SKIP=migrations,bootstrap.py,dodo.py,manage.py

pep8 -r --exclude=$SKIP .

UPDATE: The code above checks all the project Python files. To limit checking only for files going to be committed

#!/bin/sh

FILES=$(git diff --cached --name-status | awk '$1 $2 { print $2}' | grep -e \.py$)
if [ -n "$FILES" ]; then
    pep8 -r $FILES
fi

The command shows all staged files, next only the filenames are left and filters Python files. If any they are to be checked with PEP8 utility.

Saturday, October 15, 2011

Word and text translation bookmarklet

Sometimes (I hope rarer and rarer :)) I need to translate an unknown foreign word on the page. I used to select it, open Google Dictionary Translate, paste it, set source and destination languages if needs and only then press the button to translate it.

Some time later I discovered bookmarklets. I liked the approach and made one for myself.

Drag this link En<>Ru the bookmark panel to be able translate words or text.

To change default source and destination languages put their codes in place of en and ru.

Source code.
(function() {
    var t;
    try {
        t= ((window.getSelection && window.getSelection()) ||
            (document.getSelection && document.getSelection()) ||
            (document.selection &&
            document.selection.createRange &&
            document.selection.createRange().text));
    }
    catch(e){
        t = "";
    }

    if(t && (t=t.toString()) && t.length){
window.open("http://translate.google.com/?sl=en&tl=ru&hl=en&text="+t+"#en|ru|"+t);
    }
})()

I used Javascript Compressor to get compressed version to use it in the bookmarklet.

Friday, October 14, 2011

Uninstall XCode

To uninstall XCode from Mac OS X run in terminal:
sudo /Developer/Library/uninstall-devtools –mode=all

Tuesday, October 11, 2011

Upgrading Mac OS X to Lion and dealing with virtualenv

I happened to have successfully upgraded my Snow Leopard to Lion. It looks nicer, softer and so forth. But I depend on my MacBook Pro in my work and have to have everything working.

Actually after upgrade everything were broken: python did not work properly, system paths were set in their defaults. So it was absolute mess.

First, as I learned it from Snow Leopard, I have downloaded new XCode from Mac App Store. It quote big and it took all night (while I was sleeping) for downloading. In the morning I installed and all became better but one. Virtual environments were still spoiled and did not work.

After a few tries and researches I came up with the following:

sudo easy_install pip # it installs new (or even) pip version
sudo pip install virtualenvwrapper

After those commands all get working.

Thursday, October 6, 2011

CKEditor autosave plugin

If you need autosave function for the CKEditor I made a plugin. It allows to save automatically the work as you've stopped typing.

UPD  Oct 6, 2011: Plugin has moved to GitHub.