
Apple have done it again, leapt ahead of the competition with the new iPad. Now I just have to find a business reason to justify buying one.
I want an iPad
Dr Seuss and Copenhagen
Thanks to Tin Harper who started it http://cientifica.eu/blog/2009/12/cop-15-as-reported-by-dr-seuss/ and Bruce Darrell who forwarded it!
[youtube=http://www.youtube.com/watch?v=3_RlKxz_ymQ]
Using Pyparsing to extract dates from text block

This is very much work in progress, but I thought I’d post in case it helps anyone.
Can you read this ok?
A good day for the terrsitros tadoy, wohse gaol atefr all is not to klil ppolee, but to sreapd trroer. The ppear and tv are flul of reotprs of the lteast inrasece in toerrr alret and all the mnay pisbosle rsaoens for it. And all the treisrort had to do was fial to klil a panle laod of peolpe and feed the mdeia. Are tehy vrey sarmt or are we vrey giulblle?
Stumbled across this blog and had to have a go myself http://blog.ggvic.com/2006/12/it-doesnt-matter-what-order-letters.html
Our brains are so smart that we only need the first and last letters in the correct order to be able to make a very accurate guess at the word.
For a bigger challenge, read Iain M Banks, Feersum Endjinn, much of which is written phonetically, for example:
Woak up. Got dresd. Had brekfast. Spoke wif Ergates thi ant who sed itz juss been wurk wurk wurk 4 u lately master Bascule, Y dont u ½ a holiday? & I agr.
Have a go jumbling your text:
Getting started with Pyparsing
Pyparsing is especially useful for those of us who don’t use regexp enough to get any good at it! It is a deceptively simple parser for all kinds of text.
Here are a few resources and tutorials I found useful.
- Introduction to Pyparsing – http://python.cofman.dk/pycon/2006/papers/1/
- Another short intro: http://rephrase.net/days/07/04/pyparsing
- The discussions : http://pyparsing.wikispaces.com/message/list/home
- Examples: http://teckla.idyll.org/~t/transfer/public/day2.pdf
And here is my first program. The requirement is to decode an instruction into two chunks. An opening “o” or “-”, followed by a mix of “?”, “!”, “&”,”^”.
from pyparsing import *def matching(list1, list2):
if len(list1) != len(list2):
return Falsefor key in list1.keys():
if (not list2.has_key(key)) or (list2.has_key(key) and list2[key] != list1[key]):
return False
return Truetests = [
('o?',{'status':'o', 'stype':'?'}),
('-!',{'status':'-', 'stype':'!'}),
('-!!!',{'status':'-', 'stype':'!'}),
('o?!',{'status':'o', 'stype':'?'}),
]def handleStuff(string, location, tokens):
print ‘string’,string
print ‘tokens’, tokens, tokens[0][0]
return tokens[0][0]status = Word(“-o”)
stype = Word(“!?&^”).setParseAction(handleStuff)search = ZeroOrMore(status(“status”))+ZeroOrMore(stype(“stype”))
for (test, result) in tests:
print ‘—————’
parsed = search.parseString(test,parseAll=True)
return_value = {‘status’:parsed.status, ‘stype’:parsed.stype}
print test, return_value, result, matching(return_value, result)
The bits I had to google a while for:
How to return named tokens.
If you did:
search = ZeroOrMore(status(“myvar”))
and this is recommended above using setResultsName I believe.
then to get the return value:
parsed = search.parseString(“My test string”)
print parsed.myvar
The other question was what does setParseAction return and the answer is an updated token. See example above which returns the first character in the first token.
Results of the program are:
—————
string o?
tokens ['?'] ?
o? {‘status’: ‘o’, ‘stype’: ‘?’} {‘status’: ‘o’, ‘stype’: ‘?’} True
—————
string -!
tokens ['!'] !
-! {‘status’: ‘-’, ‘stype’: ‘!’} {‘status’: ‘-’, ‘stype’: ‘!’} True
—————
string -!!!
tokens ['!!!'] !
-!!! {‘status’: ‘-’, ‘stype’: ‘!’} {‘status’: ‘-’, ‘stype’: ‘!’} True
—————
string o?!
tokens ['?!'] ?
o?! {‘status’: ‘o’, ‘stype’: ‘?’} {‘status’: ‘o’, ‘stype’: ‘?’} True