klenwell information services : CocktailNapkin2010

2010 Cocktail Napkin

return to SpecialCocktailNapkin Current Napkin

Emotibot: \m/(><)\m/
Twitter Bot Bracketology
NYR: Create a profile online for the person you want to be in the New Year, and be that person next year. (C. E. Francis)
Restaurant Templates, Medical Office Templates
TIL How much warning we'd have if a killer asteroid were bearing down on Earth...
Enabled cron for wiki on new vps
A meme: 1 2 3 4
Updating WordPress: http://codex.wordpress.org/Updating_WordPress
Python list.get(n):
list_get = lambda l,n: len(l) > n and l[n]

l = [0,1,2,3]
print list_get(l,0)
print list_get(l,3)
print list_get(l,4)

Twitter Search API app engine task queues
php int to hash (for ids):
function int_to_hash($int, $set_key='limited') {    
	$CodesetList = array(
		'full' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
		'limited' => 'abcdefghijklmnopqrstuvwxyz',
	$pad = 100000;
	$codeset = $CodesetList['limited'];
	if ( isset($CodesetList[$set_key]) ) {
		$codeset = $CodesetList[$set_key];
	$hash = '';
	$base = strlen($codeset);
	$int += $pad;    
	while ($int > 0) {
		$hash = substr($codeset, ($int % $base), 1) . $hash;
		$int = floor($int/$base);
	return $hash;

$TestSet = array(1,2,10,100,1000,10000,100000,999999,1000000);

print '<table>';

foreach ( $TestSet as $i ) {
	printf('<tr><td>%s</td><td>%s</td></tr>', $i, int_to_hash($i));

print '</table>';

mysql: "group by most recent"
That is: group results of a query such that only the most recent row of each group is returned
ref: http://www.artfulsoftware.com/infotree/queries.php?&bw=920#101
# NOTE: this has not yet been tested
  FROM searches
  WHERE terms = 'foo'
  ORDER BY updated DESC
) AS s
LEFT JOIN results AS r ON r.id = s.result_id
GROUP BY result_id;

python nltk unit test
import unittest
import nltk

import sys, os
from os.path import dirname, exists, join, abspath
from datetime import date

# set base dir
base_marker = "__base__"    # add this file to base dir
def find_base(base=dirname(__file__)):
	if exists(join(base, base_marker)):
		return base
	if abspath(base) == '/':
		raise Exception("Can't find project base!  I was expecting it to be ../ or ../../.")
	return find_base(join(base, '..'))


class TestTagging_5_1(unittest.TestCase):

	def setUp(self):
	def tearDown(self):

	def testSimplePosTagger(self):        
		text = nltk.word_tokenize("And now for something completely different")
		pos_tags = nltk.pos_tag(text)
		self.assertEqual(pos_tags[2], ('for', 'IN'))
		#print pos_tags

test_case_list = [TestTagging_5_1]

# main
if __name__ == "__main__":
	for case in test_case_list:
		suite = unittest.TestLoader().loadTestsFromTestCase(case)

freakonomics blog NNT
bash pid example
function start() {
	if [[ -e $PID_FILE ]]; then
		echo "server already running"
		echo "if not running, try deleting pid file $PID_FILE"
		exit 1

	paster serve development.ini &
	sleep 1
	echo $PID > $PID_FILE
	echo 'server started'

function stop() {
	if [[ ! -e $PID_FILE ]]; then
		echo "pid file $PID_FILE not found"
		echo "try 'ps aux' to stop manually"
		exit 1
	PID=`cat $PID_FILE`
	kill $PID
	echo 'server stopped'

bash find base:
BASE=$(dirname $(readlink -fn $0))/..

parchment style: http://rlv.zcache.com/shakespeare_quotes_calendar-p1580846210289528122vxoc_400.jpg
app engine cron question
nltk example
import nltk

#pdl = nltk.corpus.cmudict.entries()
pd = nltk.corpus.cmudict.dict()

def meter(pron):
    return ''.join([str(int(bool(int(c)))) for p in pron for c in p if c.isdigit()])
def get_meter_list(m):
    return [w for w, p in pdl if meter(p) == m]
#print len(get_meter_list('0'))
#print len(get_meter_list('1'))

def tokenize(sent):
    return [w.strip().lower() for w in sent.split(' ')]
def scan(line):
    m = []
    for w in tokenize(line):
    return ''.join(m)

sent = 'A rose is a rose is a rose is a rose'
print scan(sent)

python buffer capture (assign stdout to a variable)
class BufferTestCase(unittest.TestCase):
	def openBuffer(self):
		import StringIO, sys
		self.old_buffer = sys.stdout
		sys.stdout = self.new_buffer = StringIO.StringIO()
		return self.old_buffer
	def closeBuffer(self):
		buffer_content = self.new_buffer.getvalue()
		sys.stdout = self.old_buffer
		return buffer_content
	def testBuffer(self):
		print 'hello'
		buffer = self.closeBuffer()        
		self.assertEqual(buffer, 'hello\n')  

python string diff:
from difflib import Differ
d = Differ()
print '\n'.join(list(d.compare(app_output, expect)))

google group question: serve atom through appengine
import re
def re_ungreedy(re_, s):
	r = re_.replace('%s', '(.*?)')
	return re.search(r, s).groups()
re_ = "<h2>%s</h2>"
H2List = re_ungreedy(re_, '<h2>one</h2><h2>two</h2>')

python debugger: import pdb; pdb.set_trace()
php safe query builder
	function safe_sql($sqlf, $ParamList) {
		$SafeParamList = array();
		foreach ( $ParamList as $value ) {
			$SafeParamList[] = mysql_real_escape_string($value);
		$SprintfArgList = array_merge(array($sqlf), $SafeParamList);
		return call_user_func_array('sprintf', $SprintfArgList);

PEAR HTTP_Request2 docs api

	HTTP_Request2 Demo
	Lib File Tree

$wwwd = dirname(dirname(__FILE__));
$peard = sprintf('%s/%s', $wwwd, 'kw_pear');
$path_ext = implode(PATH_SEPARATOR, array(get_include_path(),
# imports
ini_set('include_path', $path_ext); 
require_once 'HTTP/Request2.php';

$UrlList = array(
	'ok' => 'http://pear.php.net/package/HTTP_Request2/docs',
	'404' => 'http://pear.php.net/package/HTTP_Request2/docs/null',
	'fail' => 'bad url'

$TestList = array_keys($UrlList);
$test = $TestList[array_rand($TestList)];
$url = $UrlList[$test];
$Client = new HTTP_Request2();

try {
	$Response = $Client->send();
	$code = $Response->getStatus();
	$code_class = floor($Response->getStatus() / 100);
catch (HTTP_Request2_Exception $e) {
	trigger_error(sprintf('http exception: %s',
						  $e->getMessage()), E_USER_ERROR);

if ( $code_class == 2 ) {
	$output = htmlspecialchars($Response->getBody());
else {
	$output = sprintf('unexpected response [%s]: %s',

printf('<h4>test: %s</h4><pre>%s</pre>', $test, $output);

copy with exclude: rsync -rv --exclude=.svn source/* dest
wikka update page function:
function update_page($page_name, $page_body, $WikkaObj) {
	$updatef = "UPDATE %s SET latest='N' WHERE tag='%s'";
	$insertf = "INSERT INTO %s SET tag='%s', body='%s', owner='%s', user='%s', note='%s', time=NOW(), latest='Y'";
	$note = 'auto updating page';
	$user = '(system)';
	$table_prefix = $WikkaObj->GetConfigValue('table_prefix');
	$table_name = sprintf('%spages', $table_prefix);
	$page_name = mysql_real_escape_string($page_name);
	$page_body = mysql_real_escape_string($page_body); 
	if ( $WikkaObj->existsPage($page_name, $table_prefix) ) {
		# set all other revisions to old
		$is_updated = $WikkaObj->Query(sprintf($updatef, $table_name, $page_name));
	# add new revision
	return $WikkaObj->Query( sprintf( $insertf,
									  $note )); 

To call a script, you can make it as handler and put it at (as of the directory named handlers/page/, ie like handlers/page/cron.changemail.php source
function log() {
	datestamp=$(date "+%Y-%m-%d %H:%M:%S")
	printf "[%s] %s\n" "$datestamp" "$1"

CACHE_FILE_COUNT=$(find /tmp -type f | wc -l)
if [ $CACHE_FILE_COUNT -gt 0 ]; then
	log "$CACHE_FILE_COUNT files found in /tmp"
	log "no files found in /tmp"

Office Hacks: during ethnic holidays, go up to people at random and ask if they have any plans. (e.g.: week before passover, ask Chinese co-worker: "so do you have any Passover plans this weekend?")
Against the Gun is Civilization Argument: Pastebin20100226
Pattern Request Page: jquery json ajax request with cakephp
fly bomber (it's supafly)
classic olympic fail
gdata python question on google groups
copying DB in php
    function _switch_database($new_db) {
        $Dbo = ConnectionManager::getDataSource("default");
        $old_db = $Dbo->config['database'];
        return $old_db;
    function _copy_database($db, $new_db) {
        $Dbo = ConnectionManager::getDataSource("default");
        # get tables
        $sql_show_tables_ = 'SHOW TABLES IN %s';
        $extract_pattern = sprintf('{n}.TABLE_NAMES.Tables_in_%s', $db);
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $db));
        $TableList = Set::extract($ShowTables, $extract_pattern);
        # create new database
        $sql = sprintf('CREATE DATABASE %s', $new_db);
        # copy tables
        $sql1_t = 'CREATE TABLE %s.%s LIKE %s.%s';
        $sql2_t = 'INSERT INTO %s.%s SELECT * FROM %s.%s';
        foreach ( $TableList as $table ) {
            $sql1 = sprintf($sql1_t, $new_db, $table, $db, $table);
            $sql2 = sprintf($sql2_t, $new_db, $table, $db, $table);
        # test table lists
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $db));
        $TableList1 = Set::extract($ShowTables, "{n}.TABLE_NAMES.Tables_in_$db");
        $ShowTables = $Dbo->query(sprintf($sql_show_tables_, $new_db));
        $TableList2 = Set::extract($ShowTables, "{n}.TABLE_NAMES.Tables_in_$new_db");
        if ( array_diff($TableList1, $TableList2) ) {
            trigger_error('table lists in new db dows not match old', E_USER_WARNING);
            return 0;
        $this->Log_[] = "all tables in $db copied to $new_db";
        # test query: count all records and compare
        $old_db_count = 0;
        $new_db_count = 0;
        foreach ( $TableList as $table ) {
            $sql_ = 'SELECT COUNT(*) as count_ FROM %s.%s';
            $OldDb = $Dbo->query(sprintf($sql_, $db, $table));
            $NewDb = $Dbo->query(sprintf($sql_, $new_db, $table));
            $old_db_count += $OldDb[0][0]['count_'];
            $new_db_count += $NewDb[0][0]['count_'];
        $rand_table = $TableList[array_rand($TableList)];
        if ( $old_db_count != $new_db_count ) {
            $mt_ = 'record count for new db (%s) and old db (%s) does not match';
            trigger_error(sprintf($mt_, $new_db_count, $old_db_count), E_USER_WARNING);
            return 0;
        else {
            $mt_ = 'record count for new db (%s) and old db (%s)';
            $this->Log_[] = sprintf($mt_, $new_db_count, $old_db_count);
            return 1;

php encoding tests
requires http://hsivonen.iki.fi/php-utf8/php-utf8.tar.gz

    $char = 'Ü';
    $h_char = htmlentities($char);
    $utf8 = htmlspecialchars($char, ENT_QUOTES, 'UTF-8');
    $h_utf8 = htmlentities($utf8);
    $uni_char = utf8ToUnicode($char);
    $data = array(
        '$char' => $char,
        '$h_char - htmlentities($char)' => $h_char,
        '$utf8 - htmlspecialchars($char,ENT_QUOTES,\'UTF-8\')' =>
        'htmlspecialchars(\'Ü\',ENT_QUOTES,\'UTF-8\')' =>
            htmlspecialchars('Ü', ENT_QUOTES, 'UTF-8'),
        '$h_utf8 - htmlentities($utf8)' =>
        'strlen($char)' => strlen($char),
        'strlen($h_char)' => strlen($h_char),
        'strlen($utf8)' => strlen($utf8),
        'strlen($h_utf8)' => strlen($h_utf8),
        'ord($char)' => ord($char),
        'ord($utf8)' => ord($utf8),
        'utf8ToUnicode($char)' => $uni_char,
        'dechex(ord($char))' => dechex(ord($char)),
        'sprintf(\'&amp;#x%s;\', dechex(ord($char)))' =>
            sprintf('&#x%s;', dechex(ord($char))),
        'sprintf(\'&amp;#x%s;\', ord($char))' =>
            sprintf('&#x%s;', ord($char)),
        'sprintf(\'&amp;#%s;\', $uni_char[0])' =>
            sprintf('&#%s;', $uni_char[0]),
        'sprintf("&#x%s;", dechex($uni_char[0]))' =>
            sprintf("&#x%s;", dechex($uni_char[0])),
        'iconv("UTF-8","ISO-8859-1//TRANSLIT",$char)' =>
    $xhtml = <<<XHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

    $charset = 'UTF-8'; #'ISO-8859-1';
    header( 'Content-type: text/html; charset='.$charset );
    printf($xhtml, print_r($data,1));

jquery quiz
== jQuiz: jQuirks ==
A short quiz on jQuery behavior.

===1. What will these lines of code produce?===
var Test1El1 = $('div#Test1El1');
var Test1El2 = $('div').attr('id', 'Test1El2');
var Test1El3 = $('<div id="Test1El3" />');

===2. What will be the output of the code below?===
var Test2El1 = $('<div id="Test2El1" />');
var elid = Test2El1Test2El1.attr('id');
var Test2El2 = $(elid);

===3. True or False===
var id_ = 'test3id';
$('<div id="' + id_ + '" />').appendTo(document.body);
var Test3El1 = document.getElementById(id_);
var Test3El2 = $('#'+id_);
console.log(Test3El1.id == Test3El2.id);

===4. True or False===
var Test4El1 = $('<div id="Test4El" />').appendTo(document.body);
var Test4El2 = $(Test4El1);
console.log(Test4El1 == Test4El2);
console.log(Test4El1.attr('id') == Test4El2.attr('id'));

===Given a document with the following table code, answer the next 2 questions:===
<table id="Test5Table">
  <tr id="Test5Tr1"><td>cell1</td><td>cell2</td></tr>
  <tr id="Test5Tr2"><td>cell1</td><td>cell2</td></tr>

var Test5Table = $('<table id="Test5Table"><tr id="tr1"><td>cell1</td><td>cell2</td></tr><tr id="tr2"><td>cell1</td><td>cell2</td></tr></table>');

====5. What will the value of TableRows[0] be below?====
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');

====6. What will the value of RowIds[0] be below?====
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');
var RowIds = [];
TableRows.each( function(i, El) {

===7. What will the value of FindEl be below?===
// build and add element to our document
var elid = 'formbid-0.field';
var NewDiv = $('<div id="'+elid+'" />').text(elid);

var jqid = NewDiv.attr('id');
var FindEl = $(document.body).find('#'+jqid);    // note # sign
console.log(jqid, FindEl);

<div style="display:none;">Answer:
jesc = jqid.replace('.', '\\.');
var FindEl = $(document.body).find('#'+jesc);
console.log(jesc, FindEl);

music on-demand
this + http://www.dizzler.com/ + http://getfirebug.com/net.html

some cakephp code
	function render($action=NULL, $layout=NULL, $file=NULL) {
		if ( $action == null ) {
			$action = $this->view_action;
		$output = parent::render($action, $layout, $file);
		return $output;

	function index()
		$m = 'null';
		if ( isset($this->params['pass'][0]) ) {
			$m = $this->params['pass'][0];
		if ( method_exists($this, $m) ) {
			$this->view_action = sprintf('/journal/%s', $m);
			return $this->$m();
		else {
			$this->set('error', 'page_not_found');