Archive for June, 2008

New Version of URL Lister Firefox Add-on

Monday, June 30th, 2008

URL Lister Logo

Following the release of Firefox 3, I have updated my URL Lister firefox Add-on. Now it works in Firefox 3 - and I have added a new feature.

For those unfamiliar with URL Lister, its a firefox add-on that shows the URLs of all the open tabs in a textarea so that it can be copied easily. It is my first firefox add-on.

URL Lister Screenshot

Unfortunately, there is a problem with the mozilla add-ons listing page - I cannot install the extensions using that page. I am getting this error…

Firefox could not install the file at 

https://addons.mozilla.org/en-US/firefox/downloads/file/32487/url_lister-1.1-fx.xpi

because: Invalid file hash (possible download corruption)
-261

However, you can right click the ‘Add to Firefox’ button and choose ‘Save Target As’ and download the file without any issues. Then you can install the extension by dragging the downloaded file into a firefox window. That works perfectly - but not the direct install method.

Firefox 3 - World Record and Downed Servers

Tuesday, June 17th, 2008

As many others from around the web, I too am waiting for the latest iteration of the Firefox browser - Firefox 3. And again, like many others, I too am participating in the World Record attempt. Unfortunately, even though the release time is upon us, I cannot download the software - because their server is down!

As a linux user, I have come to expect the fact that servers will collapse when a new version of a distro is released. But those are big files that are transferred(the linux images will be anywhere between 600 MB to 4 GB). But in case of firefox, a 8 MB file managed to bring top servers to their knees. A testament to the popularity of firefox - although I wish mozilla had more mirrors.

If you cannot wait to get the latest version, get it from their FTP servers - they are functioning normally. But, those downloads will NOT BE COUNTED in the final tally for the world record.

Related Links

Hello Script for Java

Wednesday, June 11th, 2008

After the Hello Script for JavaScript, here is the Hello Script for Java. ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

Warning: I am NOT an expert in Java - I am just a beginner. There may will be errors(bad programming methods - not compiler errors) in the following script. If you notice any such issues, please point them out in the comments.

Code

If you want to run the code, save it to a file named ‘Hello.java’ and compile in using the command ‘javac Hello.java’. After that you can run the code using the command ‘java Hello’.


import java.io.*;
import java.util.regex.*;

public class Hello {
	public static void main(String[] Args) {
		// Printing(IO)
		System.out.println("Hello World");

		// Variables, concatenation
		String name = "Binny";
		int year = 2008;

		System.out.println("Hello, " + name + " - welcome to " + year);

		if(year > 2008) {
			System.out.println("Welcome to the future - yes, we have flying cars!");
		} else if(year < 2008) {
			System.out.println("The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!");
		} else {
			System.out.println("Anything wrong with your time machine? You have not gone anywhere, kiddo.");
		}

		// For loop
		int i=0;
		for(i=0; i<3; i++) {
			System.out.println(i + ") Hi there!");
		}

		//Numerical Array, While
		String rules[] = {"Do no harm", "Obey", "Continue Living"};
		i = 0;
		while(i<rules.length) {
			System.out.println("Rule " + (i+1) + " : " + rules[i]);
			i++;
		}

		// Associated array, foreach
		// Hmm - does Java have Associated arrays? ArrayList?

		String csv_string = "hello,world,how,are,you";
		String csv_values[] = csv_string.split(",");
		// No native Join method

		// Function, argument, return, call
		System.out.println(Hello.hello("Binny")); //The function definition is at the end of this file.	

		//Class stuff...
		Movie ncfom = new Movie("New Country for Old Men"); //It's a sequel!
		ncfom.printMovieDetails();

		// File IO
		// File reading, easy method...
		try {
			File read_file = new File ("/tmp/Hello.txt");
			FileReader in_stream = new FileReader(read_file); // Create a Character Input Stream
			BufferedReader in = new BufferedReader(in_stream);// Filter the Input Stream - buffers characters for efficiency
			try {
				System.out.println(in.readLine()); // read the first line
			} catch(IOException E) {
				System.out.println("No idea what went wrong. Sorry!");
			}
		} catch(FileNotFoundException E) {
			System.out.println("File not found. Sorry!");
		}

		try {
			// Writing to a file
			File out_file = new File("/tmp/HelloJava.txt");
			FileOutputStream out_stream = new FileOutputStream(out_file); // Create an Output Stream
			PrintWriter out = new PrintWriter(out_stream); // Filter bytes to ASCII
			out.println("Hello, from Java"); // Here we actually write to file
		} catch(java.io.FileNotFoundException E) {
			System.out.println("File not found. Sorry!");
		}

		System.out.println("\nLS command results...");
		// Command Executing
		try {
			// Execute a command
			String command = "ls";
			Process child = Runtime.getRuntime().exec (command);

			// Read from an input stream
			InputStream in = child.getInputStream();
			int c;
			while ((c = in.read()) != -1) {
				System.out.print((char)c);
			}
			in.close();
		} catch (IOException e) {
			System.out.println("Error");
		}

		System.out.print("\n");
		//Regular Expression
		String str = new String("Hello World");

		//Find a pattern
		Pattern hell_check = Pattern.compile("^Hell");
		Matcher matches = hell_check.matcher(str);
		if(matches.find()) System.out.println("Yup - its evil");

		//Replace
		System.out.println(str.replaceAll("l([^l])", "$1")); //Remove an 'l' from both words. Should print 'Helo Word'

	}

	//Function declaration.
	private static String hello(String name) {
		return "Hello, " + name;
	}

	// One for the OOP fanboys - Class, members, object and stuff.
	private static class Movie {
		public String name = "";
		public int rating = 0;

		public Movie(String name) {
			this.name = name;
			this.rateMovie();
		}

		public void rateMovie() {
			this.rating = (this.name.length() % 10) + 1; //IMDBs rating algorithm. True story!
		}

		public void printMovieDetails() {
			System.out.println("Movie  : " + this.name);
			System.out.println("Rating : " + this.rating);
		}
	}
}

Next Hello Script - C

Python Reference Manual has a lot to Learn From PHP

Wednesday, June 4th, 2008

Unlike the other scripting languages I work with(like PHP, Ruby, JavaScript, etc.), I am not comfortable using Python. I have made some stuff in Python(for example, frees) - but I could get into the flow as easily as with other languages. For the longest time, I thought it was because of the whitespace issue. But recently, I created a small Python script - that’s when I understood that whitespace does not concern me. What make me angry at python is its documentation/manual.

By Python Manual, I mean the official Python Manual available at http://docs.python.org/. I have downloaded the entire manual to my system as HTML files and use that as a reference when working with Python. This is what I have done for all the other languages I work with…

Finding a Function

There is one big problem with using HTML files as your reference - you cannot search through it. So I try to find a page in the documentation that lists all the functions in a single page. When I need to find a function, all I have to do is search through this page. These pages in the documentation serves this purpose…

What about Python? Well, Python don’t have such a page in their manual. The nearest one I could find a combination of four pages - Module Index + Library Index + Tutorial Index + Language Index. Its no where near as useful.

Even if you are online(remember, many people are not connected all the time), its still not easy to find a function in Python documentation - even with searching capabilities. Don’t believe me? OK - go to Python Docs and try to find the documentation for the function that, say, reverses an array. Now go to the PHP site and do the same.

Reference Formats

Anyway, I cannot really complain about the the availability of the ‘function page’. Its just the way I prefer - other people may not want such a page. But I can complain about the fact that they don’t provide a CHM file in their downloads section. CHM solves the problem as you can search through them easily.

PHP, on other hand, provides the CHM file. This makes the PHP manual much more easier to use.

Even though Python don’t officially provide a CHM file, others have made it available on the net. Its just a google search away. But I prefer to get these stuff from an official source.

Level of Detail in the Documentation

The worst sin of the Python manual is that the documentation is not detailed enough. To better understand this, let’s take an example - say the array reversal function. First we take a look at the Python documentation for this function

reverse()
Reverse the order of the items in the array.

One line. That’s it! If you are not well versed in Python, you will have no idea of how to use this function - because there is no example. Most people will expect that the array must be given as the argument. But the argument list is empty in the documentation. That’s because the array is not passed as an argument - rather, in Python, this function is a member function of the array object - so the proper usage will be something like this…

x = [1,2,4]
x.reverse()
#Now x has the value [4,2,1]

I know that because I know Python - but if a new user manages to find the documentation for this function, he will be confused. Another thing - I am not saying that there are no examples in the documentation - examples are given for a lot of functions. But its no where near the level that PHP has achieved.

OK - now lets see the PHP manual for the same function - array_reverse(). There is an entire page for that function. Not just for this function - there is one page of documentation for every function in PHP. And there are examples. And at the end, there is a collection of ‘user notes’ - making each function much clearer. Beautiful!

I feel a bit guilty about comparing Python with PHP documentation - PHP has the best documentation in its class of languages. But still, Python has a lot to learn from PHP(I wanted to say that for a long time ;-) ).

Subscribe to Feed