Sunday, March 30, 2008

Getting down and dirty with Python

I have spent the better part of this weekend getting down and dirty with python. Also I found the GUI library I was looking for in venster. Last night I was reading about the different GUI GUI development options available for Python. There are the the usual libraries like wxPython, GTK, QT, Tk, FLTK, Swing, SWT, Swing and .net forms. All of them are good but the main problem with most of them is that they are huge and the others which are small don't look or feel native. Out of all of them I like SWT but then again if I am going to use SWT then why not just use Java.

Venster looks like a very good project but seems to be dead for the last 4 years and if I can get some time I might want to work on it. The library is tiny and does not add much weight to the over all deployment package.

But then again its still my second day and I've barely scratched the surface. The tutorial I was using was a little sketchy about classes in python so I still need another reference for classes in Python. So far I like the language, not sure till when this love will continue. The ease of use and the community behind the project is beyond what I've seen for other languages. I still have to look at the multi-threaded support in python. Like I said I am still stuck in the basics will need to get into the advanced topics before I go off to sleep today.

Saturday, March 29, 2008

Finally learning Python

I've tried my hand with Python a couple of time but it could never really hold my interest. But over the last few years its matured a lot and its available on multiple platforms and also one of the only languages which seems to have a binding to nearly every new library that comes out.

Lets see how far I go on this adventure. And for how long python will be able to hold my interest, will I go beyond the basic "hello world" programs.

Thursday, March 27, 2008

Generating regular expression at runtime in javascript

I have never been good with regular expressions. I normally search for the regular expressions online and use it, I can never seem to get them into my brains. Lately I've been working on things where I've had to generate a regular expression at runtime. It is a trivial thing but I was unable to find it when I look for it. So in case you are looking for generating a regular expression at runtime this code snippet might help you out.

var data = "This is a string and I want to replace all the a's";
var re = new RegExp("a", "ig");
alert(data.replace(re, "m"));

This post is more like a reminder for me.

This time I can get Nineteen Million Five Hundred Thousand United State Dollars

As if the previous million dollars wasn't enough I have another 19 Million waiting for me. Well this mail landed in my Inbox and if I wasted 5 minutes going through it I normally put it on my blog as a reminder of wasting time, also this mail is funny with facts and fiction mixed together. Only read it if you have a lots of free time.

Good Day,

Thank you very much for your response, and your interest in this
transaction. Let me start by introducing myself again.I am Mr. PATRICK
K.W.CHAN I work with the Hang Seng Bank Ltd.Before the U.S and Iraqi war our
client Colonel Sadiq Uday who was with the Iraqi forces and also
businessman, made a numbered fixed deposit for 18 calendar months, with a
value of Nineteen million Five Hundred Thousand United State Dollars only
in my branch.Upon maturity several notices was sent to him, even
during the war which began in 2003. Again after the war another notification
was sent and still no response came from him. We later found out that
the Colonel along with his wife and only daughter had been killed
during the war in a bomb blast that hit their home.

After further investigation it was also discovered that Colonel Sadiq
Uday did not declare any next of kin in his official papers including
the paper work of his bank deposit. And he also confided in me the last
time he was at my office that no one except me knew of his deposit in
my bank. So, Nineteen million Five Hundred Thousand United State
Dollars is still lying in my bank and no one will ever come forward to claim
it. What bothers me most is that according to the laws of my country at
the expiration 5 years the funds will revert to the ownership of the
Hong Kong Government if nobody applies to claim the funds. Against this
backdrop, my suggestion to you is that I will like you as a foreigner
to stand as the next of kin to Colonel Sadiq Uday so that you will be
able to receive his funds.

MODALITIES:

I want you to know that I have had everything planned out so that we
shall come out successful. I have an attorney that will prepare the
necessary document that will back you up as the next of kin to Colonel Sadiq
Uday, all that is required from you at this stage is for you to
provide me with your Full Names and Address so that the attorney can commence
his job. After you have been made the next of kin, the attorney will
also fill in for claims on your behalf and secure the necessary approval
and of probate in your favour for the move of the funds to an account
that will be provided by you.There is no risk involved at all in this
matter, as we are going to adopt a legalized method and the attorney
will prepare all the necessary documents. Please endeavor to observe
utmost discretion in all matters concerning this issue. Once the funds have
been transferred to your nominated bank account we shall then share in
the ratio of 70% for me, 30% for you.
Should you be interested please send me your,

1,Full names,
2,Occupation
3,private phone number,
4,current residential address,

Via email: patrickchan233@yahoo.com.hk

And finally after that I shall provide you with more details of this
operation.

Your earliest response to this letter will be appreciated.


Kind Regards,

Patrick K.W. Chan.

Tuesday, March 25, 2008

Open Social Container and my first Facebook Application

Its the last day of my easter vacations and five days of good work. I had initially planned to go out somewhere for these holidays but as nothing really worked out I ended up staying at home. So instead of wasting my time sleeping and watching TV (which I still ended up doing) I thought of finishing my ever growing list of personal projects. But instead of starting work on one of the pending projects I ended up starting yet another project. I searched around for an open social container and I found one but it was in Java and since I don't have any server which would allow me to host the project I ended up working on my own open social container.

Well I worked on it for two days and it at least works with most of my samples I created for open social and also a few other gadgets. As of now its completely in JavaScript so in terms of security there is none to speak of, but by the end of it I did make a container and learned quite a lot. The code still looks way to hacked up to put online but I am not so sure anyone would even want to see the code in case you want leave a comment and I will clean up the code and put it up for download.

That was one part of my work the other thing I did was to find a way to run Open Social applications on facebook. Since I really did not have any idea I stared out with a application to host the comics I like http://apps.facebook.com/ezcomics/. If you see the latest version of the application its just a silly little iframe. I just did not have the time to learn and unlearn JS, SQL and XHTML for the sake of FBJS, FQL and FBML. Its simple to get but like I said I had no concrete application in mind so thankfully did not start with yet another project. Just tried to get the comics app running on facebook. In a weird way it does though it looks nothing like a facebook application but like anyone is ever going to use it. If you are looking out for a cool project why not make a bridge that allows you to run open social applications on facebook.

I would love to make such an application but given the fact I have so much on my plate I don't want to start with one unless someone wants to pay me for doing it. This also reminds me I need to complete my blogging application. I simply hate the blogger online editor.

Are you smarter than a 5th Grader?


After writing about the Indian version of the show I took the test on the fox network site. And I have the certificate to prove it ;). Thought I don't live in New York and I took the test in 2008 and not 2007. Apart from those mistakes I am so much smarter than a 5th grader, or am I?

Just made me realize how little I know or care about social studies and history. I always ended up taking questions about science and maths. The two three times I choose social studies or history I ended up giving the wrong answer. The concept of the show is nice and would go well with the Indian audience who seem to be having an overdose of the reality tv shows, and one more would not hurt.

Kya aap panchvi paas se tej hai?

UPDATE: You can play the game online at http://www.fox.com/areyousmarter/features/. I played it and here are my results.

कया आप पांचवी पास से तेज हैं? / Kya aap panchvi paas se tej hai? is an Indian version of Are You Smarter than a 5th Grader? and would be hosted by Shahrukh Khan. Now I am not really talking about how great the program is going to be or going to harp about the host. I just like the clever marketing technique by the company developing the show in India. In most states in India lottery is illegal, but paying money using premium SMS is not.

These days they have this clever little advert on the Star Network channels which asks users to SMS the answer to a simple question to get on the program. Now the adverts been playing for a month with a new question everyday. And they entice you with a million dollar prize. You need to send the SMS on a premium number, so its almost like an underground lottery with dreams of a million dollars. Even if they get a million SMS's a day thats roughly 5 million rupees a day without giving out a thing, now thats really clever.

With a population of more than a billion getting a million SMS's is not a big deal when you can get a chance at winning a million dollars. In case you've already SMS'ed the answer of the day, one this is for sure you got into the marketing hype and it just goes to prove that you are not really smarter than a 5th grader.

Saturday, March 22, 2008

A better alternative to window.onload

This code can be used as a better alternative to window.onload. We use main as the function from where the execution begins. We also have a __app_started varible which tracks if main has been called so that it is not called more than once. Basically by this technique main gets called the moment body has been completed. If you use window.onload it waits till all the images and CSS have been loaded and in most case that can really be a while.
var __app_started = false;
if(document.addEventListener) document.addEventListener("DOMContentLoaded", main, false);
else
{
document.write("<scr" + "ipt id=__ie_onload defer src=javascript:void(0)><\/scri" + "pt>");
var script = document.getElementById("__ie" + "_onlo" + "ad");
script.onreadystatechange = function()
{
if(this.readyState == "complete") main();
}
}

window.onload = main; // If all else fails

function main()
{
if(__app_started) return; // We don't want to call main more than once.

//App logic goes here

__app_started = true;
}

#include or include a file in javascript at runtime

I had written about require or #include in javascript and it works but sometimes you don't want it to be added it using DOM. This function uses DOM incase the body has been loaded or uses document.write to load the file.
function $include(url) 
{
if(document.body)
{
var script = document.createElement("script");
var head = document.getElementsByTagName('head').item(0);
script.src = fileUrl;
head.appendChild(script);
}
else document.write("<script type='text/javascript' src='" + url + "' ></sc" + "ript>");
}


If you look carefully the name of the function is $include. It just makes it stand out other than that you could name it whatever you want. This just makes the code really clean and no more messy html files with 10 script tags to load all the files. It just makes life simpler if you have too many script files to load.

Friday, March 21, 2008

"Hello World" ruby and python on dreamhost

After having dropped all my plans for a vacation I have nothing better to do so I started with the stupid idea of trying to run different languages on my dreamhost account. Let me start with ruby first.

Open up your favorite editor and copy paste the code below. Save it as testruby.cgi on your server. Change the permissions for that file to chmod 755 testruby.cgi.

#!/usr/bin/env ruby
require "cgi"
cgi = CGI.new("html3")
cgi.out("text/plain"){"Hello World"}

If all goes well you should see hello world. I tested it here. Once you've tested it out you know that your account supports ruby.

Now moving on to python. Copy paste the code below to a file testpython.py and change the permissions for that file to chmod 755 testpython.py.

#!/usr/bin/env python
import cgi

field = cgi.FieldStorage()
print "Content-Type: text/plain\n\n"
print 'Hello, world!\n'

If all goes well you should see hello world. Here is my python hello world script.

This is just one reason why I really like dreamhost. You can tinker with things I even tried to install mono but got bored mid way. There were a little too many steps plus in any case I have a windows hosting for that ;)

Incase you are looking for a web hosting and you are impressed by dreamhost use the promo code VJ97 to save $97 on any of their web hosting plan.

UPDATE: Come to think of it this post sounds like a cheap infomercial, but I really cannot help it.

Tuesday, March 11, 2008

Let me decide what WFM

Its really rare to see anyone who can actually think out of the box without their fixed set of ideas. If a person knows a "x" language or technology making that person think beyond it seems like an effort. Specially when you look at technology of any kind we have the language nazi's who will make every effort to protect their language as if they would die without it. So lets take an example of a Java programmer programming in JavaScript he/she will try and make sure that JavaScript works and feels like Java. There is no harm in it as long as that is not being preached as the only way of working. I tend to do that a lot (program like "x" language in "y") but I will never say that this is the only way of doing it. It just WFM, and I tend to learn from looking at other peoples code and changing my practice whenever I see something that I feel is better.

Then we have the Mac, Linux and till a certain point Microsoft fanatics. They seem to have their fixed set of ideas and will leave no stone unturned in trying to teach the holy gospel. The Mac fanatic will not stop harping about how his/her Mac is the shiniest object on this planet and how they plan to donate a few thousand dollars to the Apple Foundation by buying more stuff from them. Then there are the Linux fanatics who cannot stop praising the good word of "Open Source" and how every line of code that is produced should be "Open Source" and how the latest version of Ubuntu is 100 times better than Vista or some such statement.

As for the MS fanatics I've not seen too many of them lately so cannot say much about them. Coming back to the topic when we move away from WFM to preaching that is when things become annoying. Unless someone really asks you for your expert advise please don't start up like a broken record. Let people decide what is right for them.

Sunday, March 09, 2008

There is always so much to learn

After every few years I feel I know everything there is to know but along comes something new and my myth is shattered and I come back down to earth. In most cases its not even something new just something I've never bothered to look but something which actually changes my perception. Most of the times I work on multiple projects at a time and in the end the result seems to be zero. There is always that one thing that seems to hold back the complete project. Be it something small or something really big. But there is something bigger than these projects that I seem to get out of these personal projects and that thing is experience. I end up learning things that I normally think I know or seem too trivial to learn.

This weekend I was working on CSS and trying to get a native look and feel on a web page. I searched all over the net. But in the end the whole information was sketchy at best. I ended up learning a lot and finally figured out how little I knew about the topic and how little information was available on this topic. I might write something about it if I get time as its quite possible more people are searching on the topic. With the CSS3 UI guidelines still a few light years or months away this topic is very sketchy. Every browser seems to have a different way to achieve the same thing. Finally I was able to reach my goal with the same thing working in almost every browser but by the end of it completing my small little project seems like such a big task.

If I try the current solution I would have to write a separate CSS file for each and every browser that I plan to support eventually. Not a very good solution but if nothing else it works. Sorry no real code in this post but I will try and post something soon. I hope I release my project so that you have a reference point to look at.

Saturday, March 01, 2008

Just an ordinary day

Today began as just another day and somehow is ending that way. Nothing exciting or interesting, still have my TODO list which never seems to get completed. Just hoping that tomorrow might turn out to be a little more interesting.

The last two weeks have been hectic in every possible sense. First was my birthday which is normally a non event but this year for a change went out with friends and had a lot of fun. The photograph was clicked at the lake which I went last week. Have also been busy at work with tons of work to complete and somehow did complete it by the end of the week.