Shizzle

My little notebook

My marathon experience

June 7, 2011

About four weeks ago I ran my very first marathon. It was a great experience but what struck me is that the day itself is not really the hardest part of the entire preparation. As they say, the hardest part ins’t getting over the finish line but rather getting to the start. I can only confirm this.

Preparation

It was around Christmas ’10 that I thought what my goals were for the coming year. I had run the Berlin and Potsdam half marathon in 2010 and figured I could push myself harder this year. I didn’t fancy the Berlin Marathon as it is in August, which would mean training all summer when I could be lying in the sun doing nothing. Besides, I dislike the heat and much prefer colder days for running. So I signed up for the Prague Marathon in early May 2011. I knew that I wasn’t remotely in marathon shape but signing up early forced me to be serious about it all. I can only recommend this tactic.

I had been using an app on my phone called Runkeeper which GPS tracks you as you run and gives you instant feedback about distance run and average speed. They had a feature called training sessions gives you a fairly detailed plan about what distances and intensity you should be running. I signed up for the one aiming for a marathon in 4h30. The plan had a gradual increase in distance but was nevertheless pretty intense: 16 weeks training with four training sessions a week. This sounds like a big burden but I must say as the training progressed and my alcohol consumption, smoking and going out decreased I noticed not only increased fitness but my mood changed substantially, too. I become happy as I had never been in my live. It was hard to not constantly talk about how good I was feeling at the moment and how happy the training made me.

I had one annoying injury from one of the long runs on Sunday where I my left thigh started to ache for the rest of the week and would only heal by the following Sunday. I even had to pause the training for one week and that made me feel quite anxious about my progress. In the end the worry turned out to be baseless – every training has its setbacks and I haven’t talked to anyone who said that there wasn’t ever a problem during their preparation.

The peak of the training was two weeks before the actual event where I ran 28km. This doesn’t seem like a lot if your’re planning to run 42km but I had been doing 12km the day before and the stated strategy of the training plan was to “spread the load” over multiple days in order minimise the risk of injury. After this weekend the training decreased sharply and I only did about 10km max and finishing off with a gentle 15 minute stroll the day before the marathon.

The day itself

To say it upfront: the marathon was great and a thoroughly enjoyable experience. I had been running the training runs at a speed of 5:15 to 5:30 mins/km. At the marathon I reduced the time per kilometre to 6 minutes because I had no idea how my body would react to distances greater than 30 km and therefore I decided to take it easy.

I had slept poorly the night before and was nervous at the starting line but finally around about 9 o’clock the crowd went on their way. I decided not to use Runkeeper during the run because I knew that the GPS connection would drain the batteries and listening to music was more important to me anyway. This was a great decision. During the first 10km I managed to stick to my planned time almost to the second. At 10km I still felt great and fresh and at 11 I briefly saw my parents, who had come to Prague, and stopped for a quick photo.

From 10 to 20km it was still plain sailing and at the half-marathon mark I was ahead of schedule by about 1:30min. From 20 to 30km I was expecting some tiredness but I was surprised how well I was still feeling at 30km. I had been taking an energy gel every 5km (starting at 15km) and even my energy balance ,which I was very worried about before the race ,was okay: I wasn’t feeling the depleted or hungry and with a good mood I entered the last stage of the race.

The last 7km

At 32km I was ahead of schedule by about 3:30 mins. But by about 33km I started to feel some discomfort in my legs and thighs. Nothing major at that point but simply the stress of the race taking its toll. This kinda got worse by the time I reached 35km and from then on it could no longer ignore the pain. It hurt!

Well, you can imagine what happened now. I switched into survival mode an put all my will power into not stopping. I saw lots of people taking a short break at the side stretching their muscles but knew from experience that this would be a bad idea. Starting again after those breaks is very hard if not to say impossible. So I struggled on.

At kilometer 37 a lot of people were walking but I still hadn’t stopped my running (save for the drinking stops) but there was a small elevation, almost nothing, which I just couldn’t run up anymore so I walked it up. This really bruised my ego and made me loose some time but at that point I just didn’t care anymore. The same thing happened at kilometre 39 but in total I only lost about 3 minutes during the two short walking periods.

And then the final kilometre came. This was a great time as I was so relieved to have made it there. The spectators cheered and all the runners where scraping together the last bit of energy that was still in them. I crossed the finish line at 4 hours 13 minutes 55 seconds. That was 2 minutes slower than I had planned the day before but of course much better than the 4:30h that the training plan had aimed for.

In reflection

Looking back at the entire time I must say that the following things made my marathon as success.

  • Using an Android app that tracked my progress was a huge motivation boost. I really like Runkeeper but there are plenty about.
  • Practicing using the energy gels during the long training runs before the race. My biggest worry was that I would run out of energy and feel depleted. This is often called The Wall.
  • The weather: it was 17 degree on the day and there was always a slight breeze coming from the water

If you have an ambitious personality and like to push yourself, I can only recommend that you run the marathon yourself. You’ll be proud forever and you will notice an unprecedented increase in your happiness and well being.

Do it!

3 Comments

Configuring the Maven Site Plugin to accept scp-URLs for site:deploy

April 20, 2011

I ran into a slightly puzzling problem while upgrading from Maven2 to Maven3 this week. In hindsight the solution wasn’t all that complicated but it threw me a little because Maven is usually quite good at pulling all its required dependencies.

Basically, I was having trouble uploading files generated by Maven’s site plugin to a host. The URL was specified as follows

<site>scp://server:/some/long/path</site>

In Maven2 this worked out of the box without extra configuration.

In Maven3 this threw the following error during site-deploy:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-site-plugin:2.2:deploy (default-cli) on project $PROJECT_NAME: Unsupported protocol: 'scp': Cannot find wagon which supports the requested protocol: scp: java.util.NoSuchElementException

The trouble was that the Site Plugin apparently doesn’t automatically pull the Apache Wagon SSH implementation. You have to tell it manually to do that like this:

<build>
  ...
   <plugins>
     <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-site-plugin</artifactId>
      <version>2.2</version>
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.wagon</groupId>
          <artifactId>wagon-ssh</artifactId>
          <version>1.0-beta-7</version>
        </dependency>
      </dependencies>
     </plugin>
    </plugins>
  ...
</build>

4 Comments

Getting started with a free Amazon EC2 instance and logging in through SSH using only the web interface

March 15, 2011

Recently I’ve experienced strange Apache crashes on my VPS and the support ticket I opened wasn’t being answered, so I decided to check out Amazon’s offerings in this field. I turns out it is really simple to spin up a free (for a year) micro instance.

I chose Ubuntu as the operating system and a lot of tutorials tell you to configure and use the command line tool for starting an instance but I found it a lot easier to just go through the web interface. I will walk you through how to do that.

Sign up

Well, duh, you’ll need to set up an account at http://aws.amazon.com/ec2/

Selecting an AMI

Once you have the account go to the AWS management console and select the region you want your instance to be hosted. You’ll be redirected to the Console Dashboard where you’ll find a big fat “Launch Instance” button. That’s exactly what you want to do, so proceed to choosing an AMI.

An AMI is an Amazon Machine Image, basically a template for the various Linux distros. Collin Percival has also recently added AMIs for BSD, so if that floats your boat, go knock yourself out. Search for the most current (or LTS) version of Ubuntu you’re interested in and check that there is a yellow star next to it. There will be quite a few ones for your chosen release so just select one starting with the official Ubuntu user id of ’099720109477′. I’m not sure why there are so many copies of the same release but I guess they continually repackage the image with all the updates applied, so you don’t have to do that.

After the image select ‘micro’ as the size of the instance (one micro is a free for for one year for new customers) and accept the standard settings for everything until you get to the key pair section. There you should create a new named pair and download the pem file to, say, foo.pem. Remember that name for later.

Allowing network access

This one is a bit tricky and took me longest to understand even though it is right under your nose: by default your instance won’t be reachable from outside Amazon’s data centre. You’ll have to enable the services you want to allow.

So, create a new security group and give it a name you’ll remember. Then select the services you want to allow; most likely you want at least ssh, http and https but maybe you’re running a mailserver as well.

Connecting to the instance with SSH

Back in the Management Console you’ll see your newly created instance. Right-click on it and select connect from the context menu. This will give you the instructions on how to actually log into the instance. Basically you’ll need the key file and start SSH with something like this:

ssh -i foo.pem root@ec2-11-111-1-1.eu-west-1.compute.amazonaws.com

No Comments

How to use true UTF-8 input for Latex documents (using OS X)

February 5, 2011

LaTex is a great typesetting system. It (or at least its predecessor) was written by probably the most famous programmer still living. Once you grokked the syntax it is simple to create really complex documents with cross references and to split up the input into multiple files like you would do with a large program. If you like document creation which uses plain text input rather than some fancy-shmancy WISYWIG tool, LaTex is for you.

However, the ordinary LaTex distribution has a big drawback: It can only use ASCII as its input encoding. There was a hack that would let you could use Unicode characters in your document by preprocessing the file and replacing all non-ASCII characters with their escaped counterpart, but that was ugly and just that – a hack. Even worse, some characters (namely from Asian languages) do not have an escaped form so they could not be used in LaTex, full stop.

TexLive

This problem was solved by a Tex distribution that supports the full range of Unicode characters: Xetex. It was originally written for OS X only but has since been ported to a range of platforms.

Xetex allows you to type set a document using all of UTF-8 supported scripts without nasty hackery, as seen in the following screenshot.

Typesetting Arabic

Typesetting Arabic

Some additional features, which might or might not be interesting for you, are improved font support for OTF files and some advanced ligatures features. All of this might not rock your world but it is all nice to know that it is there. In short XeTex is a modern Tex system.

Installation and invokation

Xetex has since 2007 been incorporate by the TexLive distribution. There is an ordinary installation package for OS X but I prefer to use Macports for my package management, so I will walk you through that. First of all install the texlive base package with a

sudo port install texlive

Sit back, this will take a while. The command installs the binaries and a “medium” amount of packages, however, if you are a heavy user you probably use a lot of extra packages. I for one needed the ‘sectsty’ package and had to install additional ports for that.

sudo port install texlive-latex-extra

If you are looking for a specific LaTex package but don’t know which port it is in grep the list of packages.

After the installation you will be able to compile LaTex documents with the following command:

xelatex input.tex

The default out put is PDF, so if you want something different, go and check the manual.

Gotchas

XeTex assumes that you will feed it UTF-8 characters natively so the above-mentioned encoding hacks won’t be necessary and will trip up XeTex. Just remove lines like these:

\usepackage[utf8]{inputenc}

No Comments

Browsers seemingly adding extra padding below image tags

December 12, 2010

Today I found out about a little CSS quirk/feature (haven’t quite decided yet): Image tags are being assigned a seemingly undeserved 5px of padding-bottom. The weird thing is that this does not show up as padding in Firebug/Web Inspector – my CSS reset had already set it to 0. The image probably illustrates better what I mean – see the little extra space below?

Well, it turns out that images are inline elements, which means they have extra space at the bottom for the letter ‘tails’. These are the lines that go a little lower than the rest in such letters as y, p or q.

The solution is to apply display:block to the images in question.

No Comments

Running an arbitrary command whenever a file in the current directory is saved

October 10, 2010

I’ve been brushing up my CV recently and this time have made the effort to do it in Latex. I usually do Latex stuff with vim as my text editor and and excellent PDF reader for OS X called Skim.

vim and Skim tiled

Skim is able to detect, when the currently loaded PDF is being changed on disk and can automatically reload it. However, one slightly annoying problem I encountered was that I had to Alt-Tab to a terminal and run the Latex compiler after saving the .tex file. What I really wanted was to be able to run the compiler automatically when a file in the current directory is changed.

runonsave.py

Trusty old Python came to the rescue: I wrote a little script that recursively  scans the current directory every 5 seconds and executes an arbitrary command, when a file has been changed since the last scan.

You can install it with the following one liner (I’m assuming ~/bin exists and is on your PATH):

cd ~/bin && wget http://github.com/lenniboy/runonsave/raw/master/runonsave.py && chmod +x runonsave.py

If you want to watch the current directory for changes and and then run the Latex compiler simply do a

runonsave.py pdflatex cv.tex

This works with any command – not only with Latex. One other use case I could think of was regenerating your image sprites when you have saved an image and all sorts of other asset packing.

The script also automatically ignores common SCM folders. Just be careful if you are watching a huge directory tree; in this case you probably want to lower the time between scans. (At the moment this time is hard-coded in but I’m planning to use optparse in the future).

The script also has a repository on Github. If you have any improvements, for example using a better way to figure out if something has changed, go ahead and fork it.

7 Comments

Adding multiple photos to a Rails model using attachment_fu

September 6, 2010

Useless preamble

This weekend I finished off my own little Hello World mini-CMS, that I wrote in order to learn Ruby on Rails. The last part meant adding an image uploader, that would allow users attach an image to a page. There are two popular image uploader plugins for Rails: The slightly older, more complicated and feature-rich attachment_fu and the more nimble paperclip.

Paperclip seems to have the limitation that it only allows one attachment per model instance. On the other hand, you don’t need to create a separate model for your attachments. For this project I absolutely needed multiple attachment per page so I went with attachment_fu. I also didn’t want a separate form for uploading images, which would mean having to later associated the image with a page – I wanted to be able to upload from the page’s editing form. This case doesn’t seem to be covered well in attachment_fu’s documentation, so this is an attempt of closing this gap.

Installing the requirements

You will have to install an image processor. This is described in many other blog posts so I won’t regurgitate it here. I personally went with ImageMagick and rmagick. Seems to work fine.

Once you’ve done that you obviously have to install the plugin itself with:

./script/plugin install http://github.com/technoweenie/attachment_fu.git

Edit: Rails 3 has been released shortly after I wrote this post and this plugin doesn’t work anymore. However there is an alternative branch on Github, which you can install with:

Be warned though that you can’t

Setting up the models

You will need to use a separate model to store all the attachment meta data. I have called mine Photo but that name is arbitrary – call it what you want. So, lets build a migration:

class AddPhotos > ActiveRecord::Migration
    def self.up
        create_table :photos do |t|
          t.column :parent_id,  :integer
          t.column :content_type, :string
          t.column :filename, :string
          t.column :thumbnail, :string
          t.column :size, :integer
          t.column :width, :integer
          t.column :height, :integer
          t.column :article_id, :integer
        end
      end
 
      def self.down
        drop_table :photos
      end
end

Here’s the model class. Also, read up on the official docu about the all the possible options – the plugin is really quite flexible.

class Photo > ActiveRecord::Base
 
  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :max_size => 2000.kilobytes,
                 :resize_to => '500x500>',
                 :thumbnails => { :thumb => '215x215>'}
 
  validates_as_attachment
 
  belongs_to :article
end

Controller & form

A lot of tutorials say that you should set up your own controller for the image upload. But that would mean that you have to use a separate form for uploading images. What I wanted to do was to also use the ordinary page editing form for image uploads. So, I found a forum post that put me on the right track and after a bit more of trial and error I figured it out.

First, you need to slightly edit the form where you want to upload the image from. It needs to be a multipart form and you need to add a file field.

With the file_field_tag part you are telling Rails that it should not put the photo attachment in the main form object but rather create a second hash called photo. In the controller we will be reading out exactly this hash and store it in the photo model. So, here is the controller code:

class ArticlesController < ApplicationController   
 
	def update     @article = Article.find(params[:id])     
		respond_to do |format|       
			if @article.update_attributes(params[:article])           
			if params[:photo]              
				puts "Photo found"              
				# read out the POSTDATA hash 'photo' and try to create a photo 
				# also associate it with the article
				@article.photos.create!(:uploaded_data=>params[:photo]) #if image.size != 0
          	end
          format.html { redirect_to(@article, :notice => 'Article was successfully updated. [PUT]') }
          format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @article.errors, :status => :unprocessable_entity }
      end
    end
  end
 
end

I couldn’t find a good tutorial on how this is done so I hope someone wanting to do the same will find this page. Happy coding.

No Comments

Thoughts about Rails from a Django guy

July 25, 2010

My first and still my favourite programming language is Python and Django has so far been my framework of choice for my personal projects. Nevertheless, when a good friend of mine scrounged a free website off me a little while ago, I didn’t do what I normally do when friends ask for freebies: setting up another instance of WordPress on my server and let them choose a free template. Instead I decided to write my own CMS for his website using Rails.

I had read and heard lots of praise about Rails and was planning to add another skill to my list. I thought to myself that if I’m doing him a favour he can put with the slow speed of a developer  learning a new language and framework.

Whilst working through the documentation and tutorials I couldn’t help comparing Rails to Django, since it is the framework I’m most comfortable with and it takes a little time to unwire your assumptions and expectations of how a web framework ought to work. So, basically this is a one-sided mini-review of Rails.

The Ruby language

I didn’t know any Ruby before this project but everybody knows these days, that the blogging engine is the new ‘Hello World’. Ruby and Python are more alike than they’re different. Both are interpreted, use duck typing and both impose little structure on the source files. Ruby is slightly less readable to me due to the following things

  • too many sigils
  • multiple possible function/method calling syntaxes
  • the block syntax – it took me a little while to get used to but I’ve grown rather fond of it

I’ve read that Ruby lacks the amount of non-web libraries that Python has. But that doesn’t bother me since I almost exclusively do web stuff. I’m simply not clever enough to have a use for for SciPy and NumPy.

What I don’t like about Rails

ActiveRecord

The abovementioned project obviously didn’t need a really complicated data model: a handful of entities with some simple many-to-ones. Particularly because ActiveRecored markets itself as a simpler solution to heavyweight enterprise ORMs like Hibernate, I found setting up this schema surprisingly difficult. Compared to Django, Rails introduces a few new concepts, which took me little bit to get my head around it.

  • separation of schema and model: in Django the model is the schema and I couldn’t really understand why those two things should be separate
  • migrations: I can see how this could come in handy but in my case this was yet another extra thing I had to keep tabs of
  • if you have a many-to-many relationship you will have to define a join table yourself; in my view this is exactly the type of thing that an ORM at the abstraction level of ActiveRecord should take care of

Particularly due to that last point I kept thinking that ActiveRecord is just SQL rewritten in Ruby.

Templating

Using pure Ruby in .erb templates surely is powerful but to me smells of Java Scriptlet, doesn’t it? I subscribe to the view that the template language is for designers and should only allow safe constructs. Not really a biggie, but rather a little quirk.

No built-in admin

This is something I love about Django and find kind of a deal-breaker with Rails. Django gives you great looking admin interfaces for editing your data out of the box. It takes you 95% of where you want your admin area to be and I myself never had the need to customize the template. I hear that with the introduction of the newforms library it is now not so hard anymore to write your own admin views. All in all, I’m pretty surprised that Rails hasn’t even got anything remotely similar. (Maybe I have given up looking too soon? Let me know in the comments.)

What I like about Rails

Directory structure

Rails is pretty good at giving you a feeling of where your files ought to be in the directory structure, by neatly giving you a controller per model. Also, I quite like the distinction between the top level folders config, app, db, test etc. This is, in my opinion, something of a weak point in Django, where I never quite understood where stuff is supposed to live. Yes, you say that you should be separating your code into individual Django apps but I think that is the wrong abstraction level and like the concepts of plugins somehow better. That might be the Java developer in me speaking – a gem is much more like a JAR.

Dependency management

It’s great that you can specify the needed gems for your application and even tell the runtime that you need a specific version of Rails. I haven’t tried it but it seems that the needed gems are automatically installed if they aren’t already. Managing your dependencies is kinda non-existent in Django.

Grass isn’t always greener

Well, I don’t really know what I expected but Rails does not magically solve all problems and does not trivialise web development. On the other hand I wasn’t unhappy with Django – I just wanted to expand my horizon.

Rails certainly boosts your productivity but I found a few things, mostly around ActiveRecord, a bit strange and counterintuitive. I can’t say that I have fallen in love with Rails but it is a solid framework worth its popularity. Bear in mind that this is me speaking after using Rails for about 2 weeks – I’m sure I have only scratched the surface of the things that Rails can do for me; I hear that the testing and deployment tools are fantastic. Maybe I’ll do a follow up post on how my view changed after I used them.

40 Comments

taglibdoc-ng – JavaDoc for JSP tag libraries

April 8, 2010

Recently at work I had to write a set of JSP tag files (as in .tag files, not Java classes) for our designers to use. Naturally, when you write software for someone else to work with you need good documentation. At first I used Sun’s tool for generating the JavaDoc for those tag files.

Soon, however, I discovered that it had a few bugs: It was borking non-ASCII characters. And there didn’t seem to be a way to exclude certain folders so it was often listing tags twice in the resulting documentation. Another gripe I had (which is also the case with the regular JavaDoc tool) was the extremely ugly mid-90s default stylesheet. The tool seemed to have been abandoned (last change in 2005), but I managed to checkout a working copy from their CVS. Stupidly, dev.java.net requires an account to simply checkout code, but luckily I found account details on bugmenot.

The tool

Weirdly the code didn’t compile straight away as two classes were missing, but I just rewrote them. I also added a nicer stylesheet, which I pinched from JBoss.

Screenshot of taglibdoc-ng

The character encoding issues simply went away by recompiling the project with Java 1.5 compatibility settings. I also put the code under Mercurial version control (screw CVS) and uploaded it to my bitbucket.

Getting it

Just go to taglibdoc-ng’s page on bitbucket and read the technical details. Or you simply head over to the downloads page and grab the latest JAR. I have plans to put it up on Maven Central, but to be honest have never done that so will have to find out how easy that is.

No Comments