Redesign 2011

It has been a while I didn’t change anything on the site.
For the past one month or so, I work on this on and off, locally, and finally now, finding the time to get the theme uploaded to the actual site.

You’ll still find plenty of rough edges, in fact rough edges is just an understatement, I think instead of waiting forever to get the redesign done, I will just upload it, and working on it at the side.

First thing you should notice is of course the use of font from typekit, it is lobster for the title, and Adelle for the rest.

So now I got to go back to work and improve it one small bit after another.

Exception handling in python

As I came across the exception handling chapter, both code below should work as stated in the book (The Head First Python book, which is indented for Python 3.X, but I am running this code in Python 2.X),

try:
  with open("man_data.txt") as man_file:
    print >>man_file, man
  with open("other_data") as other_file:
    print >>other_file, other

or

try:
  with open("man_data.txt") as man_file, open("other_data") as other_file:
    print >>man_file, man
    print >>other_file, other

However it seems only the first one is working under Python 2.X, later code will in fact throw up a syntax error message.
Didn’t see anything on the net mentioning this has to do with changes in Python 3.x, so not sure if that is the case.

Print without newline in Python

Going thru the Head First Python book, which is intended for Python 3.X, although majority of the content work just as well in Python 2.X.

First encountered with some variations between version 3.X and 2.X at the chapter about printing out nested lists, with tab indicating different nested level.

To print a tab without a newline, you could do this in Python 3.X,

print("\t",end='')

However, the same code will throw you a syntax error in Python 2.X.
So to do the same in Python 2.X,

print "\t",

Note the final comma, which actually make sure the line will print out with space instead of a newline.
In Python 3.X, print is an actual function but in Python 2.X, print is still a statement.

I found out this from the ever helpful stackoverflow

remote_user in rails application

I am developing a little application within instantrails, suppose to work in an intranet environment.

The application let user book a part number, by filling some information. For better user experience, I plan to capture the username at the other end that make the booking, instead of creating another layer of login feature and require the user to login again. There is no complex authentication needed, just capture the username and store it together with other information.

ENV['user'] will give you the username that has the rails application running, which is not what we want.
ENV['remote_user'] return nothing at all.
I read through quite a few discussions on the net, it seems like apache didn’t actually pass the remote_user value to mongrel, hence rails application couldn’t call out remote_user. To fix that, we will need to modify on the apache configuration file, httpd.conf, and .htaccess. Admittedly, I really know close to nothing about the setup of these files, and have a hard time following the suggested steps, as to where to add the suggested line of codes, which file to modify, etc, etc.

So I figure, if we forgo apache in the setup, will it be easier to fetch the remote_user value directly from mongrel? or from thin if it is the web server being used.

But still, I just couldn’t get it working.
Is there any gem or plugin need to be installed before this remote_user tingy can be pass to rails application?

thin on windows

Read about thin for a while (faster than webrick, faster than mongrel), but didn’t really get the chance to try it out, until recently.

As I am looking for moving my little rails application out of instantrails to a proper production environment, decided to give thin a spin, too bad Passenger isn’t supporting Windows platform, heard a lot of good thing about it too.

Installing thin server on Windows isn’t quite straightforward though. Thin actually consist of mongrel parser, EventMachine, and rack, but the latest EventMachine gem is not win32 binary release, that pose a lot of problem for the installation.

There are a few workarounds suggested on the net,
from Winston

  • install rack with the usual gem install
  • install EventMachine gem 0.8.1 as local gem
  • install thin gem install thin –ignore-dependencie

But this didn’t quite workout for me, some suggested to install MinGW and MSYS, then the normal path of gem install thin should just work. Tried that, but fail as well.

Then found out that EventMachine has a binary release at 0.12.6, and with that it works right away for me, here are the steps that did it for me,

  • gem install rack
  • gem install eventmachine-0.12.6-x86-mswin32-60.gem (download gem to local, and install locally)
  • gem install thin

Only after a full circle of searching around the net, that I found out these steps are actually mentioned in the comments by Marty McGee in winston’s original post. Duh.

bubble sort

Understanding sorting algorithm by just reading text or still illustration takes a lot of mind gymnastic. Apparently, there are lot of videos online, trying to explain the concept. But not all explain it as good though, some with really boring animation, some with crappy background music, and some even both . Here are two that I find did a great job in explaining bubble sort,

This one from codegearguru. Instead of creating graphic, or cutting out paper to represent data in the sorting process, just use the poker cards! That seems obvious now, but I didn’t think about it when I try to illustrate sorting to myself initially. Ha. Think there is a series of video created for different type of sorting algorithm as well, so definitely a great tool to understand sorting algorithm. Bonus near the end of the video, another technique called shaker sort or cocktail sort is also explained, which is basically a bidirectional bubble sort which should speed up the sorting speed.

Another one from Miles Hauskaz, just plain simple and succinct explanation and a video nicely done as well.

Sorting Algorithms – Bubble Sort from Miles Hauskaz on Vimeo.

So, this is my implementation of bubble sort, admittedly not the best that you can find, but that will do for me now, until I have time to come back and do some refactoring.

arr = %w{g a c b j e e}
i = arr.length - 1
count = 0
n = arr.length - 1
def swap(m,n) 
  arr = []
  if m > n
    arr << n
	arr << m
  else
    arr << m
	arr << n   end end while n > 0
  while count < i
    puts "comparing #{arr[count]} to #{arr[count+1]}"
    a = swap(arr[count],arr[count+1])
    arr[count..count+1] = a
    count += 1
  end
  n -= 1
  i -= 1
  count = 0
end
p arr

map and each

It is a bit difficult for me to understand the difference between map and each, until I found out this in irb accidentally,

irb(main):001:0> name = %w{guido knuth adrian dhh pg}
=> ["guido", "knuth", "adrian", "dhh", "pg"]
irb(main):002:0> name.each {|n| n.upcase}
=> ["guido", "knuth", "adrian", "dhh", "pg"]
irb(main):003:0> name.map {|n| n.upcase}
=> ["GUIDO", "KNUTH", "ADRIAN", "DHH", "PG"]

map and each are suppose to do almost the same thing, but why each didn’t return the correct result in uppercase like what map did?

The different is each yield each element in the collection to the code block, in this case perform the upcase method, but return the receiver, which is the original array, hence an array of lower case names.

Whereas, map return a new array, consists of elements already went through the code block, hence an array of upper case names.

irb(main):007:0> name.each {|n| p n.upcase}
"GUIDO"
"KNUTH"
"ADRIAN"
"DHH"
"PG"
=> ["guido", "knuth", "adrian", "dhh", "pg"]
irb(main):006:0> name.map {|n| p n.upcase}
"GUIDO"
"KNUTH"
"ADRIAN"
"DHH"
"PG"
=> [nil, nil, nil, nil, nil]

So here you can see with p, both map and each output the uppercase names, but look more carefully, the return value of each, as expected, is the original array. map however, return an array of nils!

Why is that?