Macs and Emacs

In my last post, I talked about how to set up Emacs as a Python IDE. Since then, two things have changed:

  • I got a Macbook Air. Switching from Linux to Mac required a few (mostly minor) changes to my Emacs configuration.
  • I have begun using the fabulous IPython Notebook, which has really helped me organize my code and streamline my workflow.

In this post I’ll tell you how switching to a Mac has affected my configuration. In a future post (coming soon!), I’ll talk about the IPython Notebook, how I set it up for my configuration, and how it’s really improved my Python development environment. You can find the emacs configuration associated with this post here.

Emacs as a Python IDE

Update 04/08/2014: It seems like this post has been helpful for a lot of people, which really makes me happy! I just wanted to let new readers coming by know that the configuration I present in this post is out-of-date. However, I have my Emacs configuration on GitHub, and do try to keep it up-to-date. If you have any difficulties getting the configuration in the GitHub to work, please do submit a bug request and I will try to help you fix the problem.

As I mentioned in last week’s post, I am a heavy Emacs user and find it invaluable as an environment for my research. In particular, I use Emacs most for data analysis and modeling in Python, including an interface to IPython, which allows me to quickly switch between writing and running snippets of code. This proves to be a great replacement MATLAB, at least in my opinion.

I frequently get requests for my .emacs configuration file or questions about how I have such an environment set up. As such, in this post I’m going to document how to set up your Emacs configuration to support rapid Python development, including the plugins I have installed and the keyboard shortcuts I use most often.

Note that I am running GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK Version 2.24.5). I have never tried running it under OSX or Windows, so I can’t guarantee that these instructions will transfer, but you are welcome to try. If you run into any inconsistencies, feel free to send them to me and I will update this post accordingly!

Also, if you are new to Emacs, please read my beginner’s guide first, as I will be assuming familiarity with basic Emacs functionality and terminology. I will also assume you can at least read and understand LISP to some extent (but you don’t necessarily have to be able to write it).

Absolute Beginner's Guide to Emacs

I’ve been using Emacs () as my primary text editor for several years now. It takes some getting used to – the keyboard shortcuts are completely different from what you’re probably familiar with, e.g. Ctrl-C for copy and Ctrl-V for paste. Despite the somewhat steep initial learning curve, however, I find that Emacs is invaluable for rapid coding and for flexibly editing all different types of files in the same environment.

I remember how overwhelming it was to figure out how to do anything when I first got started, so in this tutorial I’m going to aim to give you the basics to get started. This is by no means a complete guide to Emacs (though if you have suggestions for things to add, I’d be happy to do so), but hopefully should be enough to start comfortably using Emacs as a text editor.

This tutorial is mainly for people who have primarily used GUI text editors and coding environments and are not used to a primarily text-based program, running commands in the editor itself, and/or using large amounts of keyboard shortcuts.

Saving Figures From Pyplot

Well, it has been a while since I’ve posted. Over the summer I moved to beautiful Berkeley, California to start my PhD in Psychology at Cal. Moving has kept me pretty busy, but as things are starting to settle down a bit, I’ve decided to start making an effort to blog regularly (even if it’s just a short and simple post like this one).

Most posts are probably going to be Python-related (particularly from a scientific computing point-of-view). If you have any requests or suggestions, please let me know! I’m always open to ideas.

Often when I’m doing data analysis, I will need to save many figures to disk at once. For example, if I’m looking at the distribution of human responses to every stimulus I have (say, around 60 different stimuli), I’m going to need a different plot for each. Matplotlib actually has a pretty straightforward function for saving figures, but there’s a little bit of scaffolding that I like to have around it by default.

The Demise of for Loops

I almost exclusively use Python in my research. I write 3D interactive experiments using Panda3D and I collect, analyze, and visualize my data using NumPy, SciPy, and matplotlib. While I have been using Python for almost 5 years now, I only began using Python for scientific programming when I joined the Computational Cognitive Science Group at MIT. The sort of programming I do these days is very different from the software engineering I used to focus on.

In particular, I almost never use for loops when I am doing any form of serious number crunching. For loops do have applications, but I think they tend to be overused, especially in Python. There are two reasons:

  1. Almost anything numerical can be done with NumPy.
  2. Almost everything else can be done with list comprehensions.

So what? Does it really matter whether you use NumPy vs. a list comprehension vs. a normal for loop? It absolutely does and I will go through a few examples in this post.

An Introduction to Classes and Inheritance (in Python)

If you would like a copy of the code used in this post, you can download it here.

This post aims to give a short, basic introduction to the concept of classes and inheritance, using Python as the language of choice. It assumes knowledge of very basic Python syntax and functions. These examples were conceived during the Boston Python Workshop this past weekend.

In Search of the Perfect Email Solution

Wow, it’s been a long time since I blogged last. I should fix that. Also, the site has a new theme! I hope you enjoy it. Anyway, onto the content…

Over the course of the years, I came to realize that I was dissatisfied with my email solution. Back in middle school, I used Hotmail (yuck). Then Gmail came out, which was shiny and fantastic, and I used that exclusively until I came to MIT. Once I discovered SSH and that pine was automatically configured with all my mail on Athena machines, I started using that. Then I found alpine. Then I got tired of having to SSH into a screen session somewhere every time I wanted to check my mail, so I switched back to Gmail again. I eventually became frustrated with GMail’s slowness and the fact that they didn’t support GPG, so I switched back to MIT’s IMAP and used Thunderbird. And so the hilarity began…

Asking Good Questions (to Receive Great Answers)

A few months ago, I blogged about problems in the hacker world. One of the biggest issues that people ended up mentioning in comments and other discussion with me is that it is important for less-experienced people to ask intelligent questions of the more-experienced people. If they are able to do that, then the more-experienced hackers will be able to help faster and more easily, and will be more likely to put the effort into providing a helpful response. So… how do you ask good questions?

A Brief Update, and Why Numpy Is Awesome

Wow, things are crazy. Classes and the rest of my responsibilities have hit like a tidal wave, and I finally am able to start swimming towards the surface to get some air. I’m only taking 3.5 classes this term, but I’m also working on my awesome UROP 15-20 hours a week, on top of being SIPB Chair, writing a game for the MIT Assassin’s Guild, being pika’s senior house manager, and being pika’s computer chair. I always love being this busy, but sometimes I wonder if I like it a little too much. It’s hard to find a term when I’m not in over my head. But, regardless, things are going well and I’m excited to almost be done with my undergraduate career!

Social Problems in Computer Science

This morning, I read a blog post about women in computer science which was quite compelling. It reminded me, of course, of another article about women in CS, and I began thinking about about what my own opinion is on the subject. Sexism in CS and similarly technical fields is certainly a problem. But why? And how have I encountered it?