Archives for April, 2009:

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'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 27 Apr 2009 and tagged , ,

Disable:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Pop out

$
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Reenable:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Pop out

$
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

Source: http://tech.karbassi.com/2007/11/06/leopard-turn-off-bonjour-mdnsresponder/

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

Apr posted 8 Apr 2009 and tagged

/SET autolog_path ~/.irssi/logs/$tag/$0.log

Pop out

1
/SET autolog_path ~/.irssi/logs/$tag/$0.log
/SET autolog_path ~/.irssi/%Y/$tag/$0.%m-%d.log

Pop out

1
/SET autolog_path ~/.irssi/%Y/$tag/$0.%m-%d.log