Meet grem. Short for gremlin, grem is a tool for managing local copies of github repos. Grem is also my first gem. It was remarkably easy to create a gem with newgem and push it to gemcutter.

gremlin (from Inti on flickr)

Right now, all grem does is clone a repository to a default directory. To try it out, run

sudo gem install grem

…and then:

grem benatkin grem

…and grem will clone my grem repo to ~/github/benatkin/grem, creating directories as needed.

I got this idea from a comment on my earlier post, Organizing github repos, by KevBurnsJr, where he suggested making a bash script for cloning repos into my directory structure. I decided to take it a step further by making a gem, and adding more features later.

By the way, standardizing on ~/github/username/reponame has worked wonders for me. It’s lowered the friction for downloading repos, looking at them, and coming back to them later. This script should lower the friction even more.

Speaking of lowering friction, with Heroku‘s help, I deployed a mini-app. Check it out at http://last7.heroku.com/ (github).

I’ve known of Incanter, a powerful statistics library for Clojure, for a while now. A couple of days ago I set out to get it running nicely, and was successful. Here are the steps I took:

Installing Leiningen

I used Leiningen because I knew it made packages easy to install, and while it is described more often as a build tool than as a package manager, it has a repl command, so I can use it for experimenting with libraries, too. It was at least as convenient as I expected. I’m going to continue using it to set up repl environments.

To install it, follow these simple instructions from the leiningen repository:

  1. Download the script.
  2. Place it on your path and chmod it to be executable.
  3. Run: lein self-install.

That’s it! It uses the Java installation that comes with OS X. I installed this on Snow Leopard, but these instructions should work on Leopard as well. If you replace aquamacs-emacs-starter-kit with emacs-starter-kit, all of the instructions in this blog post should work on other platforms, with some other minor changes.

Opening a Clojure repl with access to Leiningen

To start a new Leiningen project, I went to my home directory and ran:

This creates a directory called incanterplayground with project.clj and a few other files. Only project.clj will need to be modified to get an Inanter repl environment up and running. I opened project.clj and added the repository and dependency information for Incanter:

And then, following the instructions in Leiningen’s README file, I ran lein deps and lein repl in the project directory:

I wasn’t planning on using swank or nailgun. I haven’t been using emacs, and nailgun doesn’t sound very appealing to me, mainly because when I looked at using it with JRuby a year or two ago, it came with security warnings so strong that they seemed to be telling me you don’t want to use this. When I read the above message, though, I felt like I should give one of the two a try. Before I even tried importing incanter, I started working on getting emacs and lein-swank running.

Getting the lein-swank plugin running

The lein-swank plugin is a server, and therefore doesn’t require emacs to be up and running to run. So I tackled getting lein-swank running, and I put off installing SLIME, which I didn’t know how to use, until afterward. I added a dev-dependencies line to my Leiningen project file. Here is project.clj with this added:

I then ran lein deps and lein swank:

(Note: I made this tutorial while running through these steps for the second time, so lein deps is using files that have already been downloaded. The first time you run it, lein deps will show the download and installation progress.)

This indicates that the swank server is running, and is ready to be connected to by emacs.

Getting emacs running and installing packages with ELPA

I used to use emacs, and I had installed Carbon Emacs to try out org-mode. I didn’t check to see if ELPA was already installed, but rather, ran the directions on the ELPA website. This involved taking a piece of code and evaluating it. Then I went to the ELPA package manager, selected the packages listed in the lein-swank installation instructions, and ran the install. I saw a lot of warnings go by.

When I got done, I restarted emacs, and tried running M-x slime-connect. The command, however, wasn’t installed. I searched for help, and found a post from a user who had the same problem. Someone replied to the post saying that he probably had a bad installation of ELPA. At this point I punted and decided to try aquamacs instead. After installing aquamacs, and running it, I got the idea to find an emacs profile to use that would hopefully have ELPA already working. I searched for an aquamacs emacs profile and found aquamacs-emacs-starter-kit.

I downloaded it to my github directory, rather than to my Emacs preferences directory.

I then added these three lines to the end of ~/Library/Preferences/Aquamacs Emacs/Preferences.el (which just had comments in it):

After this, I restarted Aquamacs, saw a ton of warnings fly by, waited it for it to finish what it was doing, and restarted Aquamacs again. I saw more warnings fly by, and restarted Aquamacs again. I may have restarted Aquamacs a third time before it stopped showing a lot of warnings. It takes a little time for aquamacs-emacs-starter-kit to work its magic.

Then I installed the required packages by typing M-x package-list-packages, selecting the packages that I needed or wanted that weren’t already installed, and typing x to install them. I made sure that clojure, clojure-test, paredit, slime, slime-repl, and swank-clojure were selected before hitting x. After pressing x, I waited while some warnings flew by, and restarted Aquamacs again. Then I tried typing M-x slime and then pressing tab to see what the autocompletion showed. There were a bunch of results, so I felt confident that I had successfully installed SLIME. Indeed I had.

Running SLIME

Finally, I ran SLIME. I still had lein-swank running from a previous step, so I typed M-x slime-connect and chose the default host and port. After a second or two, I saw a silly message indicating that it worked.

I poked around the commands, first using tab completion on M-x, and then looking at the the keyboard bindings in the project.clj buffer with C-h b. I opened a new file with C-x C-f playground.clj, and started evaluating expressions in there. You need to be in a window that’s in the slime mode; the *scratch* buffer will evaluate with emacs lisp if you type C-x C-e in it.

Here’s an example session:

If you go to the end of the first line and type C-x C-e, SLIME should print out #’user/x in the status line. If it works, you know it’s evaluating Clojure and not emacs lisp. Go to the end of the second line and type C-x C-e, and then to the end of the last line and type C-x C-e. It prints out 6. This way I know that it’s running them in the same Clojure session. Pretty cool, huh?

Printing out a graph with Incanter

I found this introductory incanter code, pasted it into playground.clj, and started evaluating it.

After I ran C-x C-e on the first line, a new dock icon with a Java logo appeared. I switched back to Aquamacs, went to the end of the second line, and typed C-x C-e again. Three lines showed up in the emacs status bar, describing an object that was returned. I clicked the new dock icon, and voila! A graph showed up.

What now?

Now that I have a nice Emacs, Clojure, and Incanter setup, there are so many different directions I could go. I have something in mind, but I don’t want to reveal it, because in my mind I might feel like I’m done and lose my motivation to do it. If you need ideas, though, one thing you could do is go to the Data Sorcery blog and start trying the examples there.

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Carlton

I find that I spend an awful lot of time naming things. More than that, it’s a distraction. I also spend a lot of time organizing directories, and a lot of time trying to recall where I’ve put things.

Cluttered Office

GitHub has made it a lot easier for me to find code online. In fact, it’s so easy that sometimes I just download another copy of a repository rather than search my home directory for one.

As of yesterday, though, the madness is over, because I decided to replicate github’s organization on my own computer. Here are the first two levels of my new, and currently modest, ~/github directory:

As I get more repos downloaded, I think my new organization style will only make more sense. And if I also need a github repo somewhere else, I can symlink it. Also, ~/src is symlinked to ~/github/benatkin.

The idea came when I realized I didn’t have all of my GitHub repos in one place. As you can tell by the tree, I still don’t. Also, I’m putting stuff in my GitHub directory that I may add to GitHub later. If something stays there longer than a week and  I don’t put it on GitHub, though, I’ll move it to the archive.

This new directory structure also has the nice side effect that I won’t need to delete repositories just because I need stuff to be easier to find.  It shouldn’t take long for the number at the end of the tree output to go over 100, and in a while, the number could be in the thousands. I’ll think of it as a partial mirror.

If you do any native iPhone development, check out ASIHttpRequest. You’ll be glad you did. I tried a couple of frameworks, Objective Resource and HttpRiot before using this library. I found that using a library for making HTTP requests feels much more natural to me than using a framework. ASIHttpRequest reminds me of jQuery’s ajax() function.

Perhaps when I’m working with a large number of models I’ll want something like Objective Resource or HttpRiot. If I do, I’ll consider changing one of them to be an interface to ASIHttpRequest, which builds upon lower-level APIs to be more powerful than NSUrlRequest.

I have a django project that currently has two apps: one that contains the data for a directory site, and one that provides support functionality for importing the data in the directory site. I want them to be loosely coupled. The support app should be driving the data app. The data app, and its models, shouldn’t be driving the support app.

A problem I ran into is this: I want a record of an import to remain, even if the imported record is deleted. But, since I used a ForeignKey, and Django has cascading deletes, the record of the import would be deleted. I searched for a way to turn off this behavior, and found two options:

  • Clear the ForeignKey reference to the imported record in the record of the import before deleting the imported record
  • Use a ManyToManyField instead

At first, I used the first option. I saw the second option, but skipped it because it seemed overkill. Something didn’t sit well with having to clear a field before deleting, though. So, weeks later, I searched again, and was reminded of the ManyToManyField option.

The ManyToManyField option is a trade-off. On one hand, it fixes my coupling issues. On the other hand, it adds an extra table to my support class, and is overkill for something that only needs zero or one instances of the model associated with it. In this case, I chose the ManyToManyField option.

I’m quite happy with my new choice, and I think I might have chose it earlier if I hadn’t immediately discounted one approach and listed the pros and cons of each approach first.

JavaScript is a prototyping programming language, and as such, has many different ways to define classes or create objects. If you try to search for a tutorial on object-oriented programming in JavaScript, you’ll find that each tutorial has its own way of doing things. It can be very confusing.

Fortunately, jQuery UI has a widget framework, that makes setting up object-oriented widgets easy. After using it, I wanted to use it for everything, until I find something at least as easy to use as it. I didn’t, however, want to include a large dependency like jQuery UI if I was only using it for its widget framework.

I was glad to find, after a little experimentation with the Build Your Download page, that I can get jQuery UI’s widget framework in a download of only 9.06 kb minified. You can get this by clicking Deselect All and checking UI Core. After that, the theme drop-down box should be set to No Theme.

I made a geocoding widget for Django in a project for a client using this. It simplified my code a great deal.

One thing it does that’s good for projects that might have a lot of custom widgets on one page (which was not the case for this one) is it takes care of using jQuery’s data() method, which stores objects in a way that is less likely to cause memory leak issues in Internet Explorer than storing objects using closures.

How do you react when you find out that a lot of grief could have been prevented by something as simple as maintaining a to-do list? Or following or not following a piece of advice from a friend or colleague? Or doing a little bit more or less yak-shaving? Or taking a break when you need one? Or paying closer attention to Object-Oriented Design?

Are you happy for the learning experience, or do you get angry at yourself because it’s something you should have already learned?

While self-anger can drive a person to change, I think it’s important for programmers to be able to take a step back when it gets to be too much. After all, every passionate programmer knows enough to be successful beyond his or her wildest dreams. Execution is the tricky part.

I’m going to experiment with doing microblog summaries and putting content in a directory. For now I’m putting them on twitter tagged with #blog, copying the content, and expanding them just a bit. The full articles will simply be directories containing an index.php or index.html file. That way I can play around with the layout of individual posts, or make the pages dirt simple if I so desire.

Image editing with php & GD is simple and fun. I removed text from a SlimTimer Fluid icon I found on flickr with it.

jQuery has convenience functions for events like click and keydown, and lets you create and use your own events with bind() and trigger(), but doesn’t provide a way to create your own convenience functions for custom events. I took several lines of code from the jQuery source and created a plugin that makes creating convenience functions as easy as calling $.convenience(‘nameOfFunction’). Then I can bind or trigger an event simply by calling nameOfFunction() on the selector. This should be apparent by looking at the demo source (the third link).

  1. jquery.convenience.js – plugin source (github)
  2. convenience.html – demo (my site)
  3. convenience.html – demo source (github)

The example shows one feature of custom events that I overlooked: propagation.

I got Screenflow and a Snowflake with the intent to make screencasts, but found another use for them, that might be more valuable.

It started with me testing out my equipment. I wasn’t ready to do a scripted screencast, so I just coded for an hour, while talking, and looked at the results. I found that talking to myself out loud helped me figure things out more quickly in a couple of cases. And since I was testing my gear, talking to myself didn’t feel all that strange.

Some time after watching it, I wondered what it would be like to record myself frequently. Maybe if I did something really cool, I could take a look back and try to see what my thought process was, and it would reinforce it. Or, if I was in a slump, I could use it to remind myself what I’m capable of.

Then I asked myself whether it’s practical. The answer I got was a resounding “Sure it is!”. Space and processing power are cheap. With the default settings, Screenflow documents take roughly a gig an hour. 500 gigabyte hard drives can be had for under a hundred bucks. If I record 5 hours a day, which is a *lot*, it’s about a dollar a day for the space. Screenflow doesn’t take much processing power, or if it does, it doesn’t interfere with the work that I’m doing.

I’m not sure what the most clear analogy is to other fields, but there are obvious parallels. Disk Jockeys commonly record their experiments. Team sport coaches record their players’ performances.

I’m really excited about this. I can get part of the benefit of pair programming by talking out loud and learning from yourself, without the need for a willing partner. It’s also a journal that’s remarkably easy to write. It does take a lot of time to read, though, so I’ll probably only play back my favorite sessions, and I’ll only play bits and pieces of them most of the time.

The only caveat is I might feel like I’m my own big brother. I might as well get used to that, though, as I expect in the future there will be wearable video cameras that transparently archive everything. In fact, something like that’s probably already been built.

I still plan to do some screencasting. Stay tuned!

On the microblogging site twitter, a blog post is called an update, or informally, a tweet. When someone copies a tweet and posts it to their twitter feed, it’s called a retweet. Sometimes opinions are retweeted by those who share them, other times it’s information, and other times it’s silly memes. This evening, a local friend of mine on twitter started a meme by saying “tweet.” and asking for a retweet. One person who retweeted it got his message retweeted.

retweets

I know I’m not the first person to graph retweets, but being curious as to what this particular graph would look like, I decided to do a graph in Python as a programming exercise. I did it using a GraphViz library. The code is here:

import pydot

graph = pydot.Dot('rt', graph_type='digraph')
tweeps = ('tysoncrosbie', 
           ('phxreguy', 
             ('sbowerman',
               ('phxwebguy', 'leaky_tiki'),
               'refriedchicken')), 
           'vhgill',
           'Yartibise')

def add_edge(source, dest):
  graph.add_edge(pydot.Edge(source, dest))

def first_flat(tree):
  if isinstance(tree, tuple):
    return first_flat(tree[0])
  else:
    return tree

def find_edges(tree):
  if isinstance(tree, tuple):
    source = tree[0]
    for dest in tree[1:]:
      add_edge(source, first_flat(dest))
      find_edges(dest)

find_edges(tweeps)
graph.write_png('rt_graph.png')

The code takes a nested list structure (a tree) and produces edges from it, which can be graphed by GraphViz. It uses pydot, a GraphViz library for Python. Here is the resulting image:

rt_graph1

Observations:

  • Python doesn’t have a built-in list flattening function. This was irritating. Ruby’s is Array#flatten. It would have been so much nicer to have been able to grab the first element of a flattened list rather than write the first_flat function or copy/paste a list-flattening function from the Internet.
  • pydot is really simple to use. I liked how it could produce a png file. I was planning just to have it create a dot file and then use GraphViz to create a png, but when I saw the write_png function I decided to just use that.
  • GraphViz has reasonable defaults. It produced a nice-looking graph on the first try. I think that’s a big part of why GraphViz is as popular as it is.

I was hoping to have a little more to show for tomorrow’s Python Interest Group meeting, but this will have to do!