Thursday, July 2, 2009

How to find character encoding of a text file

Is it UTF8, ISO-8859-1, or UTF16-LE? How do you know which encoding a text file uses?

Unfortunately, it's not so easy to find out. Unicode has been around for more than 15 years and there is one program called Excel that still exports CSV files in its own fancy ISO-8859-1 (Latin-1) encoding that is bound to mess up pretty much anything if you have some characters not in the US keyboard. But of course you didn't know what encoding the CSV file is, even after almost pulling your hairs off googling for answer.

So, why bother about encoding? It's because my program can't read it if it's not converted to UTF-8. After trying many text editors hoping that it has a feature to show the current encoding, it finally dawned on me that there is this thing called Firefox which seems to recognize a file's character encoding.

Here's how to do it. Open the file in Firefox, then go to View, then choose Character Encoding. The selected one should be the encoding of the text file.

Firefox character encoding screenshot

Saturday, April 18, 2009

Standalone autotest with Rspec 1.2.0

If you're looking for how to do autotest with Rspec for standalone project, you might end up with this old article from 2007. But sadly, it doesn't work anymore with the latest Rspec (1.2.0).

After angrily scratching your head for why it doesn't work, you might dive into the source code and eventually solved the problem by writing some code. Then you found out that it's already supported by Rspec, no extra code required and you wondered why you didn't think of that. After all, if the problem seems common enough, somebody must have solved it. (Replace you with I)

And how do you do it? Let's dive in.

  1. Install Rspec and ZenTest.

    gem install rspec
    gem install ZenTest

  2. Set up Rspec.

    Create spec folder at your project root and put your spec files inside.

  3. Run autospec.

    Type autospec at your project root and it's done. I thought it's autotest and ended up wasting time figuring out why it doesn't work.


That's basically all, but there is more.

Adding color and other spec options


Just create spec.opts file inside spec directory. Fill it with some options, for example:

--color
--reverse

You can see the list of options available by executing spec --help.

Scan spec files within subdirectories


For one, the default behavior only scans spec files inside spec directory non-recursively. So if you like to organize your spec better, you're out of luck. But here is how to do it. In .autotest file in your project root, add the following code:

module Autotest::RspecMod
Autotest.add_hook :initialize do |autotest|
# Map to all "*_spec.rb" files inside spec and its sub directory.
regexp = %r%^lib/(.*)\.rb$%

autotest.remove_mapping(regexp)
autotest.add_mapping(regexp) do |filename, m|
autotest.files_matching(%r%^spec/.*#{m[1]}_spec.rb$%)
end
end
end


Pop up notification on Ubuntu


If you're on Ubuntu, you can follow these steps to have pop up notification when the test is finished. First, make sure you have libnotify-bin by running:
sudo apt-get install libnotify-bin


Then add the code below in .autotest.

require 'autotest/redgreen'

module Autotest::GnomeNotify
# Time notification will be displayed before disappearing automatically.
EXPIRATION_IN_SECONDS = 3
ERROR_ICON = 'gtk-dialog-error'
PENDING_ICON = 'gtk-dialog-warning'
SUCCESS_ICON = 'gtk-dialog-info'

# Convenience method to send an error notification message
#
# [title] Notification message title.
# [message] Core message for the notification.
# [icon] An icon filename or stock icon to display.
# [urgency] The urgency level (low, normal, critical).
# [time] The timeout in milliseconds at which to expire the notification.
def self.notify(title, message, icon, urgency='low', time=(EXPIRATION_IN_SECONDS * 1000))
`notify-send -i #{icon} -u #{urgency} -t #{time} '#{title}' '#{message}'`
end

Autotest.add_hook :ran_command do |autotest|
results = [autotest.results].flatten.join("\n")
output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/)

if output =~ /[1-9]\d*\spending?/
notify 'PENDING:', "#{output}", PENDING_ICON, 'normal', 5000
elsif output =~ /0 failures/
notify 'PASS:', "#{output}", SUCCESS_ICON
else
notify 'FAIL:', "#{output}", ERROR_ICON, 'critical', 10000
end
end
end

Monday, March 30, 2009

The Genius of SMRT

Here is an example of why you need to think.

Long time ago, there used to be a drawing that showed how long it took to travel from one station to another by Singapore's MRT.

This is how it looks like:


And there is a newer version, which looks like this one below:


Using both maps, tell me how long do I need to go from Bukit Gombak to Woodlands? Now you know that the newer version simply serves no purpose whatsoever to help people other than the fact that the timing seems to be updated and more current.

That, is the genius of non thinking.

The original document is still available on here though I can't find it linked from anywhere in the site. And there is a fancier, overkill one from TransitLink.

Thursday, March 26, 2009

Delicious screws it up, again

So one mistake in the past is not enough.

Now the Delicious toolbar is as clean as toilet water, annoying, but still can live with it. And guess what, the Awesome Bar is screwed too.

And the worst thing is, I can't downgrade! It kept coming up with "Invalid file hash" error. Great!

I'm talking about the Firefox add-on, version 2.1.032.

Update: Managed to downgrade to 2.1.018 and it works perfectly.

Friday, March 6, 2009

Why bureaucracy

... the purpose of bureaucracy is to compensate for incompetence and lack of discipline — a problem that largely goes away if you have the right people in the first place. (GOOD TO GREAT - Jim Collins)

I've never thought of it that way but it makes a lot of sense. Bureaucracy seemed to me like something that naturally evolves to manage people, especially many people. And now I know the why.

Thursday, January 15, 2009

Resizing animated gif on attachment_fu's Rmagick

So your animated GIF doesn't animate after going through attachment_fu's Rmagick resizing?

Go find the file lib/technoweenie/attachment_fu/processors/rmagick_processor.rb inside plugins/attachment_fu.

Then find this part:

def with_image(file, &block)
...
binary_data = file.is_a?(Magick::Image) ? file : Magick::Image.read(file).first unless !Object.const_defined?(:Magick)
...
end


and change Magick::Image.read(file) to Magick::ImageList.read(file). The result is:

def with_image(file, &block)
...
binary_data = file.is_a?(Magick::Image) ? file : Magick::ImageList.read(file).first unless !Object.const_defined?(:Magick)
...
end

Wednesday, January 14, 2009

Cooperation and punishment

Apparently we need to introduce an element of punishment to get people to cooperate, but this is only good in democratic societies.

In countries like the USA, Switzerland and the UK, freeloaders accepted their punishment and became much more co-operative. But in countries based on more authoritarian and parochial social institutions such as Oman, Saudi Arabia, Greece and Russia, the freeloaders took revenge — retaliating against those who had punished them. - http://www.physorg.com/news124046352.html