Posts tagged python:

Jun posted 19 Jun 2009 and tagged

How handy! Lifehacker says:

Google Docs Download, a Greasemonkey script previously featured here for creating a Google Docs bulk download feature that should already be there, is now available as a (beta-level) Python script.

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 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)

Apr posted 30 Apr 2009 and tagged

from htmlentitydefs import name2codepoint as n2cp import re

Pop out

1
2
from htmlentitydefs import name2codepoint as n2cp
import re
def substitute_entity(match): ent = match.group(3) if match.group(1) == "#": if match.group(2) == '': return unichr(int(ent)) elif match.group(2) == 'x': return unichr(int('0x'+ent, 16)) else: cp = n2cp.get(ent) if cp: return unichr(cp) else: return match.group()

Pop out

1
2
3
4
5
6
7
8
9
10
11
12
13
def substitute_entity(match):
    ent = match.group(3)
    if match.group(1) == "#":
        if match.group(2) == '':
            return unichr(int(ent))
        elif match.group(2) == 'x':
            return unichr(int('0x'+ent, 16))
    else:
        cp = n2cp.get(ent)
        if cp:
            return unichr(cp)
        else:
            return match.group()
def decode_htmlentities(string): entity_re = re.compile(r'&(#?)(x?)(\w+);') return entity_re.subn(substitute_entity, string)[0]

Pop out

1
2
3
def decode_htmlentities(string):
    entity_re = re.compile(r'&(#?)(x?)(\w+);')
    return entity_re.subn(substitute_entity, string)[0]

Example usage:

print decode_htmlentities("l'eau")

Pop out

1
print decode_htmlentities("l&#39;eau")

Source: http://snippets.dzone.com/posts/show/4569

(NB: this problem – and many, many others – can be more easily solved by BeautifulSoup, but if this is all you need to do, you might not want to pay the computational price.)

Apr posted 30 Apr 2009 and tagged

import os

Pop out

1
import os
def log(lines, max=100): n = 1 while n < max: filename = 'log_%s.txt' % (str(n)) if os.path.exists(filename): n += 1 continue else: open(filename, 'w').writelines([item + '\n' for item in lines]) break

Pop out

1
2
3
4
5
6
7
8
9
10
def log(lines, max=100):
    n = 1
    while n < max:
        filename = 'log_%s.txt' % (str(n))
        if os.path.exists(filename):
            n += 1
            continue
        else:
            open(filename, 'w').writelines([item + '\n' for item in lines])
            break

Apr posted 21 Apr 2009 and tagged

def pretty_filesize(bytes): if bytes >= 1073741824: return str(bytes / 1024 / 1024 / 1024) + ' GB' elif bytes >= 1048576: return str(bytes / 1024 / 1024) + ' MB' elif bytes >= 1024: return str(bytes / 1024) + ' KB' elif bytes < 1024: return str(bytes) + ' bytes'

Pop out

1
2
3
4
5
6
7
8
9
def pretty_filesize(bytes):
    if bytes >= 1073741824:
        return str(bytes / 1024 / 1024 / 1024) + ' GB'
    elif bytes >= 1048576:
        return str(bytes / 1024 / 1024) + ' MB'
    elif bytes >= 1024:
        return str(bytes / 1024) + ' KB'
    elif bytes < 1024:
        return str(bytes) + ' bytes'

Converted from a similar VBasic function