Thursday, July 8, 2010

Tab completion in the python interpreter

Since I program mostly in python, I spend a lot of time in the default python interpreter of my distro. I even use it as a replacement for bash quite often - mainly because of python's excellent string manipulation capabilities and well, I confess, also because bash's syntax always baffles me (especially the if statements). So whenever I get stuck with some bash code, I take the escape route of opening up the python interpreter.

But the default python interpreter that comes bundled with most linux distros (and also the OS X), lacks a killer feature of bash: tab completion. One can find this feature in some python interpreters like bpython, but they sometimes overdo it (popping up suggestions even when one doesn't want them, for example).

Anyway, I recently learned from this excellent blog post by Matthias Friedrich that it is REALLY easy to add tab completion in the python interpreter: all one needs to do is import rlcompleter and readline modules of python and assign a completer key (Tab is the most natural choice, of course). The blog post I linked above also describes how to set the PYTHONSTARTUP environment variable so that tab completion is enabled every time the interpreter is opened.

Since python's readline module uses the same GNU readline utility used by bash for tab-completion, it should be just as much fun, right? Well, that's not quite the case. Because tab completion imposes a severe restriction on you here: you cannot use the Tab key for indenting your code anymore. And using single space for python's code indentation is a very bad idea: trying to distinguish between different indentation levels in heavily nested code blocks is a trouble not worth taking even for sake of the benefits given by tab completion.

Anyway, I figured out a way to circumvent the problem by looking at these lines in the source code of the rlcompleter module (The file in question is /usr/lib/python2.6/rlcompleter.py for Debian and Ubuntu): When one presses the completion key the function complete(self, text, state) defined in line#29 is called with the word being typed as it's second argument. I simply added an extra elif after the 42nd line asking it to return '\t' if the argument consists of only whitespaces: So, now I have all the goodness of tab completion without losing the ability of using tab for code indentation!

28 comments:

  1. Excellent Thank you.

    ReplyDelete
  2. For most kids, Santa already rates pretty highly replica belts in their eyes, given the whole presents, elves and magical reindeer schtick. For the kids stuck behind a car crash in Coppell, Texas early last week prada replica, the jolly old elf has likely moved to hero status.

    Former Arlington, Texas volunteer fire fighter chanel outlet Brad Luddeke was on his way to deliver toys to needy kids when he came across the accident on Interstate 635. Luddeke training louis vuitton outlet went into full swing, as he pulled a driver from one of the crashed vehicles not only once, but again, when he noted the car replica handbags catching fire.

    ReplyDelete
  3. Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I'm hoping to offer one thing again and aid others such as you helped me. seo manchester

    ReplyDelete
  4. acheter vente maillot nba pas acheter vente maillot nba pas cher cher Such bones humble and flatter not just the security they have, many people are present, because acheter vente maillot nba pas cher one acheter vente maillot nba pas cher kind of fear and a desire, or almost impossible for the kind of opportunity. cheap to pay the acheter vente maillot nba pas cher fare, because the http://www.nailartmetisse.fr poor sale after watching the film it is no extra money, of course, left the next bucks in his pocket, pulled out to not only make the .

    ReplyDelete
  5. Being a interpreter is a very nice and interesting job but this job is not easy. An interpreter should be very educated and perfect in his job and in uk essay writing service and for this purpose he have to do much study and struggle.

    ReplyDelete
  6. Its very good and very excellent article. i really enjoyed this post and i hope you will keep posting this kinds of post in future.

    ReplyDelete
  7. valentine day Facebook profile pic cute valentines day quotes valentines day quotes for him to make someone happy, and by this, you can also make someone your Valentine. You can decorate your PC and mobile screen on this event by these type of images

    ReplyDelete
  8. Visit us now to view & share beautiful collection of Best Images of
    rose day quotes

    ReplyDelete
  9. It can be hard to break through to the misfortune alleviation office if gathering specialists are debilitated from exchanging calls. This is one of the advantages of having an assistant, for example, a lawyer or a lodging advisor. The first will scare charge authorities and the second may have contacts inside the misfortune mit office. car title loans

    ReplyDelete
  10. They were so pleasant. They clarified that losing a vocation was the ideal motivation behind why the bank ought to adjust their credit. They knew precisely who to converse with at the bank. They thought of an advance alteration plan and conveyed frames for Joe and Dawn to round out. Check Cashing

    ReplyDelete
  11. Managing an account organizations and money related loan specialists typically offer data just about their items. Their sites have data identifying with their product offering and you may locate some deceptive information that makes it troublesome for you to comprehend which items are really the best. cash advance corona

    ReplyDelete
  12. The fortunate thing about secured home credit is that it is offered with lower financing cost. As a result of the security gave this kind of credit accompanies low intrigue. cash advance

    ReplyDelete
  13. Home value advances, when used to diminish obligation, for example, Visas, are a decent approach to help recover your FICO assessment fit as a fiddle. When taking out your new terrible credit home advance, you will need to search around a bit to get the absolute best arrangement conceivable on your advance terms. cash advance

    ReplyDelete
  14. I'm certain that I don't need to clarify what you could do with the cash, however you may be upbeat to realize that with a dealer credit; there are no confinements on how you spend it. Regardless of whether it be to make up for lost time with bills, make finance or extend your advertising endeavors, having the additional working capital can truly have any kind of effect in your business. payday loans chula-vista

    ReplyDelete
  15. On the off chance that your application is affirmed, there is a couple of various elements that are included. There is be sum took into account financing, a rate of that sum that the bank will consider a service agreement, and the quantity of rate focuses that the merchant can add to the credit. car title loans

    ReplyDelete
  16. This implies those hoping to purchase condo structures should be keen with their choices and ensure that he is working with a trustworthy organization that has involvement with this kind of venture when the economy is low. The accompanying is some present data about how the home loan banks are functioning. car title loans

    ReplyDelete
  17. On the off chance that you are hoping to settle your loan obligation, you will need to investigate your accounts. Settling any sort of obligation is a procedure when your paycheck is attempting to make least installments. Since a loan online is a transient credit, intrigue includes quick. Keep these minimal effort advances here and now when you pay them off on the first due date.

    ReplyDelete
  18. Kenapa wanita ingin melakukan cara menggugurkan kandungan untuk mengakhiri kehamilan yang tidak di inginkan dengan paksa . Ada beberapa alasan seseorang ingin melakukan cara menggugurkan hamil seperti terlalu banyak anak , tidak siap menjadi ibu , masing mementingkan karir dan sebagainya . Menggugurkan kandungan mempunyai tingkat risiko yang sangat tinggi dan berat . Oleh sebab itu Anda harus memikirkan cara aborsi aman agar tidak membahayakan diri Anda . telat datang bulan merupakan hal yang wajar yang di alami oleh wanita , lantas bagaimana
    cara melancarkan haid ? Menjaga pola makan sehat dan hindari stress merupakan salah satu untuk mengatasi haid tidak lancar . Bagi pria perlu juga untuk mengetahui cara merangsang wanita agar pasangan Anda lebih bergairah dalam melakukan hubungan bersama Anda .

    ReplyDelete
  19. This information was really valuable, hope you keep it updated. Jual Lemari Arsip

    ReplyDelete
  20. I thank you for the information and articles you provided

    ReplyDelete
  21. With the progression and modernisation of the general public, the requirement for 'recreation' has turned into a vital one which ought not be disregarded. Barbacue Catering

    ReplyDelete
  22. Albeit a great many people go to school and graduate one of the various devoted instructive projects that are accessible, a few competitors can acquire certificate on the off chance that they have a four year college education that isn't explicit for recreational treatment, Gym Flooring

    ReplyDelete