Monday, February 15, 2010

The Vim mode

Vim Forever...

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.

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.

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 Byte of Vim". It was so amazing and I started using vi like a novice vimmer.

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 Vimperator. 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.

Now comes the most important job. Making Vim an ide for Python.
Let me start by listing a few things one does on a typical ide.
  1. A file browser that stays on left of screen and enables opening files on one click
  2. Ability to show multiple files in one screen
  3. Syntax highlighting and code folding
  4. Taglist that displays all the functions, classes and stuff in one place
  5. Auto Indentation
  6. Code completion
  7. Debugging
Phew!! finally the list ended.
Lets start with the first, The File browser
Vim has a very simple and light file browser plugin called NERDTree.
Installing is as simple as extracting the files into ~/.vim directory.
Using :NERDTree brings up the nerdtree and a tree structure is displayed.
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.

Showing multiple files.
This is as simple as splitting the screen into parts and showing different files on each screen.
:sp splits the screen horizontally and :vsp splits vertically.
If you notice, we have seen something similar while using nerdtree. What it is doing is simply opening another buffer for itself.
<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. can also be mixed with hjkl.

syntax highlighting and code folding
vim takes care of syntax and all we have to do is :syntax on. You can also download a syntax file from here or write one of your own and name it python.vim. Installing is as simple as placing it in ~/.vim/syntax folder.
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.

taglist 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.
The visibility of taglist can be toggled using :TlistToggle and it works similar to nerdtree.

Autoindentation
following pep8, here are a few settings that should make your life easy.
  • set expandtab
  • set textwidth=79
  • set tabstop=8
  • set softtabstop=4
  • set shiftwidth=4
  • set autoindent
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.
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 this 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.

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.
So your python.vim would look like this:

setlocal expandtab
setlocal textwidth=79
setlocal tabstop=8
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal autoindent

Code completion and Debugging are for a different set of audience ( not me ).
But shall blog about it soon.

Tuesday, September 29, 2009

GAE + Django = Godlike

Imagine 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!!!.
The tool here is Django and the platform is GAE - Google App Engine.

I first time I heard the name Django was in a blog post. 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.

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.

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, The Django Book. 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.

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 WSGI-compliant framework. Hence, it also supports Django and I am already starting to hit the limits of my imagination on the possibilities using them.

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 http://aralbalkan.com/1348.

I am going with Django because it is simply too good.

Saturday, September 26, 2009

PyCon India 2009 - Day 1

I came all the way without staying at my home town even skipping Dusshera celebrations to attend PyCon.
So you can understand how excited I was.
The day before the event..
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.
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.

The day of event ( Today )
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.
My first opinion about the organization was, it is good ( looking at the registration and stuff ).

The first presentation was the keynote, 'My Adventures with Python' by Prabhu Ramachandran.
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.

He talked about his relationship with python and the areas of using python( almost everywhere ).
Then he talked about 3d simulations in python and how Mayavi came into existence.
The presentation in overall was so nice that every other talk after that seemed dull.

Then there were many talks that ran in parallel and here is an overview of the talks I attended.

After the keynote, I attended the talk on network security tools using python.
This one discussed briefly about pypcap,dpkt and scapy.
I wasnt much interested in the first place and even the talk wasnt so gr8.

The next talk I attended was on algorithms in python and it was about the merge sort, bubble sort and stuff.

Then the next talk was about Design Patterns in python which I was really looking forward to.
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.

The next talk was on Idiomatic Python.
Though I have already read about Idioms in python, I attended the talk.
The guy who gave the talk was some college student in his final year.
The presentation was excellent. He precisely knew what he was talking about.

The last talk was on Django.
There were some three guys giving the talk and even this one was awesome.
I got few doubts on Django cleared and decided to develop applications using django.

The day 1 went on pretty well.
Now looking at the schedule for day 2, I dont see any interesting talks,
There were a few proposed talks on python for mobile app development and stuff.
Dunno why they have been cancelled.

The PyCon India 2009 conference is good.
The talks were also decent enough considering this is the first PyCon in india.

Looking forward to many more talks and a much more better PyCon in 2010.

Monday, September 7, 2009

Python OOP tutorial - made simple

This is my third post and I actually thought of writing a tutorial on python.
But I see a lot of tutorials on python on the web at various levels of detail.
Here are a few for a newbie.

A Byte of Python
Python website
Python in 100 session lines

Now Python is an OO language.
For someone new to concept of OOP, the above tutorials are a little confusing
Here is a great tutorial that explains the concept of OOP and how it is done in python.
Introduction to classes

Saturday, August 22, 2009

When I first saw python

I 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.

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.

And that was how python became my favourite language.

Friday, August 21, 2009

My first programming language

I 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.

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

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.

Usually perl code is very small and a lil cryptic, it can be fun for programmers and nightmare for debuggers.

Finally the language is awesome ( except for exceptions ) :P .

Thanks buddies

Thanks a lot punch and nine for your brilliant idea LaThCoRe.

If not for lathcore, I would have totally forgotten that I had a blog and was suppose to update it.
Anyways the moment I heard about this project, I had hell a lot of ideas to brag about.
I could not sort them out( actually lazy ) and so din bother much.

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 blog.