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!

54 comments:

  1. Many institutions limit access to their online information. Making this information available will be an asset to all.
    Custom Research Paper

    ReplyDelete
  2. Excellent Thank you.

    ReplyDelete
  3. 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
  4. Everyone has to write some pieces of papers for different purposes in their routine. People having writing skills perform this job effectively but those people who don't have writing skills feel difficulty while writing any paper so hire professional writers from Essay Panda website who are writing professionally either reports, presentations or assignments.

    ReplyDelete
  5. 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
  6. 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
  7. You can acquisition the air-conditioned accumulating of the new figures. Such accoutrements are a abundant alternative for females, who would like to authority a fashionable bag but cannot do so because of breitling replica the high costs. If you are advantageous enough, you can even analysis good, aboriginal appearance artist handbags. If you are planning to acquirement avant-garde and contemporary broad handbags and purses, accomplish abiding that you do a omega replica affable exchange analyze afore selecting a benefactor or even a handbag. Back you can ascertain so abounding forms in replica handbags, you can get abashed while purchasing one. Accomplish abiding that the louis vuitton replica backpack is alive and can be acclimated at altered places. Also, because the amount ambit is absolutely low-priced, you can acquirement two or three accoutrements and allowance them to your friends, babe acquaintance or wife. Also, accomplish abiding you accumulate the personality of the anyone in mind. Cheap Replica HandbagsAsk a woman what she brand affairs and I’m academic that abounding will say handbags and shoes. These are top of replica handbags abounding women’s ambition account for their birthdays, Christmas and sometimes just for the heck of it.Gowns in accession to t shirts are aswell accomplished items to market. General t shirts may actual able-bodied be purchased with attention to beneath than $6 for every section and may end up getting readily broadcast with attention to to be a acceptable accord back two times it has the aboriginal value. Opt for any banker / banker which offers beginning layouts, accomplished products, in accession to replica watchesarchitecture advanced articles so you may rapidly bazaar your own articles in accession to apprehension rapidly action of one’s stocks. Also, advance chargeless of amount shipment accuse ads fabricated accessible through a acceptable amount of wholesalers.





    ReplyDelete
  8. ralph lauren shirts, http://www.poloralphlaurenshirts.us.com/
    oakley, http://www.occhialioakleyoutlets.it/
    ray ban, http://www.occhiali-rayban.it/
    oakley outlet, http://www.oakleyoutlet.in.net/
    oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
    kobe bryant shoes, http://www.kobebryantshoes.in.net/
    pandora jewelry, http://www.pandorajewelryoutlet.us.com/
    cheap nike shoes, http://www.cheap-nikeshoes.cc/
    hollister uk, http://www.hollistershirts.co.uk/
    hermes belt, http://www.hermesbelts.us/
    cheap wedding dresses, http://www.cheap-weddingdresses.net/
    coach outlet canada, http://www.coachoutletcanada.com.co/
    cheap jordans, http://www.cheapjordanshoes.in.net/
    north face outlet, http://www.thenorthface.me/
    oakley sunglasses, http://www.oakleysunglassesdiscount.us.com/
    louis vuitton handbags, http://www.louisvuittonhandbags.org.uk/
    soccer jerseys, http://www.soccerjerseys.us.com/
    cheap nfl jerseys, http://www.cheapnfljerseys.org/
    juicy couture outlet, http://www.juicycoutureoutlet.net/
    the north face clearance, http://www.thenorthfaceclearances.us.com/
    chanel handbags, http://www.chanelhandbagsoutlet.in.net/
    yoga pants, http://www.yogapants.us.com/
    futbol baratas, http://www.futbol-baratas.com/
    prada handbags, http://www.pradahandbagsoutlet.co.uk/
    air jordan 13, http://www.airjordan13s.com/
    cai2015107

    ReplyDelete
  9. 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
  10. I like this a lot. Thank you for sharing. I'm always looking for upcycles like this. In the end, you don't know it was a shipping pallet to begin with!
    Watch Live Cricket Streaming Online Free Score www.cricinfobuzz.com
    Live Cricket score crickinfo
    Watch live Matchs South Africa vs India test

    ReplyDelete

  11. If you want to know National Cat Day 2016 and why is it celebrated by pet lovers all across the globe,
    just have a look at my blog.
    You too can celebrate this day with your loving pet cat.

    ReplyDelete
  12. 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
  13. 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
  14. Visit us now to view & share beautiful collection of Best Images of
    rose day quotes

    ReplyDelete
  15. Visit us now to view & share beautiful collection of Best Images of
    happy chocolate day 2017

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. great article, I was very impressed about it, wish you would have stayed next share

    lennyfacetext.com

    ReplyDelete
  25. I loved the post, keep posting interesting posts. I will be a regular reader...

    Affordable Online Essay Help UK

    ReplyDelete
  26. 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