Author Archives: Matt Bone

zope at chipy

A few days back I have a little presentation about zope interfaces at ChiPy.  It was the first time I’ve presented at ChiPy except for two lightning talks (one on turtle and another on ittywiki).  Presenting was a lot of fun!

If you’re clamoring for zope interface knowledge, more details about the talk can be found on its github page.

simple s-expression parser in python

A few weeks back, I boasted in a bar that anyone could write a simple s-expression parser from scratch (no flex, no yacc, no bison, no antler, etc).  Well, I could not boast without doing it myself, so I finally did.  My s-expression lexer/parser weighs in at about 170 lines without tests (450 lines with them), and parsers integers and symbols.  I even created a little repl which takes this:

(+ 1 2)

And converts it into this:

[Symbol(+), Number(1), Number(2)]

A pretty-printed python list consisting of objects representing symbols and numbers.

I think the design is fairly readable if a bit unusual. There are classes for each token (instances of which are used for fully and partially formed tokens) and we “add” these tokens together to form a larger token and ultimately invoke a next_token_exception allowing the lexer to yield the largest, greediest, most fully-formed token possible.

Here’s an example of the lexer in action:

>>> symbol_token("h")
<parse_sexp.symbol_token object at 0x7fb9e2d253d0>
>>> _ + symbol_token("i")
<parse_sexp.symbol_token object at 0x7fb9e2d25710>
>>> formed = _
>>> formed + " "
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "parse_sexp.py", line 33, in __add__
    raise next_token_exception()
parse_sexp.next_token_exception
>>> formed.value
'hi'

After lexing, we’re left with a stream of fully-formed tokens; it’s a simple matter to filter out the whitespace tokens and start yielding instances of Symbol() and Number() classes with proper, pythonic types for the data.

Although I used regexes (quite unnecessarily) and generators, this little thing could probably be ported to C. It’d be the fastest, most useless parser/lexer around :)

Happy Hacking!

on the road

A month ago, I left Chicago on my bicycle, headed to Moline and then south to Saint Louis.  The journey spanned 7 days, 6 nights, and 589 miles.

I rode alone with dreams of solving my problems and divining my future in sunbaked introspection.  I carried all my gear (too much of it), camped 4 nights (hotel-camped 2 nights ;) ) and navigated my poorly planned route with two guide books and my G1.

Riding alone has its perks.  Plenty of time to think, and plenty of time to think about getting lost.  Fortunately my navigating skills seemed to improve after the second day (I nearly burst into tears that day after the GPS put my location in the middle of a lake, not, as I expected, between the Hennepin and I&M Canals).

Camping alone is different.  There are very few perks.  All my nights were fairly restless, and the one night I didn’t have to hiss at raccoons to shoo them away, a large storm came through at 2:00am.  My tent held out, though, and I can sleep when I’m dead.  But still, I’ve no shame now admitting to being afraid of the dark.

`

Being alone in the middle of Illinois is an odd thing.  It’s possible to ride for hours without seeing another human, but at no point are you truly in the middle of nowhere.  Everything has the imprint of humanity.  The road itself is a man-made scar through the land, but moreover, the land is far from natural.  Almost every inch is cultivated, pruned, or tilled for our use.  I suppose this is the trade-off  of a civilized society (possibly the definition of it), but it still makes me glad there are areas we have set aside for the exclusive non-use by humans.

In general the trip is somewhat hazy.  I probably rode too fast and did not stop at enough towns to really soak everything in.  But still I find myself thinking of very specific things about the ride, whether it be pushing my bike up a giant hill or resting for a bit on a bench.  I will do this again.  Someday I will find the time to ride farther west or maybe even ride the entire American Discovery Trail.

delaying computation: lazy dictionaries in Python

Yesterday I had a problem I ended up solving another way, but my first approach required a lazy dictionary.  That is, a dictionary filled with the results of some other function, but also one that only evaluated this computation when the dictionary was first accessed. Read more »

An American Childhood

About a month ago, I finished my second Annie Dillard book, An American Childhood.  Dillard has had a special place in my heart ever since I read Pilgrim at Tinker Creek in high school.  Tinker Creek is a Thoreau-like book of insanely detailed observation so powerful I find myself rereading every two or three years.  The language in Tinker Creek is so beautifully crafted that it reads, at times, like a prose poem, the art of which can, if care is not taken, distract from the actual content of the text.  So, it is with this high opinion of DIllard that I began to read An American Childhood.  I was not dissappointed.

On the surface, the book is a memoir of Dillard’s early years in Pittsburgh, from birth through adolescence, in the 1950s.  Dillard tells the story with the same skill as Pilgrim at Tinker Creek, and with the same attention to detail.  She bounces between topics, describing her father’s wide-eyed trip down the Mississippi one summer after quitting his job, and her mother’s wit and command of the spoken word.  The common thread in all of this is her childhood exuberance.  The young Dillard is not only intelligent but hungering for knowledge of any sort, reading field guides, linguistic texts, and drawing how-tos.  Reading up on her large rock collection, Dillard is surprised to discover adults with her own sense of wonder:

One book cautioned me against refining any gold I found…if I refined it in any way I was “obliged by law” to sell it to a licensed gold dealer…these, then, were books which advised, in detail, how to avoid making money, right here in America.

The descriptions of regular events like the family’s improving social status and country club sponsored cotillions, contrasts sharply with Dillard’s inner dialogue, the fervor of which grows and grows throughout the book, causing her to wonder if it will last:

Must I then lose the world forever, that I had so loved?  Was it all, the whole bright and various planet, where I had been so ardent about finding myself alive, only a passion peculiar to children, that I would outgrow even against my will?

An American Childhood has the marks of a typical memoir, but is made all the more enjoyable by Dillard’s mastery of language.  Furthermore, the young Dillard’s refusual to be blasé about anything large or small in the universe, is echoed by her modern author’s voice and the depth of self-revelation.  This is a book enjoyable from many angles, and though I’ve read only a small fraction of Dillard’s work, she is undoubtedly one of my favorite authors.

time for a bike tour!

I’ve got gears, panniers, a can opener, and, as of today, a hammock.  Time to pack up, head out on the bike, and do some stealth camping.  We’ll have to see how managing life, etc, goes, but I’d really like to do this soon…like this coming weekend.

(yes the hammock is not properly setup in this photo, but I was at a public park and people were staring at me like I was a crazy…and while I am a crazy, I started to become self conscious and packed up and left after a quick test of the hammock)

wordpress is the one I’ve been waiting for

It’s actually quite possible that this site has had more blogging/wiki/cms engines than readers.  I’ve decided to move away from the blogger (it’s just…kind of sucky), and start using wordpress (the “track bike” of blogging according to Dan).

So, yeah, I’ve been feeling an unnatural urge to write lately, so hopefully the content will pick up a bit.  And I promise to find a wordpress theme that doesn’t grate on the retinas (i.e. I won’t choose the colors).