Revision [1556]

Last edited on 2010-08-27 10:03:00 by KlenwellAdmin
Additions:
python nltk unit test
#!/usr/bin/python
"""
http://nltk.googlecode.com/svn/trunk/doc/book/ch05.html
"""
import unittest
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)


Revision [1544]

Edited on 2010-08-11 12:15:55 by KlenwellAdmin
Additions:
[[http://freakonomics.blogs.nytimes.com/2010/08/09/what-do-you-want-to-hear-from-nassim-nicholas-taleb/comment-page-1/?apage=3#comments freakonomics blog NNT]]


Revision [1533]

Edited on 2010-08-05 12:20:26 by KlenwellAdmin
Additions:
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'


Revision [1503]

Edited on 2010-07-13 12:19:02 by KlenwellAdmin
Additions:
bash find base: %%BASE=$(dirname $(readlink -fn $0))/..%%


Revision [1500]

Edited on 2010-07-12 15:18:06 by KlenwellAdmin
Additions:
parchment style: http://rlv.zcache.com/shakespeare_quotes_calendar-p1580846210289528122vxoc_400.jpg


Revision [1446]

Edited on 2010-06-28 14:02:02 by KlenwellAdmin
Additions:
[[http://groups.google.com/group/google-appengine-python/browse_thread/thread/1210c05bb37cb1be# app engine cron question]]


Revision [1429]

Edited on 2010-06-03 14:12:46 by KlenwellAdmin
Additions:
""
nltk example
""http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html#a-pronouncing-dictionary %%(python)
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):
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)
""%%
Deletions:
""

python buffer capture (assign stdout to a variable)

""


Revision [1411]

Edited on 2010-05-21 16:07:29 by KlenwellAdmin
Additions:
""

python buffer capture (assign stdout to a variable)

""
Deletions:
""



Revision [1410]

Edited on 2010-05-21 16:07:13 by KlenwellAdmin
Additions:
""

Deletions:
python buffer capture (assign stdout to a variable):


Revision [1409]

Edited on 2010-05-21 16:06:31 by KlenwellAdmin
Additions:
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')


Revision [1408]

Edited on 2010-05-21 11:09:43 by KlenwellAdmin
Additions:
python string diff:
from difflib import Differ
d = Differ()
print '\n'.join(list(d.compare(app_output, expect)))


Revision [1342]

Edited on 2010-05-02 20:16:14 by KlenwellAdmin
Additions:
google group question: [[http://groups.google.com/group/google-appengine-python/browse_thread/thread/ca1b6838bc68d64a/5be4288579bc5a77?q=klenwell#5be4288579bc5a77 serve atom through appengine]]


Revision [1341]

Edited on 2010-05-02 11:06:39 by KlenwellAdmin
Additions:
python:
import re
def re_ungreedy(re_, s):
r = re_.replace('%s', '(.*?)')
return re.search(r, s).groups()
re_ = "

%s

"
H2List = re_ungreedy(re_, '

one

two

')


Revision [1325]

Edited on 2010-04-26 10:32:24 by KlenwellAdmin
Additions:
python debugger: ""import pdb; pdb.set_trace()""


Revision [1303]

Edited on 2010-04-16 11:47:59 by KlenwellAdmin
Additions:
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);


Revision [1188]

Edited on 2010-03-30 14:48:49 by KlenwellAdmin
Additions:
PEAR HTTP_Request2 [[http://pear.php.net/manual/en/package.http.http-request2.intro.php docs]] [[http://pear.php.net/package/HTTP_Request2/docs/latest/HTTP_Request2/HTTP_Request2.html api]]


Revision [1187]

Edited on 2010-03-30 14:46:55 by KlenwellAdmin
Additions:
/*
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('

test: %s

%s
', $test, $output);


Revision [1163]

Edited on 2010-03-24 21:52:18 by KlenwellAdmin
Additions:
copy with exclude: ""rsync -rv --exclude=.svn source/* dest""


Revision [1162]

Edited on 2010-03-24 12:31:08 by KlenwellAdmin
Additions:
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 ));


Revision [1161]

The oldest known version of this page was created on 2010-03-23 14:51:55 by KlenwellAdmin
Valid XHTML 1.0 TransitionalValid CSSWikkaWiki