tag:blogger.com,1999:blog-9496567211770426772024-03-12T22:43:40.952-07:00me, myself and pyMy adventures with PythonNishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-949656721177042677.post-82391185595391492452011-12-10T04:47:00.000-08:002011-12-10T06:09:50.872-08:00Unplanned cycle ride and Python<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjx99BcBa32TYTkvMmzX6_onTx36w3Cue3hNgEWlXJMdkiFcLbMKIqCgeCnf-ghaJQotVtHC0NQP3qi15yb5nUF1gDVhj02eMpEU659u2_eDcSRDuraSV6ENXExeXo5qwkZer90xk6LMmk/s1600/crp_to_hsn.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 241px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjx99BcBa32TYTkvMmzX6_onTx36w3Cue3hNgEWlXJMdkiFcLbMKIqCgeCnf-ghaJQotVtHC0NQP3qi15yb5nUF1gDVhj02eMpEU659u2_eDcSRDuraSV6ENXExeXo5qwkZer90xk6LMmk/s320/crp_to_hsn.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5684499233231533042" /></a><br />Around May 2011, the time I decided to go on solo bike rides and GoogleMaps has been of great help ever since. It was only after a few months, I realized there was a <a href="http://py-googlemaps.sourceforge.net/">python binding for GoogleMaps</a>. Only after going on an unplanned solo cycling trip recently, I realized how much of a help it can be. Since most of the times I'm on highways and well raid roads, it is possible to get the elevation of the whole route. When the elevations are plotted, it gives a vague idea of the terrain. On the left the terrain of road from Channarayapatna to Hassan (Elevation is in mtrs).<div><br /></div><div>Enough talk and <a href="http://ideone.com/GvBCd">here</a> is the code. The <a href="http://code.google.com/apis/maps/documentation/elevation/">elevation api</a> has been used to get elevation of a point and googlemaps api has been used to get the latitude and longitude of start and end points. The points along the route have been interpolated as of now. But they can be obtained point by point using googlemaps api and interpolated based on the distance.</div><div><br /></div><div>Will probably do all that and make another post soon. Happy coding till then.</div>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-49357567872585934442011-09-06T01:39:00.000-07:002011-09-06T03:19:00.846-07:00Enumerating Palindromic NumbersA palindromic number remains same when reversed. Ex: 141, 5, 22, 1991, 27472 and so on..<br /><br />I came across a problem which needed me to check if a number is a palindrome which I can do by reversing the number and comparing it with the original.<br /><br />But what if I wanted all the possible palindromic numbers of a given width.<br /><br />If I wanted all the 5 palindromic numbers, I can solve them by considering the number as abcba where a, b, c are digits and a!=0. The number would be a(10000) + b(1000) + c(100) + b(10) + a.<br /><br />Then the problem becomes trivial.<br /><br /><pre>for a in range(9, 0, -1):<br /> for b in range(9, -1, -1):<br /> for c in range(9, -1, -1):<br /> num = 10001 * a + 1010 * b + 100 * c<br /></pre><br /><br />But as we can see, it is not generic and making it generic this way, would require me to use iterators.<br /><br />On a sunday morning, travelling from Krishnagiri to Dharmapuri in a local bus, at 04:14, this sweet and simple approach occured to me.<br /><br />It is a recursive approach that considers the fact that all n digit palindromes can be generated by adding a digit at the front and back of the (n - 2) digit palindromes and (n - 4) digit palindromes and so on.<br /><br />Ex: 5 digit palindromes are of the form<br />a***a where a != 0 and *** is a 3d palindrome<br />a0*0a where * is a 1d palindrome.<br /><br /><pre><br />def nDigitPalins(n):<br /><br /> if n == 1:<br /> return range(9, 0, -1) <br /> elif n == 2:<br /> return [11 * x for x in range(9, 0, -1)]<br /> else:<br /> p_zeros = [i * (10 ** (n -1) + 1) for i in range(9, 0, -1)]<br /><br /> p = []<br /> for base in p_zeros:<br /> for y in range(1, (n + 1) / 2):<br /> for subPalin in nDigitPalins(n - 2 * y):<br /> p_num = base + subPalin * (10 ** y)<br /> p.append(p_num)<br /> p.append(base)<br /><br /> return p<br /><br />print nDigitPalins(8)<br /></pre><br /><br />You can run the code at <a href="http://ideone.com/szO5q">http://ideone.com/szO5q</a>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-70699065679487521952011-05-11T03:04:00.000-07:002011-05-11T03:11:29.660-07:00is perfect squareHow do you write a program that checks if a number is perfect square or not.<br /><br /><pre>def isPerfectSquare(x):<br /> i = 0<br /> while i * i < x:<br /> i += 1<br /> return i * i == x</pre><br /><br />This is such a beautiful code.<br /><br />Is it??? How about this.<br /><br /><pre>def isPerfectSquare(x):<br /> xRoot = int(x ** 0.5 + 0.1)<br /> return xRoot * xRoot == x</pre><br /><br />or in my favourite lambda way,<br /><br /><pre>isPerfectSquare = lambda x: int(x ** 0.5 + 0.1) ** 2 == x</pre><br /><br />If you know what is going on, you'll love the recipe. If you don't then start thinking.Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-7365638854838476332010-07-30T21:30:00.000-07:002010-07-30T22:51:27.047-07:00Python in VimWhat if your favorite editor can interpret your favorite language!!! That would make writing code a piece of cake. This is what happened to me recently. I knew Vim could interpret Python. Only after I tried it out, I found how easy it is.<br /><br />Simply go to normal mode and do <code> :py print 'Vim Roxx' </code> to see what I am trying to tell. You have to include a :py at the start of the line and it runs in Vim just as if it were running on a Python interpreter.<br /><br />I know how you are feeling. I felt the same way too. Isn't there a way to specify start of python code and end. There is one.<br /> <code> :py << EOF<br />print "This is cool"<br />print "I'm lovin it"<br />EOF </code><br /> does the job for you. The EOF here is only a name and any variable_name can be used. You can also specify a file to execute by using <code> :pyfile file_name</code><br /><br />Here is a quick snippet. I would like to run the Python code from my current buffer. I want to go over a line and press f4 and expect Vim to execute that command by adding a ":py" in the start.<br /><br />Hence I first do<br /><code><pre>:py << EOF<br />import vim<br />def run_line():<br /> cmd = ":py " + vim.curent.line<br /> vim.command(cmd)<br />EOF</pre></code> So now every time I go over a line and type <code> :py run_line() </code> in the normal mode, the current line is run as a line of python code. All I now have to do is bind this function to the required key and <code> nmap <f4> :py run_line() <CR> </code>does the job for me.<br /><br />Happy Vimming with Python :) :)Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-90305171552797955222010-07-22T11:58:00.000-07:002010-07-22T12:16:54.281-07:00Adding PolynomialsImagine a polynomial like 2x<sup>3</sup> - 3x<sup>2</sup> + 5 was represented as a list of coefficients in a list like [1, -3, 0, 5]<br /><br /><div>My goal is to write a function that takes two lists and does polynomial addition on them. Ex: poly_add( [1, -2, 0, 3, 5] , [3, -1, 2] ) should return [1, -2, 3, 2, 7]</div><div><br /></div><div>After a little brainstorming, I got this cute idea, which I felt was so cute that I should blog about it. Moreover it has been a while since I have written something.</div><div><br /></div><div>Here is the function</div><div><code><pre><br />def poly_add( x, y):<br /> min_len = min( len(x), len(y))<br /> return x[: -min_len] + y[: -min_len] + [ x[i] + y[i] for i in range(-min_len,0) ]<br /></pre></code><br /></div><div>If this is trivial then great. If it is not so trivial, you will love the beauty behind this function and hence python itself.</div><div><br /></div><div>I use Python and I am loving it :) :) :)</div>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com2tag:blogger.com,1999:blog-949656721177042677.post-14976675288690179622010-02-19T01:01:00.000-08:002010-02-19T02:56:15.808-08:00My daily routine ( monitor )Here is how I spend my typical day<div><ol><li>wake up early in the afternoon at 11:30</li><li>go online and check mail for 20 min</li><li>get ready for lunch by 13:00</li><li>go to the lab and spend time with my lappy till 20:00</li><li>dinner</li><li>again spend time in front of lappy</li><li>go to bed</li></ol><div>Looks like I spend most of the time in front of my lappy. Wonder what I do and for how long!!!</div><div>What if we had a program that monitors for how much time do we spend looking at each window on our machine. Actually why don't we write one. This brilliant idea was not mine but my friend <a href="http://punchagan.wordpress.com/">Puneeth</a>'s. We tried to write this program long back ( on the night of 1st day, PyCon India ). But <a href="http://python-xlib.sourceforge.net/doc/html/index.html">python Xlib documentation</a> sucks. Atleast that is what I felt when I tried to refer it. Anyways luckily that night punch got quite close to finishing this program. Dunno if he finished it later on. But I wanted to try it out. I actually saw this documentation long before we started coding for this program. I wanted to have an option on right click menu to close the current window. I found something called <a href="http://www.makeuseof.com/tag/add-custom-functionality-to-nautilus-linux/">NACT</a> for adding options to right click on nautilus and a program called <a href="http://www.steve.org.uk/Software/xclose/">xclose</a> that closes window using window display name. I could get xclose running and NACT configured but forgot about it later on.</div><div><br /></div><div>Now I start googling. I found <a href="http://www.larsen-b.com/Article/184.html">this link</a> that got me started.Punch figured out that there is something called display and then screen and then window. Luckily I remembered those words. The blog post also talks about the same. I tried my hands on Xlib using ipython and found out what all this window thingy is all about.</div><div><br /></div><div>Here is a small intro to Xlib that must get you started.</div><div><span><span></span></span><br /></div><div></div><span><span>from Xlib.display import Display</span></span></div><div><span><span><br /></span></span></div><div><span><span># initialize the display</span></span></div><div><span><span>disp = Display()</span></span></div><div><span><span><br /></span></span></div><div><span><span># list no of screens available</span></span></div><div><span><span>disp.screen_count() # only one in my case.. my laptop screen.. </span></span></div><div><span><span># get the screen to work on</span></span></div><div><span><span>screen = disp.screen() # disp.screen(0) also works</span></span></div><div><span><span><br />## everything we see is a hierarchy of windows</span></span></div><div><span><span><br /></span></span></div><div><span><span>root_window = screen.root </span></span></div><div><span><span># this window object will have its parent equal to zero</span></span></div><div><span><span><br />print root_window.get_wm_name() ## the obvious</span></span></div><div><span><span><br />root_tree = root_window.query_tree()</span></span></div><div><span><span>root_tree.children ## gives you a list of window objects, its children</span></span></div><div><span><span>root_tree.parent ## lets you access its parent</span></span></div><div><span><span><br /></span></span></div><div><span><span>Now I could not achieve what I exactly wanted. Figuring out what we concentrate on, looking at the cascade of windows and their sizes involves AI. All I wanted was the window that had focus and for how long. There is a method called event notify that notifies a focus change event but It involves a lot more digging into Xlib documentation and some patience to try out all that on ipython. For now, all I am going with the brute force method, polling the Xserver every n seconds. I ran the script while I was writing the blog post and here are the results.</span></span></div><div><span><span>#######</span></span></div><div><span><span><div>02/19/10-16:10:58 -> ans@ans-laptop: ~/Desktop/xlib</div><div>02/19/10-16:11:08 -> Google Docs�-�Upload a File - Vimperator</div><div>02/19/10-16:11:18 -> Google Docs - All items - Vimperator</div><div>02/19/10-16:11:28 -> poll-xserver.py - Google Docs - Vimperator</div><div>02/19/10-16:11:38 -> poll-xserver.py - Google Docs - Vimperator</div><div>02/19/10-16:11:48 -> poll-xserver.py - Google Docs - Vimperator</div><div>02/19/10-16:11:58 -> Blogger: me, myself and py - Create Post - Google Chrome</div><div>02/19/10-16:12:08 -> Blogger: me, myself and py - Create Post - Google Chrome</div><div>02/19/10-16:12:18 -> Blogger: me, myself and py - Create Post - Google Chrome</div><div>02/19/10-16:12:28 -> Blogger: me, myself and py - Create Post - Google Chrome</div><div>02/19/10-16:12:38 -> Blogger: me, myself and py - Create Post - Google Chrome</div><div>########</div><div><br /></div><div>I find them pretty satisfactory. <a href="http://docs.google.com/leaf?id=0B9zpXhYsI2FyMzJjNTA2MjctMGRlOS00NTE4LWIwNmMtZjFmZmMxNjQ3ZTVl&sort=name&layout=list&num=50">Here</a> is the code in case some one wants to hav a luk.</div><div>Happy Coding :)</div></span></span></div>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-38595683439492117182010-02-17T05:47:00.000-08:002010-02-17T06:44:19.456-08:00File browser using Python<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiATV6DC1ySgUJdRhC58Nb6SFJG3cMJwryL3KEHm-3YRQWAZiMpNnVaxPAdG1h_OMC0oc37yieOX4V70oyPchyRu9CTXHqvGNOrONQZXAezX1d5K_IA0U8T8Xle3MCzBHb1HPj-3MCnYU/s1600-h/pyb.png"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 200px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiATV6DC1ySgUJdRhC58Nb6SFJG3cMJwryL3KEHm-3YRQWAZiMpNnVaxPAdG1h_OMC0oc37yieOX4V70oyPchyRu9CTXHqvGNOrONQZXAezX1d5K_IA0U8T8Xle3MCzBHb1HPj-3MCnYU/s320/pyb.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5439220267042715730" /></a><br />I always wanted to have a file browser ( like nautilus ) on my shell. Something like the image on the left.<div>I prefer to have my hands on the keyboard than shifting between keyboard and the tailed buddy.</div><div>I saw my friend Lee use something called <a href="http://www.midnight-commander.org/">Midnight Commander</a>. I googled and found other alternatives <a href="http://168hours.wordpress.com/2008/08/18/10-total-commander-alternatives-for-linux/">here</a>. But only MC was appealing to me. I started using it and it is a decent one. But I was not satisfied with its display. It shows all the files with in one big column and displays all the details as if some had typed "ls -l". And recently after switching to vi mode, I am finding it hard to access the arrow keys and would prefer something similar to vi. One way of achieving it is actually remapping keys in MC. But still the concept of moving left and right does not have a meaning in MC context. Moreover, we are used to things that behave like nautilus. I wanted a file browser that displays folders and files like nautilus and makes it possible to navigate using hjkl keys. I also wanted a mechanism where I could tag files just like in delicious. I finally decided I want a new file browser and I am going to write it :) .</div><div><br /></div><div>My obvious and only choice is python. Although there is bash scripting with zenity to make your life easy, I went for python for the obvious reason. I also want the fun of coding in python. I also wanted to try out coding in python on my newly configured Vim. My idea of programming on terminals was limited to printing something on terminal. I tried out something like that long back using strings. But it was a very naive way of doing things and I didn't like it. One day I tried out something called bpython. It is another command line interpreter for python. I was fascinated by the way the program works. It is simply amazing and behaves like a proper GUI IDE on shell. Luckily my friend Madhu told me that this is written using ncurses. The word curses caught me. I thought may be it can help me out in writing my file browser. But I didnt give it much thought. I actually tried out some stuff only to see that it requires special way of approach.</div><div><br /></div><div>So now here I am, decided to write a file browser and know a word ncurses. Where do I start... obvious google. I found a few links on ncurses and learnt that it also has a wrapper in python called "curses". But then I realised this is exactly what I did when I tried ncurses for the first time and got no where. I looked back and found out that the reason I stopped is because the moment you try to manipulate your terminal, the default behaviour of your python interpreter is lost and it gets really annoying to proceed. All you can do is write code in a file and run it every time. I wondered how would bpython behave!!. I tried basic importing and initialising screen and to my delight, it did not have any effect on bpython console. bpython being written using curses, overwrites all my actions. My interpreter is working fine. So this also has a disadvantage. Although you can see all the attributes available for any object since bpython displays attributes after you type a dot, you cant really see any change that you make. Anyways I still will have to stick to my old way of doing, writing in a file and executing.</div><div><br /></div><div>I found a few links on how to program in curses using python.</div><div><a href="http://web.cs.mun.ca/~rod/ncurses/ncurses.html">Writing Programs with NCURSES</a></div><div><a href="http://www.python.org/doc/howto">Curses Programming with Python</a></div><div>These links got me started and <a href="http://docs.python.org/library/curses.html">python curses man page</a> helped me through the process.</div><div><br /></div><div>And that is how I end up with something that suits perfectly to my requirements.</div><div>Currently this is the key mapping on my PyBrowser that opens with files in current directory.</div><div><ul><li>h - move left</li><li>j - move down</li><li>k - move up</li><li>l - move right</li><li>b - back one directory</li><li>o - open the directory or file( programs are mapped to filenames in a different conf file )</li><li>H - Toggle hidden files</li><li>q - close or quit</li></ul><div>All this in 200 lines of code and 2 days of work with loooots of breaks. Now you know why I code in Python ;) .Shall add more functionality later on.</div></div>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com6tag:blogger.com,1999:blog-949656721177042677.post-53801329844220386312010-02-15T05:27:00.000-08:002010-02-15T19:21:32.762-08:00The Vim mode<span style="font-weight:bold;"><a href="http://www.vim.org/">Vim</a></span> Forever...<br /><br />It all started when I saw Venkatesh Choppella working without taking his fingers off the keyboard to do many things which usually required shifting my hand between keyboard and my tailed friend, mouse. I knew for sure that he was using emacs. It was cool and I was fascinated about this cute little tool. My friend Puneeth uses it all the time for editing. He also has some crazy extension on firefox that troubles me while using. It is emacs extension.<br /><br />Anyways now I wanted to use something cool like that. I know for sure that Vi is the competitor and so I started by googling for editor wars. I found many links that support emacs. So I started learning emacs rather looking at emacs. First thing I didnt like was the use of control key so often which required shifting of my fingers way down the home row. Then I had problem shifting my fingers to use the arrow keys for navigating through the file.<br /><br />So here I am left with no other option than vi. I have used vi for quite a while and am pretty comfortable with it. But all I do in vi is first get into insert mode and edit the file as if I am using notepad. I started searching for tutorials on vi and found one that is so simple yet so amazing. I thank Swaroop CH, yes the same guy who wrote the book "A Byte of Python", for another amazing book <a href="http://www.swaroopch.com/notes/Vim">"A Byte of Vim"</a>. It was so amazing and I started using vi like a novice vimmer.<br /><br />Now editing a text file is something I do only once in a while. Most of the time I either code in Python or browse on Firefox. Ofcourse watching stuff and listening to music is also there but doesn't make sense in this context. So now what is this emacs plugin for firefox and is there something similar for Vim???. Yes there is one and it is called <a href="http://www.vimperator.org/">Vimperator</a>. It transforms firefox into something that behaves similar to Vim editor and hence very seldom do I need the mouse. It is a totally different experience using firefox in Vim mode.<br /><br />Now comes the most important job. Making Vim an ide for Python.<br />Let me start by listing a few things one does on a typical ide.<br /><div><ol><li>A file browser that stays on left of screen and enables opening files on one click</li><li>Ability to show multiple files in one screen </li><li>Syntax highlighting and code folding</li><li>Taglist that displays all the functions, classes and stuff in one place</li><li>Auto Indentation</li><li>Code completion</li><li>Debugging</li></ol><div>Phew!! finally the list ended.</div><div>Lets start with the first, The File browser</div><div>Vim has a very simple and light file browser plugin called <a href="http://www.vim.org/scripts/script.php?script_id=1658">NERDTree</a>.</div><div>Installing is as simple as extracting the files into ~/.vim directory.</div><div>Using :NERDTree brings up the nerdtree and a tree structure is displayed.</div><div><div>Moving between files is the same as it is in normal mode. The usual hjkl work and <CR> or the enter key is used to expand or collapse a directory. Clicking enter on a file opens the file in edit mode. 'o' key can also be used to do the same. <ctrl+w> <ctrl+w> is used to switch context between nerdtree and the file buffer. 'i' opens the file in a new screen.</div></div><div><br /></div><div>Showing multiple files.</div><div>This is as simple as splitting the screen into parts and showing different files on each screen.</div><div>:sp splits the screen horizontally and :vsp splits vertically.</div><div>If you notice, we have seen something similar while using nerdtree. What it is doing is simply opening another buffer for itself.</div><div><ctrl+w> <ctrl+w> can be used to switch context. The usual vim way of prepending a number also works here. so 4 <ctrl+w> would execute <ctrl+w> command four times. <ctrl+w> can also be mixed with hjkl.</ctrl+w></div><div><br /></div><div>syntax highlighting and code folding</div><div>vim takes care of syntax and all we have to do is :syntax on. You can also download a syntax file from <a href="http://www.vim.org/scripts/script.php?script_id=790">here</a> or write one of your own and name it python.vim. Installing is as simple as placing it in ~/.vim/syntax folder.</div><div>code folding is a little different for different contexts. For python, the obvious is indentation. First do :foldmethod=indent. Then scroll to your block of code and za toggles the folding of code.</div><div><br /></div><div><a href="http://www.vim.org/scripts/script.php?script_id=273">taglist</a> is a plugin for vim that supports many languages. It requires a package called "exuberate ctags" which should be directly available on repo for any distro. Then download the zip file and extract contents to ~/.vim and you have your plugin.</div><div>The visibility of taglist can be toggled using :TlistToggle and it works similar to nerdtree.</div><div><br /></div><div>Autoindentation</div><div>following pep8, here are a few settings that should make your life easy.</div><div><div><ul><li>set expandtab</li><li>set textwidth=79</li><li>set tabstop=8</li><li>set softtabstop=4</li><li>set shiftwidth=4</li><li>set autoindent</li></ul><div>at this point you will notice that you have to type the 4 spaces or tab for the first time and then the code is auto indented from the next line.</div><div>Wouldn't it be great if we didn't have to bother about even touching the tab key and the editor does that depending on the context. All that is just one step away. Download <a href="http://www.vim.org/scripts/download_script.php?src_id=4316">this</a> script and place it in ~/.vim/indent folder. Now use the command ":filetype plugin indent on" or add the line "filetype plugin indent on" to your .vimrc file in home folder and there you go. Vim does all your indenting.</div><div><br /></div><div>If you would have noticed, we have to type in all these conf settings every time we start vim. We don't want vi to do these settings for all the files either. What if we could set things only for files that end in .py. There is a very simple way of accomplishing that. Change the "set" to "setlocal" and put all your settings in a python.vim file and place it in ~/.vim/ftplugin.</div><div>So your python.vim would look like this:</div><div><br /></div><div><span><span></span></span><div>setlocal expandtab</div><div>setlocal textwidth=79</div><div>setlocal tabstop=8</div><div>setlocal softtabstop=4</div><div>setlocal shiftwidth=4</div><div>setlocal autoindent</div><div><br /></div></div></div></div><div>Code completion and Debugging are for a different set of audience ( not me ).</div><div>But shall blog about it soon.</div></div>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com4tag:blogger.com,1999:blog-949656721177042677.post-20269784266626694842009-09-29T02:08:00.000-07:002009-09-29T03:10:57.836-07:00GAE + Django = GodlikeImagine you are given tools and the platform simultaeneously to show off your talents and everything at free( as in free beer ;) ) of cost. Wouldn't you start right away!!!.<br />The tool here is <a href="http://www.djangoproject.com/">Django</a> and the platform is <a href="https://appengine.google.com/">GAE - Google App Engine</a>.<br /><br />I first time I heard the name Django was in a <a href="http://www.zedshaw.com/blog/2009-03-20.html">blog post</a>. Though at that time, I didn't know the beauty of philosophy behind Django, the excitement to try something in the web building domain pushed me to go for it. Adding to my excitement was the fact that Django was implemented in Python. The next moment, I installed Django and started reading Django-docs. Since this was the first time I tried something on web building, I got stuck at a point and couldn't proceed further. But I still had the dream of designing a very small website or at least a web page. Luckily, I was able to attend a talk on Django at PyCon India 2009 and got the solution for my problem.<br /><br />On the same day, my friend suggested me "google app engine", when I told him I was interested in Django. Though the name says its a Google project, I had no clue what it was. It was then lost in the jargon of my "to do" ideas I got after attending PyCon.<br /><br />The excitement of continuing Django from the point where I left it got to me on the day on PyCon itself, after attending the talk on Django. I started reding Django, now from an e-book. After reading it for 3-4 hrs, I was totally lost and had no clue what was going on. I googled for a good book on Django and found the awesomest source for Django tutorial, <a href="http://www.djangobook.com/about/">The Django Book</a>. As I started reading, I not only found the book very easy to learn, but also very clear on the concepts. The whole book can be finished in a quarter of a day even by an intermediate python programmer if he has a fairly good knowledge of HTML. The greatness here is not of the book but of the framework. The design was so thought of and researched to fit every need that, you will start appreciating it after the first hour of reading itself. Django follows the DRY-Do not Repeat Yourself principle and adheres to it in every possible way. I got some very simple ideas and decided to implement them. Wait!!! What if I had a chance to host it. Then suddenly it stuck to my mind that I had a new topic to read about, GAE. <br /><br />I googled and started off with installing and getting started. GAE is a platform with which you can host your website on the google architecture and monitor it. GAE supports website backends in two languages viz., Python and Java. GAE itself ships with an appengine, a framework. But it also supports any any <a href="http://www.python.org/dev/peps/pep-0333/">WSGI</a>-compliant framework. Hence, it also supports Django and I am already starting to hit the limits of my imagination on the possibilities using them.<br /><br />GAE also supports many frameworks like CherryPy, Pylons, and web.py. There is a debate on whether to use Django or appengine framework. Here are a few links that help you decide <a href="http://aralbalkan.com/1348">http://aralbalkan.com/1348</a>.<br /><br />I am going with Django because it is simply too good.Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com1tag:blogger.com,1999:blog-949656721177042677.post-39439812330900027782009-09-26T12:10:00.000-07:002009-09-26T13:14:39.444-07:00PyCon India 2009 - Day 1I came all the way without staying at my home town even skipping Dusshera celebrations to attend PyCon.<br />So you can understand how excited I was.<br />The day before the event..<br />I went thro' the list of presentations and wanted to be ready with all the software incase I suddenly feel like coding sometime during the event.<br />So I got the py3.1( since some talks needed py3.x ) source and went to sleep early since I didnt want to miss the keynote which was the first talk the next day.<br /><br />The day of event ( Today )<br />I was expecting BOF tents ( even brought my laptop with charger and spike !!! along ) and stalls as was the case for FOSS.IN 2008 but didnt find any. <br />My first opinion about the organization was, it is good ( looking at the registration and stuff ).<br /><br />The first presentation was the keynote, 'My Adventures with Python' by Prabhu Ramachandran.<br />I have already seen a video of the talk he gave at scipy conference and felt he is simply too excellent at giving presentations. His voice, body language, presentation slides evetything are simply awesome.<br /><br />He talked about his relationship with python and the areas of using python( almost everywhere ).<br />Then he talked about 3d simulations in python and how Mayavi came into existence.<br />The presentation in overall was so nice that every other talk after that seemed dull.<br /><br />Then there were many talks that ran in parallel and here is an overview of the talks I attended.<br /><br />After the keynote, I attended the talk on network security tools using python.<br />This one discussed briefly about pypcap,dpkt and scapy.<br />I wasnt much interested in the first place and even the talk wasnt so gr8.<br /><br />The next talk I attended was on algorithms in python and it was about the merge sort, bubble sort and stuff.<br /><br />Then the next talk was about Design Patterns in python which I was really looking forward to.<br />The content of the talk was a little advanced and required much more detailed explanation and cleaner examples. But the presentation wasnt so gr8 but I got links to a few resources that are useful to get a better knowledge of design patterns.<br /><br />The next talk was on Idiomatic Python.<br />Though I have already read about Idioms in python, I attended the talk.<br />The guy who gave the talk was some college student in his final year.<br />The presentation was excellent. He precisely knew what he was talking about. <br /><br />The last talk was on Django.<br />There were some three guys giving the talk and even this one was awesome.<br />I got few doubts on Django cleared and decided to develop applications using django.<br /><br />The day 1 went on pretty well.<br />Now looking at the schedule for day 2, I dont see any interesting talks,<br />There were a few proposed talks on python for mobile app development and stuff.<br />Dunno why they have been cancelled.<br /><br />The PyCon India 2009 conference is good.<br />The talks were also decent enough considering this is the first PyCon in india.<br /><br />Looking forward to many more talks and a much more better PyCon in 2010.Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-55003273422602583162009-09-07T08:19:00.000-07:002009-09-07T08:45:12.357-07:00Python OOP tutorial - made simpleThis is my third post and I actually thought of writing a tutorial on python.<br />But I see a lot of tutorials on python on the web at various levels of detail.<br />Here are a few for a newbie.<br /><br /><a href="http://www.swaroopch.com/notes/Python">A Byte of Python</a><br /><a href="http://docs.python.org/tutorial/index.html#tutorial-index">Python website</a><br /><a href="http://sysblogd.wordpress.com/2007/11/17/learning-python-a-easy-yet-powerfull-scriptingprogramming-language/">Python in 100 session lines</a><br /><br />Now Python is an OO language.<br />For someone new to concept of OOP, the above tutorials are a little confusing<br />Here is a great tutorial that explains the concept of OOP and how it is done in python.<br /><a href="http://pytut.infogami.com/node11-baseline.html">Introduction to classes</a>Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com2tag:blogger.com,1999:blog-949656721177042677.post-14127438669152277112009-08-22T04:37:00.000-07:002009-08-22T05:12:24.413-07:00When I first saw pythonI wrote the "Hello World" program in perl and showed it to voodoo. He showed similar hello world program in python. I already heard of python once from punch. But I was happy with perl at that time and so didnt bother much about it. The next day at office was not the way I expected. Things were not moving as fast as expected with my perl and so my firend and colleague asim( who was a pythonista by then ) suggested python.<br /><br />I thought "another language!!!". asim suggested me "A Byte of Python" by "Swaroop Kumar C H". As I started reading, "The interpreter is nice" and then "hey the variables are friendly" and after a while, "this language is awesome". But I wasn't comfortable with the indentation. Now after telling a few people about this feature, I understand why I felt that way. Now after using python for more than a year, I understand how useful this feature is for maintaining code.<br /><br />And that was how python became my favourite language.Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-20123131890851350362009-08-21T08:26:00.000-07:002009-08-21T08:46:05.101-07:00My first programming languageI was doing my ps2 at TSae,Bangalore. It was my 5th day. My project manager Sateesh gave me a brief introduction about the project and assigned my job. He was a gr8 man wid lots of patience. I told him I am totally new to software development and all i know is doing some basic math in C. He suggested me to learn Perl first and then he would guide me accordingly.<br /><br />I started with the book "Teach yourself perl 5 in 21 days". As I went through, I thought "These variables are gr8, they can store strings, numbers, anything ( except for $ sign before every variable )" and then "these RegEx are fun ( If exceptions are taken care )" and<br /><br />Recently when I was learning perl properly for regular use, I came across this in some book that describes perl in one line. It said "Perl is all about thinking like Larry". Larry is the creator of perl.<br /><br />Usually perl code is very small and a lil cryptic, it can be fun for programmers and nightmare for debuggers.<br /><br />Finally the language is awesome ( except for exceptions ) :P .Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0tag:blogger.com,1999:blog-949656721177042677.post-50316513791511621252009-08-21T07:54:00.000-07:002009-08-21T21:47:47.509-07:00Thanks buddiesThanks a lot <a href="http://punchagan.wordpress.com/">punch</a> and <a href="http://cloud9trt.blogspot.com/">nine</a> for your brilliant idea <a href="http://lafootrix.wordpress.com/2009/08/09/loading/">LaThCoRe</a>.<br /><br />If not for lathcore, I would have totally forgotten that I had a <a href="http://nishi2rock.blogspot.com">blog</a> and was suppose to update it.<br />Anyways the moment I heard about this project, I had hell a lot of ideas to brag about.<br />I could not sort them out( actually lazy ) and so din bother much.<br /><br />today after chatting with punch, I've decided to blog something and then it struck to me that sometime back I wanted to create a blog about python. Here's my chance and hence this <a href="http://pyandme.blogspot.com/">blog</a>.Nishanthhttp://www.blogger.com/profile/15674967935728040240noreply@blogger.com0