Archive for the ‘programming’ tag
URLs worth caring about
I like seeing URLs, whether in the link text or by hovering over a link. They are often truer to their content than what the person adding the link writes. I always have the status bar turned on in my browsers. It annoys me that there is no status bar on the iPad. It pleases me that Chrome shows URLs without taking up space for a status bar, by fading them in when hovering over a link. But a significant percentage of URLs aren’t worth a whole lot. They are either too long, or contain nothing identifying but numbers.
Some say that URLs will go away. But do they have to? If people still care about them, I don’t think so. That’s why we need URLs worth caring about. Just like we need buildings worth caring about, and neighborhoods worth caring about:
(Yes, I realize that this blog’s urls suck, and don’t have to. I plan to change that soon.)
Downgrading Django to 1.1.2
I had some issues upgrading to Django 1.2.1, and needed to roll Django back to 1.1.2. I searched for “Downgrading Django”, and didn’t find instructions, so now that I’ve figured it out, I’m posting instructions here.
First, if you’re using easy_install, I suggest switching to pip. It has more features, is better designed, and uses the same repositories, so it’s easy to upgrade. To install it, type sudo easy_install pip.
To install Django 1.1.2, type sudo pip install Django==1.1.2. When I ran this, pip automatically removed the newer version of Django, and the two glitches I was encountering with the admin interface went away. Once I figure out what caused the glitches, I’ll upgrade back to Django 1.2 so I can take advantage of its new features.
new grem feature: jumping quickly to a location on github.com
I added a new feature to grem: if you’re somewhere in your ~/github directory, you can simply type “grem” and the program will use launchy to open an appropriate page on GitHub in the default browser.
For example:
- if you’re in ~/github, your browser will navigate to http://github.com/
- if you’re in ~/github/qrush, your browser will navigate to http://github.com/
- if you’re in ~/github/qrush/gemcutter or a subdirectory, your browser will navigate to http://github.com/
This is handy, because often I’m in someone’s repo and I wonder what else they worked on. All I have to do now is go up a directory or a few, and run “grem” with no arguments, and I’m there!
I chose to make it go to a repo, rather than a directory in the tree, when I’m in a subdirectory of a repo, for simplicity’s sake. This way, I don’t have to worry about when a directory has been added to a local copy of a repo but not the github repo.
What do you think? Is this command-line tool useful? Do you think my directory structure makes sense? Are there any other ways you can think of that I can take advantage of having a system for organizing my copies of repositories?
grem: a github repo manager
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.
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).
Incanter and Aquamacs on Mac OS X with Leiningen and aquamacs-emacs-starter-kit
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:
- Download the script.
- Place it on your path and chmod it to be executable.
- 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.
Organizing github repos
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.
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.
ASIHttpRequest Rocks
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.
Django: Foreign Keys and Coupling
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.
jQuery UI widgets
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.
That’s it?!!
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.




