Auto-completion: why not module name?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Candide Dandide
Python 3.4 provides auto-completion facility within a Python console embedded in a command line terminal.


But apparently this facility doesn't allow the user to complete with standard module name. For instance, editing the following :

>>> import ma

and hitting the TAB key doesn't generate

math

as I was expecting but the following strings :

map( max(

[tested on Ubuntu 12.10]

Is it the expected behaviour ?


Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Chris Angelico
On Tue, Mar 17, 2015 at 3:23 AM, candide <c.candide at laposte.net> wrote:

> Python 3.4 provides auto-completion facility within a Python console embedded in a command line terminal.
>
>
> But apparently this facility doesn't allow the user to complete with standard module name. For instance, editing the following :
>
>>>> import ma
>
> and hitting the TAB key doesn't generate
>
> math
>
> as I was expecting but the following strings :
>
> map( max(
>
> [tested on Ubuntu 12.10]
>
> Is it the expected behaviour ?

Looks to me like it's just doing the normal tab-completion of globals,
rather than having a special case for the 'import' statement. So what
you're talking about here is a feature request:

When the input begins "import ", please can tab-completion enumerate
available modules?

This is a very plausible feature request, but be aware that it will
involve a very costly disk search. Figuring out what modules could be
imported means going through the entire Python module search path,
enumerating .py (and other) files, and that could be a lot slower than
you think.

It might even already be available, but disabled by default (because
of that cost). But someone other than me will be better placed to
answer that.

ChrisA


Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Jonas Wielicki
On 16.03.2015 17:40, Chris Angelico wrote:
> This is a very plausible feature request, but be aware that it will
> involve a very costly disk search. Figuring out what modules could be
> imported means going through the entire Python module search path,
> enumerating .py (and other) files, and that could be a lot slower than
> you think.

With possible side-effects, like help("modules") has. I don?t think that
this is a trivial task (and if it is, why is help("modules") not
implemented in that trivial way?)

regards,
jwi

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150316/0ceaca95/attachment.sig>

Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Ian Kelly-2
In reply to this post by Chris Angelico
On Mon, Mar 16, 2015 at 10:40 AM, Chris Angelico <rosuav at gmail.com> wrote:

> Looks to me like it's just doing the normal tab-completion of globals,
> rather than having a special case for the 'import' statement. So what
> you're talking about here is a feature request:
>
> When the input begins "import ", please can tab-completion enumerate
> available modules?
>
> This is a very plausible feature request, but be aware that it will
> involve a very costly disk search. Figuring out what modules could be
> imported means going through the entire Python module search path,
> enumerating .py (and other) files, and that could be a lot slower than
> you think.

Completeness would also be an issue. There is no general requirement
that module finders use the file system at all (one could write a
finder and loader to import modules from the network, for instance),
nor is there any API for enumerating the modules available.

Another difficulty would be packages. You can't generally know whether
foo.bar is a module until you've already imported foo.


Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Steven D'Aprano-11
In reply to this post by Candide Dandide
On Tuesday 17 March 2015 03:23, candide wrote:

> Python 3.4 provides auto-completion facility within a Python console
> embedded in a command line terminal.
>
>
> But apparently this facility doesn't allow the user to complete with
> standard module name. For instance, editing the following :
>
>>>> import ma
>
> and hitting the TAB key doesn't generate
>
> math
>
> as I was expecting but the following strings :


You might like my tab completion and command history module:

http://code.google.com/p/tabhistory/

I've been using it on Linux for about three or four years, and although I
don't promise it is bug-free, it shouldn't blow up your computer :-)

It supports module completion in `import` and `from ... import` statements.
It even supports module attribute completion if the module is already
cached.

E.g. if the re module is cached, typing

    from re import ma[TAB}

will compete the "ma" to "match".


By default, the tabhistory module:

* indents at the start of the line
* completes on module names in `import` and `from` statements
* completes on file names inside strings
* and completes on global and builtin names and keywords everywhere else.


You can read an announcement here:

http://code.activestate.com/lists/python-list/672898/


Feedback from Mac and Windows users is very, very welcome.





--
Steve



Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Oscar Benjamin-2
On 17 March 2015 at 08:10, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Tuesday 17 March 2015 03:23, candide wrote:
>
> You might like my tab completion and command history module:
>
> http://code.google.com/p/tabhistory/
>
> I've been using it on Linux for about three or four years, and although I
> don't promise it is bug-free, it shouldn't blow up your computer :-)

Good work Steven. I just gave it a try and it works great.

> It supports module completion in `import` and `from ... import` statements.
> It even supports module attribute completion if the module is already
> cached.
>
> E.g. if the re module is cached, typing
>
>     from re import ma[TAB}
>
> will compete the "ma" to "match".

BTW ipython does this and goes one step further. It basically imports
the module while tab completion is ongoing so that you can complete
"from ...import" without needing to import the module first. In
principle that could be problematic but in practice I find it useful
and it has never caused me any actual problems.

> By default, the tabhistory module:
>
> * indents at the start of the line

Any reason for using 8 spaces for a tab? Or is that just my terminal
(gnome-terminal)?

> * completes on module names in `import` and `from` statements
> * completes on file names inside strings

I'm not sure what causes this but I have a symlink in my user
directory called "current" that just takes me to the things I'm
currently working on. When I tab complete it it puts in a quote
character:

>>> with open('current'

I think it thinks that "current" is a complete filename when it's
actually a symlink to a directory containing other things. If I delete
the quote and add a slash then it continues to complete normally.

Not sure if it's relevant but my .inputrc has:
set mark-symlinked-directories on

This setting changes the way that symlinks to directories are
completed so that a slash is automatically added when I tab complete
them.

> * and completes on global and builtin names and keywords everywhere else.
>
> You can read an announcement here:
>
> http://code.activestate.com/lists/python-list/672898/
>
> Feedback from Mac and Windows users is very, very welcome.

On Linux unfortunately.


Oscar


Reply | Threaded
Open this post in threaded view
|

Auto-completion: why not module name?

Steven D'Aprano-11
In reply to this post by Steven D'Aprano-11
On Tue, 17 Mar 2015 11:22 pm, Oscar Benjamin wrote:

> On 17 March 2015 at 08:10, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> On Tuesday 17 March 2015 03:23, candide wrote:
>>
>> You might like my tab completion and command history module:
>>
>> http://code.google.com/p/tabhistory/
>>
>> I've been using it on Linux for about three or four years, and although I
>> don't promise it is bug-free, it shouldn't blow up your computer :-)
>
> Good work Steven. I just gave it a try and it works great.

Thank you!


>> It supports module completion in `import` and `from ... import`
>> statements. It even supports module attribute completion if the module is
>> already cached.
>>
>> E.g. if the re module is cached, typing
>>
>>     from re import ma[TAB}
>>
>> will compete the "ma" to "match".
>
> BTW ipython does this and goes one step further. It basically imports
> the module while tab completion is ongoing so that you can complete
> "from ...import" without needing to import the module first. In
> principle that could be problematic but in practice I find it useful
> and it has never caused me any actual problems.

I thought about that, but I decided against it because of the security risk:

from evil_module_of_doom import something

Just tab-completing the name "something" shouldn't run the evil module. I
suppose I could add it as an optional feature, defaulting to off. Or try
parsing the source code.


>> By default, the tabhistory module:
>>
>> * indents at the start of the line
>
> Any reason for using 8 spaces for a tab? Or is that just my terminal
> (gnome-terminal)?

By default, it should indent with an actual tab character, which most
terminals treat as 8-spaces wide. But you can change that by setting
completer.indent to whatever string you want. Are you sure it is using
actual spaces?


>> * completes on module names in `import` and `from` statements
>> * completes on file names inside strings
>
> I'm not sure what causes this but I have a symlink in my user
> directory called "current" that just takes me to the things I'm
> currently working on. When I tab complete it it puts in a quote
> character:
>
>>>> with open('current'
>
> I think it thinks that "current" is a complete filename when it's
> actually a symlink to a directory containing other things. If I delete
> the quote and add a slash then it continues to complete normally.

Weird. It shouldn't be adding a close quote. Mind you, there appears to be
at least one other bug in the file name completion, so I need to work on
that soon.

However, I can reproduce the bug, so I'll work on that too. Thank you for
the report!



--
Steven



Reply | Threaded
Open this post in threaded view
|

Auto-completion of Unicode names [was why not module name?]

Steven D'Aprano-11
In reply to this post by Candide Dandide
Speaking of tab completion, would anyone be interested in being able to
auto-complete \N{...} unicode character names? I'm considering that as an
enhancement to my tabhistory module.

Python supports \N{...} backslash escapes in Unicode strings, so we can
write things like:

py> print(u"\N{CYRILLIC CAPITAL LETTER ZE WITH DESCENDER}")
?


There are currently somewhere in the vicinity of 110 thousand such names.



Any interest?



--
Steven



Reply | Threaded
Open this post in threaded view
|

Auto-completion of Unicode names [was why not module name?]

Chris Angelico
On Wed, Mar 18, 2015 at 10:29 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:

> Speaking of tab completion, would anyone be interested in being able to
> auto-complete \N{...} unicode character names? I'm considering that as an
> enhancement to my tabhistory module.
>
> Python supports \N{...} backslash escapes in Unicode strings, so we can
> write things like:
>
> py> print(u"\N{CYRILLIC CAPITAL LETTER ZE WITH DESCENDER}")
> ?
>
>
> There are currently somewhere in the vicinity of 110 thousand such names.

Not from me, no. I don't usually use \N escapes - it's usually easiest
to do an external search to figure out the code point, and then either
paste in the character itself (assuming it's not a combining
character, or whitespace, or something, where the loss of clarity
would be a problem), or just use a \u or \U escape. If I knew the
names perfectly, then I might use \N escapes, but even with tab
completion, they'd be quite slow to key in.

How many people actually use \N escapes and hand-enter them?

ChrisA


Reply | Threaded
Open this post in threaded view
|

Auto-completion of Unicode names [was why not module name?]

Dan Sommers
In reply to this post by Steven D'Aprano-11
On Wed, 18 Mar 2015 10:29:53 +1100, Steven D'Aprano wrote:

> Speaking of tab completion, would anyone be interested in being able
> to auto-complete \N{...} unicode character names? I'm considering that
> as an enhancement to my tabhistory module.

Only if it's fuzzy.  One use case is that "opening curly quote"
character, except that I don't remember that it's actually a "left
double quotation mark."  Once I type "open," or "quot," no amount of
ordinary left-to-right / startswith completion will help me.  And if I
have to look it up somewhere, then I can probably just copy/paste an
actual character or a U+XXXX code once I find it.

> Python supports \N{...} backslash escapes in Unicode strings, so we
> can write things like:
>
> py> print(u"\N{CYRILLIC CAPITAL LETTER ZE WITH DESCENDER}")
> ?

Or I can just enter ? directly using my OS' preferred input method(s),
which is in all likelihood easier and more familiar and comfortable? for
someone who has a need for a ? in the first place.  (I am not one of
those people; I just copied and pasted the ? that you provided.)

> There are currently somewhere in the vicinity of 110 thousand such
> names.

I think you just made my point about the completion being fuzzy.  ;-)

Dan

? Okay, so that's just an *opinion*.