Rochester Python Meetup Message Board › help with tab-completion in a python program

help with tab-completion in a python program

Adam H.
user 9995744
Rochester, NY
Post #: 5

Hi, everyone.

I'm working on a program that uses mostly the terminal for input, and it badly needs tab-completion, especially for filenames. I've found a few examples on the web, but none seem to work smoothly. (On my Ubuntu system, both 12.04 and 10.04, one problem is that readline is not catching a leading "/", so I can't start searching at the root directory.

The code in github gist linked below is almost right, but it needs a little work.

Is anyone interested in fiddling with it, or does anyone know of simple method or library I have overlooked? It seems like there should be a built-in filename completer, or some simple way (simpler than mine) to do such a universal thing!

One warning--on both my Ubuntu Thinkpad and my OS X machines, I have managed to bog down the whole system by fiddling with readline "completers." I guess that the buffers I am working in are not dedicated my shell or program. The example here doesn't do any damage, but be careful.

https://gist.github.com/4040404­

Adam

Seán H.
Sean_Hayes
Rochester, NY
Post #: 4
I did some searching and couldn't find out why text doesn't include slashes. It may have something to do with the way GNU readline works.

The code you posted works on my laptop though. Also, if you comment out "readline.set_completer(complete)"­, it will use the built in completer function (I verified this by adding some print statements to the beginning of complete()). I'm using UBuntu 12.04 and Python 2.7.3.
Adam H.
user 9995744
Rochester, NY
Post #: 6
I did some searching and couldn't find out why text doesn't include slashes. It may have something to do with the way GNU readline works.

Strange.

The code you posted works on my laptop though. Also, if you comment out "readline.set_completer(complete)"­, it will use the built in completer function (I verified this by adding some print statements to the beginning of complete()). I'm using UBuntu 12.04 and Python 2.7.3.

This is all strange. The default completer won't do anything above the working directory. I can't get it to see "/" (root), ".." the upper directory, /home/hayes/ etc.

Good that it mostly works. I guess it is worth finishing, since no one has shown that I'm re-inventing the wheel.

On another note--is there a better venue for technical discussions unrelated to a meetup, maybe reddit or something?
Seán H.
Sean_Hayes
Rochester, NY
Post #: 5
Stackoverflow is good.
David R.
user 6950319
Scottsville, NY
Post #: 2
Adam, Are you using the readline support from the standard library? It works on all flavors of Unix (Linux, Mac OSX, etc.) Also, can you give a specific code example of what you are doing? -Dave
Adam H.
user 9995744
Rochester, NY
Post #: 7

Thanks, Sean. I don't think you can just have chit-chat in a group on stackoverflow, can you?

Adam, Are you using the readline support from the standard library? It works on all flavors of Unix (Linux, Mac OSX, etc.) Also, can you give a specific code example of what you are doing? -Dave

Yeah, I'm using readline. I tried "cmd" library, but it did not contain the objects it was supposed to.

I have something almost working (again), but done more sensibly with calls to os.path instead of lots of try/excepts. If you're interested, have a look here. I'm working on it at the moment.

https://gist.github.com/4040404­
Seán H.
Sean_Hayes
Rochester, NY
Post #: 6
Hmm, most websites where you can talk about programming are for getting help with a specific problem you're having. I'm not sure where you'd go for a casual conversation.
Adam H.
user 9995744
Rochester, NY
Post #: 8
Finally, something that works. It just needs a little clean-up. See the comments in the snippet for the author. I just made little changes to get it to tab-complete file paths by default.

My gist with a completely new snippet.

--Adam
Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy