Thursday, August 13, 2009

best blog ever

johnaugust.com

Has nothing to do with quantitative finance or technology--it's about screenwriting.  But it's very well written (no surprise) and, most strikingly, reading the comments doesn't reinforce my most cynical  humanity the way most blogs do



best blog ever

johnaugust.com

Has nothing to do with quantitative finance or technology--it's about screenwriting.  But it's very well written (no surprise) and, most strikingly, reading the comments doesn't reinforce my most cynical feelings for humanity the way most blogs do.



Thursday, August 6, 2009

Dear Steve,

MEMORANDUM--HIGHLY CONFIDENTIAL

To:  Steve Jobs

From:  bondgeek

Date: August 6th, 2009

re:  BlackBerry Curve Outsells iPhone 3GS -- Smartphone Sales -- InformationWeek

Dear Mr. Jobs,

It's about the email.  True push technology and straight forward synching with corporate email (so what if you're smarter than the Lords of the IT realm at all these major corporations--so is my coffee cup--they control their fiefdoms and you don't--WORK WITH THEM!!!).

Please let me know if you have any questions.





Tuesday, July 14, 2009

RegEx Tokenizer

The follow code snippet is adapted from Fredrik Lundh's effbot.org entry
Using Regular Expressions for Lexical Analysis

Say you want to tokenize an expression such as "(3+5)*10":
#!/usr/bin/env python
'''
Use regex to tokenize a string expression.
adapted from:
http://effbot.org/zone/xml-scanner.htm
'''
import re

reg_token = re.compile(r"""
\s* #skip whitespace
([0-9\.]+| #one or more digits or '.'
aka floats or ints
\w+| #words
[+\-*/!^%&|]{1,2}| #operators
.) #any character except newline
""",
re.VERBOSE)

def tokenize(expr):
'''
Returns a list of tokens for an expression string.
Allows operators +-*/!^%&|
Treats doubled operator e.g., **, ++ as single token
'''
def v_token(obj):
try:
if '.' in obj:
return float(obj)
else:
return int(obj)
except:
return obj

return [v_token(tkn.group()) for tkn
in reg_token.finditer(expr)]


Let's test on
some expressions

expr = ["(3+7)*90", # basic
"(3+7.1)*90", # has floats
"(3+7.1)*90*alpha", # has variables
"(3+7.1)*90*alpha, g", # invalid expression, tokenize and leave to parser
"(5.0 - 3.2)/6*9", # other forms
"b = 2 + a*10",
"x = \n x**2", #picks up **, ++ as a token
"i++",
""
]

for exp in expr:
tkns = tokenize(exp)
print("\nExpression: %s\nTokens: %s " % (exp, tkns))

Gives us...
Expression: (3+7)*90
Tokens: ['(', 3, '+', 7, ')', '*', 90]

Expression: (3+7.1)*90
Tokens: ['(', 3, '+', 7.0999999999999996, ')', '*', 90]

Expression: (3+7.1)*90*alpha
Tokens: ['(', 3, '+', 7.0999999999999996, ')', '*', 90, '*', 'a', 'l', 'p', 'h', 'a']

Expression: (3+7.1)*90*alpha, g
Tokens: ['(', 3, '+', 7.0999999999999996, ')', '*', 90, '*', 'a', 'l', 'p', 'h', 'a', ',', ' g']

Expression: (5.0 - 3.2)/6*9
Tokens: ['(', 5.0, ' -', 3.2000000000000002, ')', '/', 6, '*', 9]

Expression: b = 2 + a*10
Tokens: ['b', ' =', 2, ' +', ' a', '*', 10]

Expression: x =
x**2
Tokens: ['x', ' =', ' \n x', '**', 2]

Expression: i++
Tokens: ['i', '++']

Expression:
Tokens: []


code snippet is at dzone: http://snippets.dzone.com/user/bondgeek



Bare Minimum Vim

Because you just can't seem to avoid using it.

Bare Minimum Vim

v,V - visual mode, visual-line mode
(hit ESC to exit)
n__ - prefix a command with a number to repeat 'n' times
E.g., 5dd deletes 5 lines

Write & Quit = Save & Exit
! - generally causes a command to ignore changes--see :q!
:w - write the file without exit
:w [filename] - saves to new filename, stays in current buffer
:w! [filename] - overwrites if filename exists
:wq - write and quit
:q - quit (fails if there are unsaved changes)
:q! - quit and throw away changes

Navigation
NOTE: lower case treats punctuation as word break
[n]G - go to line "n", end of file if n omitted
e,E - jump ahead to end of word
b,B - jump back to beginning of word
w,W - jump ahead to start of word
0,$ - jump to start(0) or end($) of line

Edits
r - replace character under cursor
x - delete character under cursor (places in clipboard)
dd, dw - delete a line, word
J - Join line below to current line
. - dot repeats last change
u - undo last change
e! - discard changes to last write

Insert Mode
i,I - start insert mode at i=cursor, I=start of line
a,A - start insert mode after a=cursor, A=end of line
Esc - exit insert mode

Yank & Pull = Cut & Paste
yy,yw - yank a line, word
y$ - yank to end of line
p, P - put the clipboard after, before cursor

Find & Replace
/ptrn - search forward for pattern, wraps by default
?ptrn - search backward for pattern
n,N - repeat in same(n), opposite(N) direction
%s/o/n/gc, g
- global replace old with new with or without confirm


Splitting windows
Note: Ctrl+w, then the command
^w s - Split window horizontally
^w v - Split window vertically
^w w - switch between windows
^w q - Quit a window

Combo commands
ea - jump to end of word, then start insert mode after cursor
xp - delete then paste (transposes characters)
:wq - write then quit

Sightly beyond bare minimum
:e - re-edit current file--e.g, if it has changed on disk
:e [file] - edit [file] in new buffer
ZZ = :wq
ZQ = :q!
d[move] - delete in direction of arrow movement
D - delete to end of line
R - Replace mode, instead of insert
~ - Switch case of character under cursor
:r [file] - read file, inserting under cursor
h,j,k,l - Navigate right, down, up, left
()[]{} - Navigate by sentence, paragraph, section
E.g. '(' moves 1 sentence backwards, ')' moves forward

Some other useful references
http://www.worldtimzone.com/res/vi.html
http://www.fprintf.net/vimCheatSheet.html
http://jmcpherson.org/editing.html
-- Good place to start if you want to know more, written by somebody who appreciates vim.

Thursday, May 28, 2009

switch statements in python

I may as well weight in on the absence of a switch statement in Python, though the topic has been addressed, see:

Python Zone » Python switch statement, or
http://dinomite.net/2008/python-switch-statements-part-2

Here are some examples of switching structures using dictionaries:

# switch example 1
cases = {
'a':
    lambda: 'one',
'b':
    lambda: 'two',
'default':
    lambda: 'three'
}

switch = lambda c: cases.get(c, cases['default'])()

var = ['b','xxx']
for v in var:
    out = switch(v)
    print("Switch on %s = %s"%(v,out))


# switch example 2
op = raw_input("Enter operation for 2 'op' 3: ")

if op in "+-*/":
    print("2 %s 3 = " % op)
else:
    print("'%s' is an unkown operation." % op)

cases = {
     '+': lambda : 2 + 3,
     '-': lambda : 2 - 3,
     '*': lambda : 2 * 3,
     '/': lambda : 2. / 3.
     }

switch = cases.get(op, lambda : 0)()

print(switch)


# switch example 3
name = raw_input("What is your name? ")
op = raw_input("enter 'l' or 'p'")

cases = {
    'l': len,
    'p': lambda txt: txt.upper()
    }

def default(obj):
    print("Invalid entry, %s" % obj)

switch = cases.get(op, default)
out = switch(name)

print(out)
I would say that each of these is as readable as C's switch statement.  I can't say anything for performance, since I haven't run any tests--but Python's dictionaries are fast.   In fact, if I understand the implementation correctly this pattern is pretty much what the switch statement does--set up a hash table (i.e. a dictionary) of test values and choose a code block based on the condition entered.  

The main argument would be the cumbersomeness of switch on larger blocks of code.  To do that you need to move each block of switch code into a function defined by def.   My only response to that is that if you're writing large blocks of code inside a switch statement, you should think about putting it in a function--in my experience there is usually a fair amount of reused code inside a large switch statement, since the conditions usually each deal with a separate state of a single variable.  Also, the difference in readability between switch and nested if...then..else's diminishes rapidly the larger the conditional code blocks get.

So, should Python have a switch statement?  Readability of code long term may benefit if there is one way to do a switch as opposed to using different, although similar patterns like above.  But in general, I support keeping control flow logic made of the simplest building blocks--in the long run I think that keeps bloat down and enforces more thinking about the nature of the specific programming problem at hand.

But if a switch statement was added to Python, I'd probably use it.





Wednesday, May 13, 2009

Speaking of timing

Using list comprehension is much faster than not:

In [35]: ll = [(x,x*x) for x in range(100)]

In [36]: def f1(obj):
....: for row in obj:
....: x = row[0]
....: y = row[1]
....:

In [37]: def f2(obj):
....: for row in obj:
....: x,y = row
....:

In [38]: def f3(obj):
....: for row in obj:
....: x,y = (row[0],row[1])
....:

In [39]: timing(f1,10000,ll)
f1 2.04

In [40]: timing(f2,10000,ll)
f2 0.8

In [41]: timing(f3,10000,ll)
f3 2.41
Function calls always add a bit of overhead:

def f1(obj):
x = min(obj,0.0)

def f3(obj):
if obj <= 0.0:
x = obj
else:
x = 0.0

In [56]: timing(f1,10000,5.)
f1 0.06

In [57]: timing(f1,10000,-5.)
f1 0.05

In [67]: timing(f3,10000,5.)
f3 0.04

In [68]: timing(f3,10000,-5.)
f3 0.04


Useful to know.