online cocktail napkin
links and notes for later organization2009 Napkin
2008 Napkin
2007 Napkin
WordsIReallyLike
ThumbDrive
Questions for Strangers
python nltk unit test
#!/usr/bin/python
"""
http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html
"""
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, '..'))
sys.path.append(find_base())
class TestTagging_5_1(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
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)
unittest.TextTestRunner(verbosity=2).run(suite)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
fi
paster serve development.ini &
PID=$!
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
fi
PID=`cat $PID_FILE`
kill $PID
rm $PID_FILE
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
http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html#a-pronouncing-dictionaryimport 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):
m.append(meter(pd.get(w)[0]))
return ''.join(m)
sent = 'A rose is a rose is a rose is a rose'
print scan(sent)
#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):
m.append(meter(pd.get(w)[0]))
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 self.new_buffer.close() return buffer_content def testBuffer(self): self.openBuffer() 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
python:
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
<?php
/*
HTTP_Request2 Demo
Lib File Tree
kw_pear\
HTTP\
Request2\
Request2.php
Net\
URL2.php
PEAR\
Exception.php
PEAR.php
*/
$wwwd = dirname(dirname(__FILE__));
$peard = sprintf('%s/%s', $wwwd, 'kw_pear');
$path_ext = implode(PATH_SEPARATOR, array(get_include_path(),
$peard));
# 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();
$Client->setMethod(HTTP_Request2::METHOD_GET);
$Client->setUrl($url);
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',
$Response->getStatus(),
$Response->getReasonPhrase());
}
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,
$table_name,
$page_name,
$page_body,
'(Public)',
$user,
$note )); To call a script, you can make it as handler and put it at (as of 1.1.6.2) 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"
else
log "no files found in /tmp"
fiOffice 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
ycombo
fly bomber (it's supafly)
classic olympic fail
gdata python question on google groups
curious
copying DB in php
function _switch_database($new_db) {
$Dbo = ConnectionManager::getDataSource("default");
$old_db = $Dbo->config['database'];
$Dbo->reconnect(array('database'=>$new_db));
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);
$Dbo->query($sql);
# 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);
$Dbo->query($sql1);
$Dbo->query($sql2);
}
# 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;
}
}
$Dbo = ConnectionManager::getDataSource("default");
$old_db = $Dbo->config['database'];
$Dbo->reconnect(array('database'=>$new_db));
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);
$Dbo->query($sql);
# 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);
$Dbo->query($sql1);
$Dbo->query($sql2);
}
# 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
<?php
require('utf8.inc');
$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\')' =>
$utf8,
'htmlspecialchars(\'Ü\',ENT_QUOTES,\'UTF-8\')' =>
htmlspecialchars('Ü', ENT_QUOTES, 'UTF-8'),
'$h_utf8 - htmlentities($utf8)' =>
$h_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(\'&#x%s;\', dechex(ord($char)))' =>
sprintf('&#x%s;', dechex(ord($char))),
'sprintf(\'&#x%s;\', ord($char))' =>
sprintf('&#x%s;', ord($char)),
'sprintf(\'&#%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)' =>
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">
<head><title>sandbox</title></head>
<body>
<pre>
%s
</pre>
</body>
</html>
XHTML;
$charset = 'UTF-8'; #'ISO-8859-1';
header( 'Content-type: text/html; charset='.$charset );
printf($xhtml, print_r($data,1));
var_dump($char);
var_dump($h_char);
var_dump($utf8);
var_dump($h_utf8);
require('utf8.inc');
$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\')' =>
$utf8,
'htmlspecialchars(\'Ü\',ENT_QUOTES,\'UTF-8\')' =>
htmlspecialchars('Ü', ENT_QUOTES, 'UTF-8'),
'$h_utf8 - htmlentities($utf8)' =>
$h_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(\'&#x%s;\', dechex(ord($char)))' =>
sprintf('&#x%s;', dechex(ord($char))),
'sprintf(\'&#x%s;\', ord($char))' =>
sprintf('&#x%s;', ord($char)),
'sprintf(\'&#%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)' =>
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">
<head><title>sandbox</title></head>
<body>
<pre>
%s
</pre>
</body>
</html>
XHTML;
$charset = 'UTF-8'; #'ISO-8859-1';
header( 'Content-type: text/html; charset='.$charset );
printf($xhtml, print_r($data,1));
var_dump($char);
var_dump($h_char);
var_dump($utf8);
var_dump($h_utf8);
jquery quiz
== jQuiz: jQuirks ==
A short quiz on jQuery behavior.
===1. What will these lines of code produce?===
<code><pre>
var Test1El1 = $('div#Test1El1');
var Test1El2 = $('div').attr('id', 'Test1El2');
var Test1El3 = $('<div id="Test1El3" />');
console.log(Test1El1);
console.log(Test1El2);
console.log(Test1El3);
</pre></code>
===2. What will be the output of the code below?===
<code><pre>
var Test2El1 = $('<div id="Test2El1" />');
$(document.body).append(Test2El1);
var elid = Test2El1Test2El1.attr('id');
var Test2El2 = $(elid);
console.log(Test2El1);
console.log(elid);
console.log(Test2El2);
</pre></code>
===3. True or False===
<code><pre>
var id_ = 'test3id';
$('<div id="' + id_ + '" />').appendTo(document.body);
var Test3El1 = document.getElementById(id_);
var Test3El2 = $('#'+id_);
console.log(Test3El1.id == Test3El2.id);
</pre></code>
===4. True or False===
<code><pre>
var Test4El1 = $('<div id="Test4El" />').appendTo(document.body);
var Test4El2 = $(Test4El1);
console.log(Test4El1 == Test4El2);
console.log(Test4El1.attr('id') == Test4El2.attr('id'));
</pre></code>
===Given a document with the following table code, answer the next 2 questions:===
<code><pre>
<table id="Test5Table">
<tr id="Test5Tr1"><td>cell1</td><td>cell2</td></tr>
<tr id="Test5Tr2"><td>cell1</td><td>cell2</td></tr>
</table>
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>');
$(document.body).append(Test5Table);
</pre></code>
====5. What will the value of TableRows[0] be below?====
<code><pre>
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');
console.log(TableRows[0].attr('id'));
</pre></code>
====6. What will the value of RowIds[0] be below?====
<code><pre>
var Test5Table = $('table#Test5Table');
var TableRows = Test5Table.find('tr');
var RowIds = [];
TableRows.each( function(i, El) {
RowIds.push(El.attr('id'));
});
console.log(RowIds[0]);
</pre></code>
===7. What will the value of FindEl be below?===
<code><pre>
// build and add element to our document
var elid = 'formbid-0.field';
var NewDiv = $('<div id="'+elid+'" />').text(elid);
$(document.body).append(NewDiv);
var jqid = NewDiv.attr('id');
var FindEl = $(document.body).find('#'+jqid); // note # sign
console.log(jqid, FindEl);
</pre></code>
<div style="display:none;">Answer:
<code><pre>
jesc = jqid.replace('.', '\\.');
var FindEl = $(document.body).find('#'+jesc);
console.log(jesc, FindEl);
</pre></code>
</div>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];
}
#debug($this->params);
#debug($m);
if ( method_exists($this, $m) ) {
$this->view_action = sprintf('/journal/%s', $m);
return $this->$m();
}
else {
$this->set('error', 'page_not_found');
}
}CategorySpecial
[There are no comments on this page]