IdeaMonk

thoughts, ideas, code and other things...

Wednesday, September 30, 2009

PyCon India 2009!



Before I forget, I must pen down this energizing experience at India's first ever PyCon that happened in IISc Bangalore on 26th and 27th September, 2009. I also happened to find Amit from GSoC 09's Delhi meetup and @hnprashanth.
Day 1, we were a little late, "My adventures with Python" was a miss. I, along with Rohan and Prabhat moved to much needed "Introduction to Python". It cleared a lot of doubts I had and we tried out some List Comprehensions, Dictionaries, etc. I knew that xrange is faster than range, but I had not cared to explore why. The myth was busted when Noufal talked about generators which are like functions frozen in time, no other words can better explain them. I immediately remembered how cool was the Prime generator I had seen months back.
The next talk we attended was "Test driven development in Python", again an eye opener to quality code, which is less error prone and easy to maintain. I got bored by the time they compared different test frameworks, I guess for a beginner, knowledge of a concept is enough, the latter part of talk was beneficial to actual practitioners.
The next talk - Idiomatic Python and other language features - What an excellent talk on the language by Sykora. Like, did you know a try... except block could be faster than an if.. else in a loop. This has been tested by Sykora. Differences between upcoming Python 3.x and 2.x were clearly highlighted, and I personally loved the way Sykora presents.
I headed for "Django: A blessed webframework for perfectionists with deadlines", I guess it overflew schedule or something, I just saw the same talk over youtube yesterday, and have been waiting to try Django soon. Btw, I just realized I had seen the guy giving the intro somewhere on twitter, it happened to be @scorpion032 !
Coming up next was "National Mission on Education through ICT & Python", Wow! what an awesome plan! Imagine being actually able to use a computer to solve your daily problems, thanks to Python. MHRD has planned to embed in Python into college curriculum. They distributed Ubuntu live DVDs customized for Python, along with video tutorials on Python. Python being the only human readable programming language unarguably suits the purpose. Maybe I won't see such a day, but future generations would do :) It led me to think about why not Python at Schools, which has become an ongoing discussion on ilugb and bangpypers mailing list. So many are pissed off with non standard TurboC++ which belongs to pre-historic ages of 4kb RAM luxury as Robert Lafore put it. I personally feel Python is excellent replacement for GWBASIC.
Its sad that we missed those fossee.in T-Shirts, with that cool antigravity xkcd comic strip all over on the back of it. PyCon's T is too good enough. Well, that was day 1 of PyCon. The IISc area was so clean that we could'nt find any cigarettes at all. I guess we made Amit wait a lot at majestic while fagging, thanks to Rohan for engaging him over in-depth discussion on KDE and GNOME dev. Back home Prabhat was really excited about Python, and tried it out whole night. Btw, also found out bpython, which happens to be an excellent Python interpreter ( a mod of ipython actually ) for any beginner.

Day 2 was lighter, again we missed the first talk on Mayavi/TVTK. Road To InPyCon 2009, but Noufal was a much needed talk on how it all happened. I wish it was scheduled as last talk. The we headed over to Test Driven Development by @sai_venkat from TW. Yes, it was entertaining, but as I don't develop softwares as a profession, the latter part of the talk didn't make much sense. But it did make sense of Software Engineering as a subject, I've been questioning its existence ever since this semester started, now I don't for I can relate it to things in real life. Today's S/w Engg lecture on Unit Testing at college was much more interesting than usual.
Come Lightning sessions to be ended in BOF. Various lightning talks, Pyglet one worth the mention. Friends pushed me to show web2hunter and I wondered whats there to present in it. Having pushed over and over I had to yield, so we talked with Mr. Senthil who humbly gave me permission, and to my surprise, he actually knew about web2hunter! Ahh damn, having not spoken to a huge crowd ever was making me feel nervous. But I finally gave it, a short one for 10-15 mins I guess. Some found it really lightening, others found it neat. I still have to watch it again, I feel I was too fast while explaining the code, some people were yawning... but by end I could hear some loud claps, I guess that did it, 'taste of food lies in eating'. What I wished to stress was code-re-usability made easy in python. You know, web2hunter is a small script which evolved from a small terminal script to a webapp on the fly, thanks to Python. Btw, while making web2hunter, I also realized that the if __name__=='__main__': block can actually come handy when writing tests for modules.

We moved to airport after that, Ishan had flight to Delhi that evening, hectic trip though. Ah we also worked on some PHP script, got trapped with a 0770 /etc/sudoers, resetting the password from GNOME helped... while at airport and had the usual anime exchange.



The snakes who made it possible

A memorable experience indeed, an invigorating, energizing one! A truly Pythonic one :)
Looking forward to PyCon 2010 with great expectations.

Labels: ,

Sunday, September 27, 2009

QUOTD

BrainF**k - Increment-Decrement, Read or Write, move the pointer Left or Right

Labels:

Tuesday, September 22, 2009

Some more GDB fun.

Last weekend I was there for a 3 hour battle of brains for ICPC-Amritapuri prelims. Though our team did not qualify this time, and only one team would be going onsite, I had a really good time debugging with gdb and helping out others debug fast. The problem looked something like this -
  • There is F(n) = a*F(n-1) + b where 1 <= a,b,n <= 1000000000
  • F(0) = 1
  • Find F(n)
Now one would simply begin with a code similar to following -
#include <iostream>
using namespace std;

#define ull unsigned long long

ull a,b;

ull f( ull n){
if (n == 0)
return 1;
else
return a*f(n-1) + b;
}

int main (){
ull n;
cin >> a >> b >> n;
cout << f(n) << endl;
return 0;
}

Having put unsigned long long one would think it will be able to contain numbers as large as 10^9, but there is one thing that everyone missed, what range would F(n-1) fall in? Here we're trying to multiply 'a' (which is as big as 10^9) with F(n-1) (whose range we don't know).

$ g++ -g fn.cpp
$ ./a.out
2 3 4
61
$ ./a.out
10000 100203 2323
11085757089601029659
$ ./a.out
100000000 100000000 100000000
Segmentation fault

There we go, larger values within given limits kill our program. A genius would realize that this would happen even before writing this implementation, a smart one would intuitively know what happened, but what about the average joe? That's where gdb comes to give you inside story of what went wrong, so that instead of spending even 5 minutes figuring out what happened, you actually see and say 'oh, so this is what broke my code'. Here's how -

ideamonk@sacea:~$ gdb ./a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) run
Starting program: /home/ideamonk/a.out
1000000000 100000000 100000000

Program received signal SIGSEGV, Segmentation fault.
0x08048738 in f
(n=99825467) at fn.cpp:12
12 return a*f(n-1) + b;
(gdb)

One thing you can easily notice is that we entered a very large value for 'a' and at each step 'a' is getting multiplied to f(n-1), added with 'b', and multiplied again with 'a' in next recursion. Even though unsigned long long int has huge limit of +18,446,744,073,709,551,615 for 32bit processors, successive multiplications of numbers of range 10^9 is enough to quickly eat out this limit. Hence the segmentation fault... Nopes as pointed out by Prasanna though the compiler marks it as Segmentation Fault, its a Stack overflow actually (read comments for details). Well that's about the scene at ICPC, many people got interested in what gdb is and how did I do that, it's common, everyone has been annoyed by the seg faults in their 1st year c/c++ course.

The other day
tilda - ( a Quake/Doom like terminal for linux ) which I extensively use, stopped working. It happened right after I restarted my Ubuntu after update. Wonder why, Alt+F2 [tilda]... nothing came up. I headed onto terminal, and then ran tilda from there - bingo - a nice little 'Segmentation Fault' came up. When you're aware of scissors like gdb that help you make precise cut, even with a blind fold, who would be scared of entering the foreign territory of someone else's code?
So there started the bughunt....

apt-get source tilda .... OK
./configure
..
...
.....
hmm I dont have vte something... okay lets get it....

apt-get source vte ... OK
cd vte-0.20.0
./configure
..
..
....
.....
Nopes.. what's this tgetent thing... Google... okay so I need to get libncurses-dev before I compile vte.
Thank god they packaged libncurses-dev :)
sudo apt-get install libncurses-dev
Then I make and install vte, and finally make tilda with debugging flag on as -

$ ./configure CFLAGS="-g"
$ make
// lets debug
$ gdb ./src/tilda
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) run
Starting program: /home/ideamonk/Desktop/tilda-0.9.6/src/tilda
[Thread debugging using libthread_db enabled]
[New Thread 0xb7132710 (LWP 1549)]
tilda: No such file or directory
Unable to set tilda's icon: /usr/local/share/pixmaps/tilda.png
[New Thread 0xb6f0eb90 (LWP 1557)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7132710 (LWP 1549)]
0x08051365 in tilda_keygrabber_bind (keystr=0x0, tw=0x9855bd8) at key_grabber.c:260
260 if (strcmp ("", keystr) == 0)

Clearly caught! and now we know that we need to fix right here - key_grabber.c:260
keystr happens to be pointing to 0x00 in this case, I think my tilda's configuration was erased by some action, which now makes strcmp do a segmentation fault.
A check for null pointers before strcmp would be good.
So, I attach a check for null pointer before performing a strcmp and make the function return FALSE in case its null. I compile again, and tilda sweetly informs me that I haven't set a keybinding for terminal popup, and nicely shows me the preference box to set one. So now I know what happened, the keybinding... wherever it was stored, got erased. Did I have to look at complete source of tilda? No way :) Didn't I tell you that its like a scissor which is precise even if you use it with a blindfold over your eyes. Here's the tiny patch that would save you from such tilda crashes.

Labels: , ,

New header and search box

Carrying on with the sketchy and simplistic look to my blog, I removed the old header and drew some logo+monk+rocket with the iBall pen tablet I bought today.

By the way, adding search box to classic blogger is so easy, all you need to do is add a for and style it to whatever you feel like -


<form action='/search' method='get'>
<input name='q' type='text' />
<input type='submit' />
</form>

I've started hosting header and other images on App Engine itself, 1 GB a day for free seems very very cheap :)

Labels:

Sunday, September 20, 2009

Trying some jQuery effects

Many websites and blog mention about how creatively has Dragon Interactive used cross fade to give that amazing hover effect on their navigation bar.

Looks awesome right. So today I sat to figure out how do I do the same thing with jQuery and this is what came up in my mind -



  1. We place two DIVs over one another -

    <div class='content'>
    <div class='overlay'>
    </div>
    </div>

  2. Do some Photoshop/GIMP foo and make two images


  3. Style them to have those different backgrounds -

    .content{
    width:500px;
    height:300px;
    background: url(images/frame2.png) no-repeat;
    margin:0px auto;
    }
    .overlay{
    width:500px;
    height:300px;
    background: url(images/frame7.png) no-repeat;
    }

  4. Add hovering behavior to the upper one with jQuery -

    <script type='text/javascript' src='jq.js'></script>

    <script language='javascript'>

    $(document).ready( function() {
    $(".overlay").hover(
    function() {
    $(this).stop().fadeTo(400,0);
    },
    function () {
    $(this).stop().fadeTo(300,1);
    }
    );
    });

    </script>

That's it, done :) head on to live demo here or checkout the complete source on github.

Labels: , , ,

Saturday, September 19, 2009

Easy to define lists in Python

Right now I just realized how easy does Python make it to define a list of all alphabets, uppercase and lower case + numbers.
While in C/C++ I would go for an array like this -

char foo[] = { '0', '1', '2', '3', '4', '5' .... 'a', 'b', 'c' .... 'Y', 'Z' };

I hope you can understand how painful would it be two write all elements encapsulated in their respective single quotes... and imagine if after that you need to shuffle them!
However in Python I wouldn't actually need to go through that pain, I can simply write -

foo = [k for k in "0123456789abcdefghijklmnopqrstuv....Z"]

That's is, I got a list of all those characters, easy to understand, easy to type out.
Then as for shuffling it you can go for random

import random
random.shuffle (foo)
# want to get a string out of it? here it is
randomcombination = ''.join(foo)


Oh by the way, a small chit-chat with @l0nwlf actually uncovered that there is the string library... and then there are so many predefined things that we could've actually used -

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> print string.digits + string.letters
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>>
listing = [k for k in string.digits+string.letters]

Just some small things that make Python your weapon of choice when you sit down to work upon some idea you got in bathroom.
After looking at some amazing 1liners for euler and hearing about LCs and itertools, I think the following is seriously gonna be true one day... and the pythonists shall inherit the earth...

Labels: ,

Wednesday, September 16, 2009

Who stole all that bandwidth? or was it a design accident, was it a python ... ?



That's what happened to Web2Hunter yesterday. Downtime due to quota exceed, I never thought 1 Gb a day would ever exceed... but now I actually have to think about saving every possible bit. Here is my explanation to the downtime of Web2Hunter -



  1. I post about Web2Hunter on HN. Drives good deal of traffic to Web2Hunter.
  2. Many users throw requests simultaneously, thanks to 5 second ajax loop which never ends for sake of Web2Hunter's simplicity/chaos or randomness.
  3. Web2Hunter validates domains using Google, this contributes much less to the outgoing bandwidth, no problem so far.
  4. Google gives back html, each roughly 3-4 KB in size. Given a surge in traffic, and the 5 second ajax loop, this means a lot of incoming bandwidth.
  5. Again, the response to ajax request is minute (a domain name), so nothing much to do with outgoing bandwidth quotas.
  6. So, even if things are ajaxified, the poor design used to suck up lot of bandwidth internally
Solutions - things have been working fine now, and incoming bandwidth quota isn't climbing every 5 minute as it used to do yesterday.
  • Ajaxwhois API came out as a life saver. It returns a few bytes of JSON instead of chunk of html, and I don't even need to parse the JSON for it is too simple to make out from it's contents.
  • ΒΆIt could be possible for someone to misuse/proxy the domain finding url that ajax loop fetches every 5 seconds. For that I've tried to filter the requesting client by referer information. This would reduce the chances of hotlinking/sucking

    if (self.request.headers.get('Referer') != None):
    if (self.request.headers.get('Referer').find('web2hunter.appspot.com')!=-1):
    # show your content
    .
    .
    .

  • Images to be converted to jpeg, obviously every bit counts, and we really don't need fancy png without any need of translucency.
  • Another one yet to be implemented - since web2hunter does magic of random-combination to get you cool names, one good idea to save some more bandwidth would be to store all the domains that are unavailable so as to not to lookup again for them. Hmmm... but that means we lose out on any domain marked today, if it expires tomorrow. Hmm... I guess this idea would also need monthly truncation/refreshment of domains stored in unavailable list. Not good for maintenance...
So, till then I hope Web2Hunter runs smoothly on the Engine without any glitches. So far, even a one hour test has not been able to do anything to 12% incoming bandwidth :)

Labels: , ,

Tuesday, September 15, 2009

App Engine... Is it enough?

I wonder if the bandwidth limits on App Engine are actually enough! While this blog, my lunarpages shared hosting, byethost's free hosting easily survive sudden surge in traffic due to their high bandwidth limits, Web2Hunter on App Engine on other hand, almost reached incoming bandwidth quota yesterday after many visits from HN post -

and today's scene -
Today is supposed to be a normal traffic for Web2Hunter, still it has eaten up 65% of incoming bandwidth. I wonder where did it all go, of if urllib2 inside an App Engine app is a very costly decision given they don't allow sockets.
Besides another point to note is that when we had done a simulated DDoS over a free byethost account, it drained 3 Gb of bandwidth in a day, but yet the service kept continuing without any issues. That seriously makes you wonder which one is better - a free php+mysql+no-ads or AppEngine+rapidly-sucked-bandwidth. I still have to investigate the bandwidth issue by writing another app, which doesn't actually make many requests in background using urllib2/httplib. Presence of these two libraries makes an app a little different from usual db+interface+logic apps. Lets see...

Labels: , ,

Web2Hunter - grab those domains before they vanish

Last weekend I was trying to make a more useful app over Google App Engine... at least better than the first one. So here's what I got for you - Web2Hunter, yet another one that brings up some randomized yet organized information to you, thanks to urllib2 and Python in the back. Here is the intro article I wrote for it -

How many times you have been stuck for hours thinking of that new name for your startup? How many times have you come up with that awesome name and you found out that all your imagination was on sale by a domain squatter? Don't worry, for Web2Hunter is here for rescue. It not only saves you from spending extra hours over thinking a new name, but also from the dismay of not finding a domain for the name you liked.

Simply put, Web2Hunter shows you a list of random trendy names which are actually available for purchase. It's about assisting your creativity. It throws up randomness, strange names of any kind. It's fun to watch because it opens your mind to new possibilities when trying to name your service, or even have new ideas.

Let Web2Hunter hunt down some cool names for you before you spend your precious time over a new name. Web2Hunter runs over Google App Engine and tried to be minimalistic. Do you have any suggestions to improve Web2Hunter ?

The original article can be found on The Indian Startup - Tools blog.

Feel free to fork Web2Hunter's source code on github. I also happened to give a short talk on it at PyCon India 2009, which was very impromptu and organized, for all I had to show was code, no slides. Have a look (youtube).

Labels: , , , ,

Monday, September 14, 2009

jQuery Mountains


Adding callbacks and chaining events one after the other in jQuery finally ends up in deep mountains of code-nests. It happens again and again with me! Wonder if anyone else too falls to this trend while writing some jQuery fx...

Labels: ,

Saturday, September 12, 2009

Now that's my vim...

People talk of IDEs, some vouch for eclipse, some netbeans, some prefer geany, some people like me use gedit and gvim all the time. Last month I was looking for some better customizations for Python in gvim and gedit and happened to try out every other python related plugin. Simplicity of gvim ruled out the incompatibility of some plugins of gedit in the end. Beautified with monokai color theme and Monaco font, it almost gives you feel of those colorful RoR screencasts. Have a look -


  • NERDTree is an awesome plugin that helps you browse/switch through files in your project just as one would do with gedit's file browser. I've kept Ctrl+Alt+N as a shortcut to toggle NERDTree
  • Pydiction is another good plugin to tab-complete your python code.
  • As for Monokai color theme you've got three options - molokai, lanai, monokai_modified
  • Grab Monaco.ttf

    $ wget http://www.gringod.com/wp-upload/MONACO.TTF -O /tmp/monaco.ttf
    $ sudo cp /tmp/monaco.ttf /usr/share/fonts/truetype/freefont/
    $ sudo chmod o+r /usr/share/fonts/truetype/freefont/monaco.ttf
    $ fc-cache -f -v

  • Have fun changing your terminal font to monaco, better make it as default monospace font.
You can even checkout my dotfiles stash on github. Make sure you make changes to your home location on line 73 of .vimrc.

pydiction in action

Labels: ,

Wednesday, September 09, 2009

QUOTD

Education System - Man's miserably failed attempts at measuring and mechanizing the human intellect.


System's never do anything creative...
-- ideamonk

Labels: ,

Sunday, September 06, 2009

Cowsay!

Who wants a thinking speaking configurable cow! I wonder if anyone has found a good use for it. As for me, its just another fun tool like fortune and fish. You can make cow speak anything from stdin, it's fun making it speak some fortune for you.
ideamonk@sacea:~$ sudo apt-get isntall cowsay
ideamonk@sacea:~$ fortune | cowspeak

I wish I could have two cows calling each other, as in
ideamonk@sacea:~$ fortune | cowthink | cowsay

You know... it's like the cow would think first and then speak :P but that doesn't happen as of now... maybe soon. Till then I have to avoid doing so...
ideamonk@sacea:~$ fortune | cowsay | cowthink
_________________________________________
( _____________________________________ )
( / All the troubles you have will pass \ )
( \ away very quickly. / )
( )
( ------------------------------------- )
( )
( \ ^__^ )
( )
( \ (oo)\_______ )
( )
( (__)\ )\/\ )
( )
( ||----w | )
( )
( || || )
-----------------------------------------
o ^__^
o (oo)\_______
(__)\ )\/\
||----w |
|| ||
ideamonk@sacea:~$


You can even have a tux :)
ideamonk@sacea:~$ fortune | cowsay -f tux
______________________________________
/ Windows and DOS -- a turtle and it's \
\ shell. /
--------------------------------------
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/


Read more about it on Linux Gazette.

Labels: , , ,

Saturday, September 05, 2009

ILUG-B meetup #2

Well, it was a pleasant Saturday with 50-cent playing on the auto in which I sped towards TW office - for ilug-b meet. This one was a total gyan session and even though there was even more OHP than last time, I'm glad that I pulled up till the end. After all I can definitely say BeleniX and ZFS are the next two things on my tryout list. Ram's presentation was awesome and in the end we got to see some seriously cool stuff people made through Virtualization and Crossbow on Solaris. Hats off to the $350 magic box guys!
And, oh the discussion over GPG finally ended I suppose! Finally when things were light under beer, I could observe some less OHP... 4chan, LOLcat, Mitnick, first-time-on-linux and what not. I guess I've had my usual amount of beer that invites me for a nice sleep. Even mom has guessed it as she puts it "ette der katte lagalao? daru peeke elhi yay ki...?" damn! How do I respond to that :P

Afterthoughts -
  • Linux is more about loving the OS, than just being an enthusiastic user...
  • Determination is even more important as pg puts it.

Labels: , , ,

Friday, September 04, 2009

Timepass


Got two papers tomorrow, and even the LUG meet, but somehow I don't feel like starting at all unless the last hour arrives, it's always been like this with me! Someday this habit would end, and maybe I would do something more than time pass.
As of now I need to wonder a little over what Visual C++ 6, yeah the old one is doing under HCI in my 3rd year syllabus, and why my teacher is so keen on putting the structure of WNDCLASS as a question, why to do something which is obsolete, and why even commit it to memory! And what good does is hold in memorizing 10 10 constants each which some company decided to use in their API, shouldn't education be a little more universal? and not a double-faced bitch as I see it. And why are people related to technological education sleeping while it happens to be a dynamic field which changes every second.
When the world is moving towards making things easier for us to do in lesser steps, these ghosts of the past are hell bent on teaching exorcism of medival ages which are hard to do and hold no significance at all.
What kind of education is this? or is it just that someone who has no will to adapt to the change is pulling off teaching what she learnt in her 20s. Academia at such times looks like home of loosers!

Epilogue:
  • Hmm, memorizing Nano notes turned out to be tougher than reading a few pages of the old Charles Petzold book
  • VC++ was not that tough to push into the brain, the push-discomfort is inversely proportional to relevance of stuff being pushed in.

Labels: , , ,