Tuesday, August 16, 2011

Compiling Vim on Ubuntu Natty

The latest version of vim 7.3 included in Ubuntu Natty has a bug (Ruby-related I think) that causes the awesome command-t plugin to stop working. To get around this you will have to compile vim yourself to get to a higher patch level. I've done this a couple of times recently but always forget which dependencies need to be installed and which configure flags to enable and which make targets to run if you change stuff. So I thought I'd try to document my findings in case I have to do it again and to try to figure out exactly which packages need to be installed. On some occasions I've ended installing hundreds of megs of dependencies just to be on the safe side.

1) First you need to clone the vim repository using mercurial, this step is easy and is described on the official vim.org site: http://www.vim.org/mercurial.php

2) Install dependencies: sudo apt-get build-dep vim This pulls in about 554 MB of stuff at the time of writing and I'm not sure if all of it is needed but it at least is quick to type. Running configure at this point, gawk was still missing.

2) Next you need to configure the build you create. I've found the following to be smallest subset of required flags: ./configure --with-features=huge --enable-rubyinterp

3) then make and make install

Friday, May 13, 2011

Deploying a Django app with Apache and mod_wsgi

At work recently I've been deploying a small Django app. It's taken quite a long time. Most of the time things have been moving forward nicely but every now and then there has been some misleading detail in some instruction which has made me stand in place for a long time.

In a nutshell (on Ubuntu)




  • Apache needs the mod_wsgi module

  • Use the WSGIScriptAlias directive to point to the location of your project's .wsgi file

  • Point to your settings in the .wsgi file



This procedure is described in How to use Django with Apache and mod_wsgi

Complications




  1. I used virtualenv to prevent the installed modules from conflicting with other modules. This meant I had to follow the Virtual Environments section of mod_wsgi's wiki. You don't have to follow these instructions precisely. The instructions are aimed at preventing system-wide modules from shadowing/overriding modules in your virtualenv. Simply using site.addsitedir with the virtualenv's site packages directory worked for me.



  2. NOTE! The instructions in Django's docs also neglected to mention that you have to append two paths to your sys.path. You have to append both your project's path as well as the project's parent's path. If you don't add the parent you can't specify os.environ['DJANGO_SETTINGS_MODULE'] prefixed with your project's name. If you don't add the project itself you can't import the apps inside it.



  3. The server that was going to run the web app only had Python 2.4 installed. I wanted to make sure that my development environment would be reasonably close to the production environment. Installing an older version of python was very easy. All you had to do was use make altinstall instead of make install. However, now you also need to compile the mod_wsgi module manually. You use the --with-python=/usr/local/bin/python2.4 configure flag to specify which version to compile for. Then when you make the module it ends up in a hidden .libs folder. Move it from there to Apache's modules folder. I chose to rename it to wsgi_2.4.so or something so it wouldn't overwrite the module installed by apt-get earlier. Check some .load file from your /etc/apache2/mods-available/ for reference. Then you can add your own wsgi_2.4.load and wsgi_2.4.conf files to mods-available and use a2enmod to enable it. Now since you are using your own vintage Python version you have to additionally use the WSGIPythonHome /usr/local/ (or something, my mind is a bit unsure of this detail at the moment) directive to specify the prefix to your Python installation.



  4. Finally if you are serving static content (css, images, js etc.) you should be doing that straight with your web server instead of through Django. Django has a very nice app called django.contrib.staticfiles which through adding STATIC_URL to your settings.py file, adding django.core.context_processors.static to TEMPLATE_CONTEXT_PROCESSORS and prefixing your static urls with {{ STATIC_URL }} allows you to easily serve files with runserver. Then for production use you will want to set up the STATIC_ROOT variable in settings.py, put application-specific media in a static folder and run manage.py collectstatic to have all your media files transferred into the STATIC_ROOT. Now you can serve the files from this location by using the Alias directive in Apache for example.



I felt way more l33t when I had to set up the 2.4 version of Python manually than by simply installing everything through apt-get like a n00b.

Google Nexus S



Earlier this week I finally decided to upgrade my old Nokia E51 to an Android phone. Iphones are cool and all, but as a programmer who would like to make some apps of my own, it would be too much to have to both get a Mac and learn objective C as well as iCocoa-whatever-it's-called. You can even code Python on the android!

I think I bought the E51 around 2007-2008. It was good enough for some basic web surfing and worked excellent as a mobile broadband device on train trips at least. However, for a long time I've felt inferior to all the smart phone owners.

I'll get the minor problems I've had out of the way first, so I can end on a positive note. The wlan at the office has been very weak. It has seemed to work fine in my wlan at home though. I was able to download some of my Spotify playlists without problems. Also Angry Birds crashed during start-up and after that there was some bug that shifted the whole set of levels one step down, so I had to reset the game's data.

Apart from these problems, I've been very satisfied with the Nexus S so far. The jump in web browsing convenience from the E51 is huge. The UI is very smooth. One of the reviews I read before getting the phone mentioned something about slight delays in the UI, but I haven't noticed anything. Setting up e-mail was incredibly easy. I guess it helps that I use gmail both for my personal e-mails and for my work e-mail. Now I get nice little notifications every time I get an e-mail to either of them.

There are a lot of nice touches too. I like the effect when the screen is turned off which looks a bit like an older TV screen being turned off. There were some pretty nice blip and blop message notification sounds to choose from.

There are still many areas I haven't explored that much yet. I have only tried taking two pictures so far. Both of which seemed perfectly fine. I haven't tried tracking that much with the GPS yet. There are still a lot of settings to explore. Today, for example, I figured out that it was possible to easily switch between languages while typing an SMS. Another thing which is a bit unclear is how to tell which apps can show a widget with extra information on the home screen.


I've only downloaded free apps so far, so I don't know if you can buy apps without a credit card in Finland yet. Not that it matters that much to me. I have to say I'm a bit overwhelmed with all the crap apps in the app market though. I would much rather have an app market which is free from Coder Joe's MyFirstAndroidApp which is either a crappy clone of some other game (like Angry Birds, Bejeweled or Tetris) or some lame memory game with naughty pictures. Google, please give us a way to filter these apps out!

Well I guess that's about it. This was just supposed to be a brain dump about my initial opinion of my new awesome phone.

PS. The image entering functionality on blogspot (blogger?) is awful. I tried putting the cursor where I wanted the image to go. Press "Add Image" and both of my uploaded images go to the top of the article. No way to drag them in the Compose view. So you have to edit the hellish html to get the image where you want...

Tuesday, March 22, 2011

O'Reilly Disaster Relief 50% Special Off E-books

Today O'Reilly had a great 50% sale on all e-books with all revenues going to the Japanese Red Cross for disaster relief after the earth quake + tsunami from about 10 days ago.

I bought the following books for a rainy/sunny day:

High Performance MySQL, Second Edition

I personally prefer Postgres. I think MySQL feels a bit flimsy for some reason I can't articulate. However MySQL is still very widely used and I felt that whatever lessons I learn from this book probably applies to other SQL databases.

Canvas Pocket Reference

I coded a simple clickomania code with a friend at work which uses canvas for drawing so I was interested in learning more about using canvas.

Programming Android

I don't even own a smart phone :( But I'm still interested in learning about programming for Android. I'll probably end up owning one sooner or later. Just not in a hurry to get one.

TCP/IP Network Administration, Third Edition

This is probably a lot of stuff that I already learned in school many years ago. But I feel like I still have some gaps in my understanding. Gaps that don't usually affect my every day work, but gaps that I still want to fill, well just because.

Anyway, this was a great move by O'Reilly. They deserve a lot of credit for putting this special together. The total paid amount is at $70k at the moment. Let's hope it goes even higher and that Japan finally gets the power plant situation under control.

Monday, February 21, 2011

"The web" has been replaced

I'm working on my own Rails application during evenings at the moment just wanted to point out something I already had noticed a few months ago: most of the old technologies that made up "the web" a while ago have now been isolated through a new layer of "languages" (I hesitate to call html and css languages).

JavaScript can now be written using coffeescript. CSS can be written using sass/scss/less. HTML can be written using haml. Now there might have already been replacement technologies for all these areas a long time (don't know too much about alternative browser languages), but now it's really feeling like these technologies are good enough to be used in real projects.

My Rails application is going to attempt to use all of them. While I think haml is nice and clean and it's great to not have to type all those annoying angular braces, I'm even more excited about coffeescript and scss. As most people who take the time to learn JavaScript notice, it actually has some really good parts but it's bogged down by some pretty last-millennium syntax and some really awkward design and implementation mistakes. On the other hand, I think coffeescript has the nicest syntax of all languages at the moment.

I haven't really used the CSS replacements that much yet. Layout and design aren't my strongest areas. However in nearly every web application I've worked on so far, the CSS has been very messy. I'm hoping that scss might help alleviate this problem.

Monday, January 31, 2011

Why PHP sucks #1

Welcome to my series of posts of why PHP is the worst language I've ever had to work with. Today's topic is error handling. Or perhaps PHP's lack of it. One of PHP's philosophies seems to be that the programmer should not be troubled by any nasty errors. It's better to attempt to do something even if that is wrong. Failing fast with a clean error message and a stack trace is not one of PHP's virtues.

PHP seems to (I'm just guessing here) have only had errors (with different codes for warnings, notices etc.) before. Now it also has exceptions. This is pretty annoying because you end up having to write your error logging code. This involves setting up your own custom error handler which rethrows the errors as exceptions. Then you can add your own custom exception handler which can obtain a stacktrace from the exception and print it out inside <pre> tags so that you can make some sense of it.

Then you figure out that PHP treats "fatal errors" differently. It bypasses your error_handler and just shuts down your application instead. Then you figure out that you can register a shutdown function and try to output a stacktrace manually from there. Unfortunately by the time this function is called PHP has already torn down most of the stuff you were using and the only thing in your stacktrace is gonna be your own shutdown function.

So you end up installing the xdebug extension and by enabling it you can finally get some output when your program. Except it's a big mess of lines where you end up just having to go through all the mentioned lines to see if you can figure out what's causing your problems. Then after a while you notice that you can echo a single opening <pre> tag at the beginning of your script while debugging your errors and suddenly all the output from xdebug is quite readable. But the fact that you have to go through all of this crap shows why PHP sucks.

Saturday, January 8, 2011

Programming books I read in 2010

I read quite a few programming-related books in 2010. Here are the ones I can remember:

Tehokas Java EE-sovellustuotanto

Perhaps the most important book I read all year. Helped me in getting my current job. Taught me the basics of Maven, Hibernate, Wicket, Spring and so on. All of which were needed in the first project I was working in.

Javascript: the Good Parts

Very nice book. I read this for the Javascript study group we've started at my workplace. Recommended to read more than once if you really want to learn Javascript.

Effective Java

Very good Java book.

Refactoring to Patterns

I just skimmed through the descriptions of the patterns and skipped most of the longwinded refactoring instructions. If I remember correctly the smaller refactorings of Martin Fowler's were more immediately useful.

Patterns of Enterprise Application Architecture

While the content of this book was different from what I expected based on the "enterprise application" of the title, it was still a very good book. It taught me more about how Hibernate works. And I've referred to this book a few times when refactoring a project at work.

The Pragmatic Programmer

I had already read this one before. I just read through it again because it was such a long time since I last read it and because it's such a short and easy read.

Facts and Fallacies of Software Engineering

Sort of interesting. The abstraction level was quite high. I was aware of many of the ideas in the book already.

Pro Git

Online book about git. In 2010 I greatly improved my understanding of Git. This book was one of the best sources for learning about it.

CSS Mastery: Advanced Web Standards Solutions

HTML and CSS is one of the biggest pain points of making web applications. One of the things I remember the best from this book was how often the chapters had to have some extra workaround crap instructions for IE and what a pain in the ass it is to make rounded corners.

Rails Recipes

This one was written for Rails 1, so it was a bit out of date. At the moment, I can't really think of any useful recipes I learned from this.