Archives for May, 2009:

May posted 31 May 2009 and tagged , ,

This function pipes a javascript file to a Python implementation of Douglas Crockford’s handy JSMin, a javascript minifier.

minify() { if [ -f $1 ] then MIN=${1%.[^.]*}.min.js cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js BEFORE=`wc -c <$1` AFTER=`wc -c <$MIN` echo "$BEFORE $1" echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)" else echo "$1 not found" fi }

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
minify() {
  if [ -f $1 ]
  then
    MIN=${1%.[^.]*}.min.js
    cat $1 | python ~/path/to/jsmin.py > ${1%.[^.]*}.min.js
    BEFORE=`wc -c <$1`
    AFTER=`wc -c <$MIN`
    echo "$BEFORE $1"
    echo "$AFTER $MIN ($(echo "scale=2; 100*$AFTER/$BEFORE" | bc)%)"
  else
    echo "$1 not found"
  fi
}

It also prints the minified version’s character count as a percentage of the original’s:

$ minify prototype.js 

  126127 prototype.js
   93689 prototype.min.js (74.28%)

May posted 14 May 2009 and tagged

import os

Pop out

1
import os

List all files in a directory recursively, with relative paths:

def list_dir(dir_name): for r, d, f in os.walk(dir_name): files = [os.path.join(r, n) for n in f if not n.startswith('.')] return files

Pop out

1
2
3
4
def list_dir(dir_name):
    for r, d, f in os.walk(dir_name):
        files = [os.path.join(r, n) for n in f if not n.startswith('.')]
    return files

Create a directory if it doesn’t already exist:

def make_dir(dir_name): if not os.path.isdir(dir_name): os.mkdir(dir_name)

Pop out

1
2
3
def make_dir(dir_name):
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)

Empty an existing directory:

def empty_dir(dir_name): if not os.path.isdir(dir_name): os.mkdir(dir_name) else: [os.remove(f) for f in list_dir(dir_name)]

Pop out

1
2
3
4
5
def empty_dir(dir_name):
    if not os.path.isdir(dir_name):
        os.mkdir(dir_name)
    else:
        [os.remove(f) for f in list_dir(dir_name)]

May posted 10 May 2009 and tagged

^a c
Create new window (shell)
^a k
Kill the current window
^a w
List all windows
(the current window is marked with *)
^a 0-9
Go to a window numbered 0-9
^a n
Go to the next window
^a ^a
Toggle between the current and previous window
^a [
Start copy mode
^a ]
Paste copied text
^a ?
Help (display a list of commands)
^a ^\
Quit screen
^a D
Power detach and logout
^a d
Detach but keep shell window open
space or return
End a command

Source: Indiana University UITS

May posted 4 May 2009 and tagged ,

RewriteEngine On RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Pop out

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

May posted 1 May 2009 and tagged

This is just something I find myself having to do a lot, so here’s a convenient little function to reverse sort a Python dictionary by value.

from operator import itemgetter

Pop out

1
from operator import itemgetter
def sort_dict(d, sort_key=1, rev_bool=True): return sorted(d.items(), key=itemgetter(sort_key), reverse=rev_bool)

Pop out

1
2
def sort_dict(d, sort_key=1, rev_bool=True):
    return sorted(d.items(), key=itemgetter(sort_key), reverse=rev_bool)