Three Steps to Make GNOME3 Livable

Two places to make tweaks, then a whole bunch of extensions to install.

In gconf (use gconf-editor):

key path value
/apps/mutter/general/overlay_key Menu
/apps/mutter/general/workspaces_only_on_primary false
/desktop/gnome/shell/windows/button_layout close:
key path value

In dconf (use dconf-editor):

key path value
org.gnome.shell.calendar.show-weekdate true
org.gnome.shell.clock.show-date true
org.gnome.desktop.interface.font-name Sans 11
org.gnome.desktop.interface.gtk-key-theme Emacs
org.gnome.desktop.interface.automatic-mnemonics false
key path value

Extensions (install from GNOME Shell Extensions):

timing-convenience, a Haskell package

I’ve been working on packages to make it easier to write in my favorite scripting language, Haskell. The first to make it on Hackage is timing-convenience.

Install as usual:

cabal install timing-convenience

Then you can get the time three days ago:

import Data.Time
import Data.Time.Convenience

main = timeFor 3 Days Ago >>= putStrLn . show

(See why I call it a scripting language? I’d like to see Perl be that obtuse!)

I used this in my script that generates my This Week in Open Source series of blog posts on the thoughtbot blog. I also made use of my github package, which is still under development.

Dynamic Class Methods in Ruby

Rails has a whole slew of class methods that really should be instance methods. The most notable of these methods is ActiveModel::Naming‘s model_name, which is a class method that is expected on every ActiveModel object (for example, it is expected by form_for). This code uses that as an example, but the pattern applies anywhere.

If you want to make an ActiveModel factory, which is a boring and classic design pattern, then you need to deal with the fact that the model_name must be both a class method and different for each instance of your factory. That is:

a = ActiveModelFactory.new("hello")
b = ActiveModelFactory.new("goodbye")
a.class.model_name.should == "hello"
b.class.model_name.should == "goodbye"
a.class.model_name.should == "hello"

Here’s how that looks:

class ActiveModelFactory
  def initialize(model_name)
    @model_name = model_name
  end

  def class_with_model_name
    self.class_without_model_name.tap do |c|
      c.instance_variable_set('@_model_name', @model_name)
      (class << c; self; end).send(:define_method,:model_name) do
        value = self.instance_variable_get('@_model_name')
        model_namer = Struct.new(:name).new(value)
        ActiveModel::Name.new(model_namer)
      end
    end
  end
  alias class_without_model_name class
  alias class class_with_model_name
end

Going through this: the initializer takes the model name and tucks it away in an instance variable, where it belongs. That part is straight-forward.

To make the model_name class method work we redefine the class to be different for each instance. It goes like this:

The class_with_model_name method produces the instance’s class, but first it modifies it. It sets an instance variable on the class itself, @_model_name, which is the same as the instance’s @model_name value. Then it defines a method named model_name on the class. Inside model_name it gets the value of this @_model_name instance variable, which has traveled from the factory instance through the class into an eigenclass and into a define_method block. Once it has this value it builds the struct and produces the ActiveModel::Name object as needed.

To tie this all together we use the alias_method_chain trick, which is used when someone screwed up. Alias class to class_without_model_name, then our class_with_model_name to class.

Elsewhere by me: Unobtrusive Ruby doesn’t make people jump through these hoops. This research was done as part of my liaison gem, which is an ActiveModel factory.

Check your email into git using procmail

In this blog post you will see how I commit my email into git one message at a time, as it is delivered, using procmail.

Screenshot of the git log, showing the emails committed with their sender and subject

This is a simple three-step process:

Write a program to commit an email to git.

This is the shell script I use; I named it ~/.bin/commit-an-email . It constructs a commit message with the sender and subject, adds the email to the repo, and commits using that message:

#!/bin/sh

MAILDIR=$HOME/Maildir

commit_message=`grep '^From: ' $LASTFOLDER; grep '^Subject: ' $LASTFOLDER`

cd $MAILDIR && \
git add `echo $LASTFOLDER | sed -e 's|^/home/mike/Maildir/||'` && \
git commit -m "$commit_message"

Set a procmail TRAP.

The TRAP setting points to a shell command which is executed after procmail has finished processing an email; it passes a copy of the email to stdin. Set this to the commit-an-email script in ~/.procmailrc :

TRAP=/usr/home/mike/.bin/commit-an-email

Clean up deletions and moves, and push.

After the email is delivered you may read it, move it to another mailbox, or delete it. It’d be great if these actions were tracked as well. One option is to write a series of mutt macros that redefine s, d, enter, and so on, to do the appropriate git command. Alternatively, you could handle this nightly with this script, meant to be run from cron. I named it ~/.bin/commit-emails:

#!/bin/sh

GIT=/usr/local/bin/git
MAILDIR=$HOME/Maildir

cd $MAILDIR && \
$GIT add . && \
$GIT add -u && \
$GIT commit -m "Nightly maintainance" && \
$GIT push origin master >/dev/null

And this is my crontab :

5 1 * * * /usr/home/mike/.bin/commit-email

One big mutt inbox, filtered after-the-fact using procmail

This article shows how to configure mutt and procmail to drop all emails into one big inbox which is then filtered, after you’ve read the email, into a distinct mailbox. For example, emails from your friends, girlfriend(s), sysadmin, and bank all show up in your inbox; after reading each they are sent to =friends/, =romances/, =nightly/, and =finances/, respectively, automatically.

Rationale: each morning I read every email available to me, in order of a loose priority, no matter which mailbox it was in. As an email came in I’d immediately read it. It made little sense to open a new mailbox just to read yet another email; the mailboxes are more useful for saving the email to reference later.

In this setup you now need two procmailrcs: one that delivers stright to your inbox (you could probably get away without procmail for this role, even; I use procmail to handle the Maildir format and also to commit each email to git after delivering it), and one for the final filtering. I named these ~/.procmailrc and ~/.procmail/finalrc .

The ~/.procmailrc looks roughly like this:

DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir/
:0:
$MAILDIR

(Mine also has a TRAP, as touched on above re git.)

Boring. It could probably be one line, even.

The other procmailrc, ~/.procmail/finalrc , is your real procmail rules. If you have an existing ~/.procmailrc, move it to this new place. Here’s an interesting snippet from mine:

DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir/
LOGFILE=/dev/null

# Friends
:0:
* ? formail -x"From" -x"From:" -x"Sender:" \
    -x"Reply-To:" -x"Return-Path:" -x"To:" \
        | fgrep -is -f ~/.procmail/friends.list                                 
friends/

# Android
:0:
*^TO_droids@lists.bostonandroid.org                                             
droids/

# Catch-all
:0:
$MAILDIR

But great; so far emails are landing in your inbox and just staying there. Next you add this to your ~/.muttrc :

macro index y '<enter-command>unset wait_key<enter><tag-prefix><pipe-entry>/usr/local/bin/procmail /usr/home/mike/.procmail/finalrc<enter><tag-prefix><delete-message><enter-command>set wait_key<enter>'
macro pager y '<enter-command>unset wait_key<enter><pipe-entry>/usr/local/bin/procmail /usr/home/mike/.procmail/finalrc<enter><delete-message><enter-command>set wait_key<enter>'

When you’re done reading an email press y . This will pipe that email through the ~/.procmail/finalrc and delete it from your inbox, moving onto the next email.

DatePreference, an Android library

Use this library if you want a DatePreference in your Android app.

This is available as source code, a JAR, and a dex file.

Screenshot showing the date preference for selecting the date of birth

Use it just like any other preference in your PreferenceScreen XML:

<org.bostonandroid.datepreference.DatePreference
      android:key="dob" android:title="@string/dob"
      android:defaultValue="1991.01.01" />

There are also convenience methods for getting the selected value, setting the value, and so on. Full API documentation is available.

DatePreference was written during Boston Android hackfests by Mike Burns with help from other Boston Android developers. Feedback welcome!

Mentors Wanted

I’m something of an experienced Web developer. Ten years or so in Web programming in Perl, Scheme, Ruby, and dabbling in Haskell and Python. I’ve worked with MySQL, PostgreSQL, and MongoDB; CSS, XHTML, and XML; MVC, client-server, and state-based.

Lately I have ideas that are bigger than what I know; these ideas are outside the Web. And I need knowledge help.

I have three projects that I would like a mentor for. These projects touch on niche technologies where I’d like to leave a mark and fully understand the implementations. If you are local (Bostonish) and available we should talk.

The projects need mentors for these concepts:

  • Google Android testing. Experience with integration testing and unit testing all aspects of Android development, including various kinds of Activities, Services, Preferences, and so on. This you and you’re local? Let me know or come find me at the next Boston Android hackfest.
  • GNOME or GTK development. Experience with writing a GTK app, using Glade, in either C, Python, or Haskell. Normal GNOME apps are most interesting, but I am also interested in writing panel applets. This you and you’re local? Let me know.
  • BitTorrent client and server. My most ambitious project to date involves applying BitTorrent to concepts it was not made for, but I need help thinking through and working with the protocol itself, and programming the client and server as needed. This you and you’re local? Let me know.

Boston Android

I am co-running the local Boston Android programming group these days. Just so you know.

Come hack code on Monday, August 23rd at the thoughtbot office.

What I want out of a Web framework

Haskell has a serious Not Invented Here problem where it comes to Web frameworks. Everyone is unhappy with what we have, but no honest features differ between the six or so out there.

Let’s start with what I don’t care about:

  • Speed
  • Computer Science

If I cared about these things—or if any other Web developer cared about these things—I wouldn’t write in Ruby (or PHP, Perl, PLT Scheme, etc.).

These are nice but they’re not selling points.

Overall what I—and, Rails has taughts us, other Web developers—want out of a Web framework is a strong opinion. I want to be told where to put things. I want to be told that routes go here, HTML templates go there, tests go in this other place, and so on.

To not do that is to assume that Web apps are simple, that people maintain exactly one Web app, and that Web sites are completely unique. None of those assumptions are true.

Next up I want to bring knowledge over from the rest of my career. Some of my favorite design patterns apply here: observer and MVC. Just use MVC for your Web framework. It makes sense. I mean, it doesn’t, but it approximates it so well that it’s worth it.

The view should look like HTML. I’m not writing it, afterall; the designer is. Or she and I are writing it together. Either way, it shouldn’t be validated. I don’t care if it’s valid; the designer doesn’t care; the Web browser doesn’t care (and sometimes needs it to be invalid); so just relax already. (Any Haskell in the view template needs to be valid. I understand this.)

And I don’t want to write SQL so use an abstraction layer like the active record pattern or Arel or something dead easy. Simulate OO if you need to; it makes sense some times, and invoking behavior on data is that time.

I don’t want to “pretend I’m writing desktop apps” because this isn’t a familar metaphor. I want to write HTTP apps. I want to interact with GET, PUT, POST, and the others. The Web is a very familiar metaphor: data goes in, data comes out, and it’s over. It’s like a function call but with more moving parts. Not only does this make it easy to think about, it makes it easy to test.

I test my code. At work I test-drive my code, which also implies 100% code coverage but we don’t tend to care about code coverage. But that’s the kind of code I write: code that needs to be tested. I want to use an integration test suite to describe how the application should perform, and I want to use a unit-level test suite to ensure that refactoring goes well. My Web app will talk to other Web apps so I damn well better be able to fake that part out of my tests.

Pulling knowledge in from the rest of my career, I want to use Cucumber and something better than Selenium. In Rails we use Webrat and Holy Grail and EnvJS and ShamRack and a whole slew of other tools. There’s nothing stopping me from using Cucumber to describe my behavior except I can’t back it with a Haskell equivalent of Webrat.

Oh speaking of knowledge, I need documentation. The first-pass tutorial for a Web app isn’t “hello, world”, it’s a blog. If “hello, world” requires a full tutorial then your Web framework belongs in 1995. Make a screencast, those are fun. You should say, “Look at all these things I’m not doing!” in it, and mean it. But be aware that I’m not writing a blog. Well, I am, but it’s part of the app. Clients always demand a blog (and an admin interface, statistics, copy changes, A/B testing, …).

I see an idea crossing your mind right now and let me just say that splitting the individual components into discrete divisions that know nothing of each other simply isn’t fun to maintain. And even if it were fun to maintain, you don’t know how to make such a thing in the first version of your framework.

Speaking of things you don’t know how to do at first, I want hooks. Nearly every Web site out there is slightly different from another Web site, and I want to hook into the framework to handle those differences. The part of the framework that catches exceptions and shows a 500? I want to be able to replace that or augment it. The part that maps URL paths to module and function; the part that looks for the location of the HTML templates; the part that loads configuration files—all this, and more, are things I may want to augment.

If you provide a hooks, you should have a directory for storing plugins. That’s a nice, cheap way of having people contribute to your Web framework.

I mentioned errors, and this is one spot where Haskell, as a language, falls short. Here’s what you can get from a Ruby exception: backtrace, message, class name, and anything defined on Object, Class, Module, or Kernel. On the other hand, here’s what you can get from a Haskell exception: message. So you know that somewhere, somehow, maybe in your code, `head’ got an empty list.

(I ran into this while writing a Hoptoad notification library for Haskell. It was a very “wait … what! … waaait. Oh, what!” reaction when I realized this.)

Rumor has it that ghc is working on backtraces.

As a tangent let me tell you about the first (and last) time I tried Merb. I had been a Rails developer for maybe three years by then and I had heard that Merb was a simpler Rails. “That sounds nice,” I thought to myself. “Rails is a big pile of hacks so presumably they’ve brought that pile down to a manageable level.” First thing I noticed was that ActiveSupport was missing. This meant I couldn’t #pluralize. As I wrote more I realized more and more was missing and that, really, Rails was extracted from a real, production-level, money-making Web app … and Merb simply wasn’t.

Anyway, when the Web app is ready I’m going to deploy it to staging and, when that’s ready, it’s off to production. Maybe there will be a pre-staging server, too, or a mirror of staging, or a mirror of production with forked traffic. Either way deployment needs to happen and, when it breaks, it needs to be undone.

So it sounds like Rails has what I want. Why am I still here? Haskell gives me: type checking, a friendly and small community, and threading. It’s a smart language for smart people, and I want to pretend to be a smart person.

The next step

Which Haskell Web framework is the one we should bring into this dream? There’s no need to make yet another; let’s just take an existing one and fully realize it. I can’t figure out which, though. Advice?

An Infinite List of Fibonacci Numbers in Ruby

So I was reading through the Haskell Prelude when I stumbled across `scanl' as a kind of abstraction over `foldl'. I stared, and thought, and stared some more, and couldn’t come up with a use for it; a quick Web search revealed exactly one use: Fibonacci numbers.

(In retrospect: duh.)

So here are all the Fibonacci numbers in Haskell:

fibs = 0 : scanl (+) 1 fibs

As you can see this makes use of infinite, recursive lists. So I wrote this in Ruby.

First I needed infinite lists:

class LazyEmpty
  def empty?
    true
  end
end

class LazyCons
  attr_reader :first

  def initialize(first, &rest)
    @first = first
    @rest = rest
    @rest_called = false
  end

  def rest
    if @rest_called
      @rest
    else
      @rest_called = true
      @rest = @rest.call
    end
  end

  def empty?
    false
  end
end

And now that I have such a thing, I need behavior on it. For example, #take will produce the first n elements of such a list:

class LazyEmpty
  def take(n)
    LazyEmpty.new
  end
end
class LazyCons
  def take(n)
    if n < 0
      self
    elsif n.zero?
      [self.first]
    else
      self.rest.take(n-1) + [self.first]
    end
  end
end

So now I can get at the elements:

irb(main):001:0> l = LazyCons.new(1) { LazyCons.new(2) { LazyCons.new(3) { LazyCons.new(4) { LazyEmpty.new }}}}
=> #<LazyCons:0x7f656ee21208 @rest_called=false, @rest=#<Proc:0x00007f656ee212a8@(irb):2>, @first=1>
irb(main):002:0> l.take(2)
=> [3, 2, 1]

To get the most fun definition for the list of Fibonacci numbers I need #scanl:

class LazyEmpty
  def scanl(q, &f)
    LazyCons.new(q) { LazyEmpty.new }
  end
end

class LazyCons
  def scanl(q, &f)
    LazyCons.new(q) { self.rest.scanl(f.call(q, self.first), &f) }
  end
end

So now I have all the parts needed:

def fibs
  LazyCons.new(0) { fibs.scanl(1) {|x,y| x + y} }
end

See?

irb(main):008:0> fibs.take(10)
=> [55, 34, 21, 13, 8, 5, 3, 2, 1, 1, 0]

That is to say, the nth Fibonacci number is the first element from take(n).

def fib(n)
  fibs.take(n).first
end

At this point I wondered how it performed, especially against the naïve implementation. So I used Benchmark.rb and ruby-prof to compare #fib_naive, #fib_tail (the tail-call rewrite of the naïve implementation), #fib_while (the while loop rewrite of the tail-call rewrite), and #fib_inf_list (as defined above) … and the infinite list was not in last place!

       user       system      total        real
naive 12.020000   2.140000  14.160000 ( 14.174045)
tail   0.000000   0.000000   0.000000 (  0.010399)
while  0.020000   0.000000   0.020000 (  0.008730)
inf    3.360000   0.150000   3.510000 (  3.513216)

naive memory:
Thread ID: 70118283774380
Total: 970.630000

 %self     total     self     wait    child    calls  name
  5.11    970.63    49.61     0.00   921.02 17710000  Object#fib_naive-1 (fib_benchmarks.rb:6}
  1.01      9.79     9.79     0.00     0.00 17710000  Fixnum#- (ruby_runtime:0}
  0.94      9.16     9.16     0.00     0.00 17710500  Fixnum#< (ruby_runtime:0}
  0.53      5.17     5.17     0.00     0.00  8855000  Fixnum#+ (ruby_runtime:0}
  0.00     73.73     0.00     0.00    73.73        1  Integer#times (ruby_runtime:0}
  0.00     73.73     0.00     0.00    73.73      500  Proc#call (ruby_runtime:0}
  0.00     73.73     0.00     0.00    73.73      500  Object#fib_naive (fib_benchmarks.rb:6}
  0.00     73.73     0.00     0.00    73.73        1  Object#profile (fib_benchmarks.rb:48}


tail memory:
Thread ID: 70118283774380
Total: 0.630000

 %self     total     self     wait    child    calls  name
  6.35      0.63     0.04     0.00     0.59    10500  Object#fib_tail_aux-1 (fib_benchmarks.rb:18}
  1.59      0.01     0.01     0.00     0.00    10500  Fixnum#+ (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00    10500  Fixnum#- (ruby_runtime:0}
  0.00      0.05     0.00     0.00     0.05      500  Object#fib_tail_aux (fib_benchmarks.rb:18}
  0.00      0.05     0.00     0.00     0.05      500  Object#fib_tail (fib_benchmarks.rb:14}
  0.00      0.05     0.00     0.00     0.05        1  Integer#times (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00    11000  Fixnum#zero? (ruby_runtime:0}
  0.00      0.05     0.00     0.00     0.05        1  Object#profile (fib_benchmarks.rb:48}
  0.00      0.05     0.00     0.00     0.05      500  Proc#call (ruby_runtime:0}


while memory:
Thread ID: 70118283774380
Total: 0.040000

 %self     total     self     wait    child    calls  name
 50.00      0.04     0.02     0.00     0.02      500  Object#fib_while (fib_benchmarks.rb:26}
 25.00      0.01     0.01     0.00     0.00    11000  Fixnum#> (ruby_runtime:0}
 25.00      0.01     0.01     0.00     0.00    10500  Fixnum#+ (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00    10500  Fixnum#- (ruby_runtime:0}
  0.00      0.04     0.00     0.00     0.04        1  Integer#times (ruby_runtime:0}
  0.00      0.04     0.00     0.00     0.04        1  Object#profile (fib_benchmarks.rb:48}
  0.00      0.04     0.00     0.00     0.04      500  Proc#call (ruby_runtime:0}


inf memory:
Thread ID: 70118283774380
Total: 63.810000

 %self     total     self     wait    child    calls  name
  3.96      2.53     2.53     0.00     0.00   126500  LazyCons#initialize (./fib_inf_list.rb:18}
  1.13     42.51     0.72     0.00    41.79   220500  Proc#call-1 (ruby_runtime:0}
  0.66      3.16     0.42     0.00     2.74   115500  LazyCons#scanl (./fib_inf_list.rb:37}
  0.53      2.94     0.34     0.00     2.60   126500  Class#new (ruby_runtime:0}
  0.38     37.95     0.24     0.00    37.71   105000  LazyCons#rest-1 (./fib_inf_list.rb:24}
  0.16      0.10     0.10     0.00     0.00   105000  Fixnum#+ (ruby_runtime:0}
  0.13     63.81     0.08     0.00    63.73    10500  LazyCons#take-1 (./fib_inf_list.rb:41}
  0.11      0.07     0.07     0.00     0.00   126500  #allocate (ruby_runtime:0}
  0.08      0.25     0.05     0.00     0.20    11000  Object#fibs (./fib_inf_list.rb:56}
  0.03      0.02     0.02     0.00     0.00    10500  Array#+ (ruby_runtime:0}
  0.03      4.60     0.02     0.00     4.58      500  Proc#call (ruby_runtime:0}
  0.02      4.48     0.01     0.00     4.47    10500  LazyCons#rest (./fib_inf_list.rb:24}
  0.00      4.60     0.00     0.00     4.60        1  Integer#times (ruby_runtime:0}
  0.00      4.58     0.00     0.00     4.58      500  LazyCons#take (./fib_inf_list.rb:41}
  0.00      4.60     0.00     0.00     4.60        1  Object#profile (fib_benchmarks.rb:48}
  0.00      4.58     0.00     0.00     4.58      500  Object#fib_inf_list (./fib_inf_list.rb:52}
  0.00      0.00     0.00     0.00     0.00    10500  Fixnum#- (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00    11000  Fixnum#< (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00    11000  Fixnum#zero? (ruby_runtime:0}
  0.00      0.00     0.00     0.00     0.00      500  Array#first (ruby_runtime:0}
Follow

Get every new post delivered to your Inbox.