How I spent Saturday night

My wife keeps winning when we play scrabulous. So I wrote some performance-enhancing javascript here to close the gap.

The first time the page loads, you have to download the whole 1.4 mb word dictionary. Then your browser will cache that file locally, so future look-ups are really quick.

There’s at least one bug so far. When you submit your list of tiles like ABCDEFG and your pattern B..Y.. I make a regular expression object where I replace each dot with [ABCDEFG]. So that means I’m not keeping track of how many of each tile you have. If the BGGYGG were a real word, then my function would suggest it, even though this is clearly impossible.

Along with fixing that bug, I want to make a few improvements:

  • Figure out how to make this into a firefox extension.
  • Display the score next to each word suggestion.

This story offers a good excuse to use the blockquote tag.

A friend of mine, Y, is spending the week in the jury pool for Cleveland. Every day, he gets brought in with all the other potential jurors. They all answer questions and then the attorneys throw people out that they don’t like.

Yesterday’s trial involved a guy facing 31 counts of child rape — little girls. Y has two daughters, about the same age as the victims. The judge asked if anyone in the panel would have difficulty remaining objective in this trial.

Y replied that he could attempt to apply the letter of the law and follow due process, but he wouldn’t be able to deny his visceral reaction:

“I would be like somebody that graduated from medical school but faints at
the sight of blood.”

That got him put back in the pool for tomorrow’s trial.

Anyway, I thought that was a clever phrase.

Paul Graham has some good advice

His royal highness got around to addressing all the people saying nasty things about arc.

Sarcasm aside, I like this paragraph a lot, where he explains his design method:

Here it is: I like to find (a) simple solutions (b) to overlooked problems (c) that actually need to be solved, and (d) deliver them as informally as possible, (e) starting with a very crude version 1, then (f) iterating rapidly.

This articulates a bunch of fuzzy ideas I’ve been wrestling with myself.

Learning a new vim trick is more interesting than working on my Technology Investment Tax Credit application

The Ohio Technology Investment Tax Credit is a fantastic boon to starting businesses. But applying for it is really boring.

Anyway, when I opened up vim to work on my application, I found a really good tip of the day, copied below:

VimTip 295: Line/word/file/whatever completion

In addition to vimtip #291 you can use whole completion mode. It can complete whole lines (<C-x>l, then <C-p>, <C-n>), filenames (<C-f>), keywords, words from custom dictionary and many, many others. During coding it usually saves a LOT of key strokes 😉 This mode has many other powerful features, for example when completing word (by &;tlC-x><C-p> or just by <C-p>) you can continue completion with another <C-x<>-p>. For example, after writing such text:

this is first line
second line is here

Placing cursor at third line and pressing <C-x>l will double last line – <C-n>, <C-p> in this moment can be used to manipulate completed line. Or, instead of completing whole line you can press ‘f’ and then complete by <C-p> which will result in ‘first’ word. After that you can <C-x><C-p> to get ‘line’ word (since this is next word after ‘first’). Try yourself for other powerful combinations.

Now I just need a completion mode that will calculate intelligent sales forecasts, and this application would be done.

Keeping your system clock synchronized with Ubuntu is trivial

I have a few Ubuntu servers scattered across the earth. This is all I had to do to make sure that their system clocks were synchronized:

sudo apt-get install ntp ntpdate

Those Ubuntu packages are automatically configured to make your box use the time server at ntp.ubuntu.com. It couldn’t be any simpler.

By the way, if you want to point to a different time server, the config file in /etc/ntp.conf is full of helpful comments.

One plus-side of working in a startup

Some guy wrote this diary post at k5 complaining about stupid corporate-speak phrases like

Going forward, I’d like to touch base about getting more value-added metrics concerning the mission critical legacy informatics.

I used to hear meaningless gibberish like that all the time.

Now that I’m working for myself, I may have a lot of stress about other stuff, and way more work to do, but at least I don’t have to hear that kind of idiocy.

How to use itertools.cycle to set even and odd rows

I find code like this in a lot of web applications:

list_of_x_objects = ['a', 'b', 'c', 'd', 'e']
for i, x in enumerate(list_of_x_objects):
if i % 2: htmlclass = "odd"
else: htmlclass = "even"
print """

  • %s
  • """ % (htmlclass, x)

    Never mind the print statement. That’s just to illustrate the point without having to explain some template syntax.

    The same thing can be expressed with itertools.cycle:

    list_of_x_objects = ['a', 'b', 'c', 'd', 'e']
    for htmlclass, x in zip(itertools.cycle(['odd', 'even']), list_of_x_objects):
    print """

  • %s
  • """ % (htmlclass, x)

    I see several advantages of the second approach:

    • It’s way more flexible. I can easily switch to a style that repeats every three lines (or four, or five…).
    • I don’t create the variable i when all I really want is a class variable that toggles between values.
    • The second approach avoids the modulus operator. Since I hardly ever use the modulus operator, when I do come across it, I always have to take a second and puzzle out what’s happening.

    Notes from Cleveland Ruby meeting on Thursday, Jan 25th

    This post contains some python-related information, I promise.

    Fun time. Corey Haines explained behavior-driven development and showed some examples using RSpec at last night’s Cleveland Ruby meetup.

    As an aside, Corey said “powershell is what the unix command line will be when it grows up” and a thousand angels fell over dead when they heard this blasphemy.

    The story-based tests in RSpec seem downright magic. You can write in an english-y syntax:

    Given a = 1,
    When
    b.foo(a)
    Then
    b should return "Hurray"

    Or something like that.

    I like that RSpec supports a result called “Pending”. This guy writes a good explanation of how it works, and I agree with this remark:

    It’s easy enough to rename a test method so it doesn’t execute, but before RSpec I’ve never worked with one where you can mark it as pending and it then reminds you that you still have work to come back too.

    I figure that it would be straightforward to add this into nose. Maybe raise a special exception called PendingTest that gets caught differently.

    I learned a neat way of using a mock object without having to pass it in as a parameter based on some code I saw last night.

    Corey had a couponcontroller that operated on coupon objects. He made a mock coupon object to use with his tests for his couponcontroller. Then, in his test code, he monkeypatched the coupon module so that when somebody said “give me a coupon” he got a mock coupon instead.

    I spent a few minutes trying something vaguely like that in python. I’m not sure I like it, but it gets the point across.

    I have a file coupon.py:

    # This is coupon.py.

    class Coupon(object):
    "I'm the real coupon"

    def foo(self):
    print "This is the real coupon"
    return "foo"

    And I have a file couponcontroller.py:

    # This is couponcontroller.py.

    from coupon import Coupon

    def couponcontroller():
    c = Coupon()
    return c.foo()

    In my test_couponcontroller.py, I want the couponcontroller to use my mock coupon, not the real one.

    # This is test_couponcontroller.py.

    import couponcontroller

    class mockCoupon(object):
    "I'm not the real coupon."
    def foo(self):
    print "Congratulations. You're using a mock."
    return "foo"

    def setup():
    # Mess with the module.
    couponcontroller.Coupon = mockCoupon

    def test_couponcontroller():
    "couponcontroller should return a string 'foo'"
    assert couponcontroller.couponcontroller() == "foo"

    It seems to work:

    $ nosetests -s test_couponcontroller.py
    couponcontroller should return a string 'foo' ... Congratulations. You're using a mock.
    ok

    ----------------------------------------------------------------------
    Ran 1 test in 0.003s

    OK

    In summary, there’s clearly a lot of smart people in the ruby community, even if they insist on using syntax like

    @adder ||= Adder.new