FAQ
hello,

I'm trying to implement autocompletion into my editor.
But I find some weird behavior,
or at least I don't have the faintest idea why this behavior occures,
and even more important how to solve it
In the example below I try to autocomplete " wx.s" , which in my humble
opinion should at least produce "wx.stc" (and some others ).

The completion is done in a procedure, so the "locals" is quite clean.
Before calling the completer, I import the left part of my string to be
completed.
The program below gives indeed "wx.stc" as an option,
but if I leave the second line out, marker "<<<===" ,
I get no results.

Can someone explain this behavior and
maybe even have a solution ?

thanks,
Stef Mientki


import rlcompleter
import wx.stc # <<<===

# ***********************************************************************
# ***********************************************************************
def _get_completions ( word ) :
left_part = None
right_part = word
if word.find('.') >= 0 :
word_parts = word.split('.')
left_part = '.'.join ( word_parts [ : -1 ] )
right_part = word_parts [ -1 ]

try :
exec ( 'import ' + left_part )
except :
return None

Completer = rlcompleter.Completer ( locals () )
State = 0
Next = Completer.complete ( word, State )
result = []
while Next :
result.append ( Next )
State += 1
Next = Completer.complete ( word, State )

result = ' '.join ( result )
return result
# ***********************************************************************


# ***********************************************************************
# ***********************************************************************
if __name__ == "__main__":

word = 'wx.s'
completions = _get_completions ( word )
print completions
# *******************************************************

Search Discussions

  • Gabriel Genellina at Sep 30, 2008 at 4:34 am
    En Sun, 28 Sep 2008 19:25:30 -0300, Stef Mientki <stef.mientki at gmail.com>
    escribi?:
    I'm trying to implement autocompletion into my editor.
    But I find some weird behavior,
    or at least I don't have the faintest idea why this behavior occures,
    and even more important how to solve it
    In the example below I try to autocomplete " wx.s" , which in my humble
    opinion should at least produce "wx.stc" (and some others ).
    wx is a package. Modules within the package are not, by default,
    attributes of the package - unless they're imported in __init__.py or your
    code imports them.
    So the autocompleter is doing the right thing - wx.stc does not exist
    until it is explicitely imported.

    py> import wx
    py> wx.stc
    Traceback (most recent call last):
    ...
    AttributeError: 'module' object has no attribute 'stc'
    py> 'stc' in dir(wx)
    False
    py> import wx.stc
    py> wx.stc
    <module 'wx.stc' from '...'>
    py> 'stc' in dir(wx)
    True

    --
    Gabriel Genellina
  • Stef Mientki at Sep 30, 2008 at 7:06 pm

    Gabriel Genellina wrote:
    En Sun, 28 Sep 2008 19:25:30 -0300, Stef Mientki
    <stef.mientki at gmail.com> escribi?:
    I'm trying to implement autocompletion into my editor.
    But I find some weird behavior,
    or at least I don't have the faintest idea why this behavior occures,
    and even more important how to solve it
    In the example below I try to autocomplete " wx.s" , which in my
    humble opinion should at least produce "wx.stc" (and some others ).
    wx is a package. Modules within the package are not, by default,
    attributes of the package - unless they're imported in __init__.py or
    your code imports them.
    So the autocompleter is doing the right thing
    in what perspective ?
    the autocompleter is only meant to assist the program writer ;-)
    - wx.stc does not exist until it is explicitely imported.
    I guess I've to study the package.
    For the moment I'll implement a user editable list of additions.

    But with your remarks I tried __all__
    And now I wonder why rlcompleter is not simply using "wx.__all__",
    it than does gets all the items ?

    cheers,
    Stef
  • Gabriel Genellina at Sep 30, 2008 at 8:07 pm
    En Tue, 30 Sep 2008 16:06:07 -0300, Stef Mientki <stef.mientki at gmail.com>
    escribi?:
    Gabriel Genellina wrote:
    En Sun, 28 Sep 2008 19:25:30 -0300, Stef Mientki
    <stef.mientki at gmail.com> escribi?:
    I'm trying to implement autocompletion into my editor.
    But I find some weird behavior,
    or at least I don't have the faintest idea why this behavior occures,
    and even more important how to solve it
    In the example below I try to autocomplete " wx.s" , which in my
    humble opinion should at least produce "wx.stc" (and some others ).
    wx is a package. Modules within the package are not, by default,
    attributes of the package - unless they're imported in __init__.py or
    your code imports them.
    So the autocompleter is doing the right thing
    in what perspective ?
    the autocompleter is only meant to assist the program writer ;-)
    It's hard to write an autocompleter that does the right thing in all cases
    :)

    For a package, you have several sources for possibly valid attributes:

    - its dir() (that is, the package object's own attributes)
    - the __all__ attribute, when it exists
    - the list of modules inside the package directory or directories (given
    by its __path__ attribute)

    Sometimes __init__.py is empty - and enumerating the modules inside the
    directory is the right thing to do. Sometimes the author explicitely
    imports things in __init__.py, things that comprise the public interfase
    to the package. In that case I'd not like to see "private" modules
    appearing in the list.
    Combine with __all__, which might be defined or not. Mix all those, choose
    your own autocomplete algorithm, and see what happens...
    - wx.stc does not exist until it is explicitely imported.
    I guess I've to study the package.
    For the moment I'll implement a user editable list of additions.

    But with your remarks I tried __all__
    And now I wonder why rlcompleter is not simply using "wx.__all__",
    it than does gets all the items ?
    __all__ isn't always defined. It's only used when you do "from xxx import
    *" AFAIK.

    --
    Gabriel Genellina
  • Stef Mientki at Sep 30, 2008 at 8:18 pm

    Gabriel Genellina wrote:
    En Tue, 30 Sep 2008 16:06:07 -0300, Stef Mientki
    <stef.mientki at gmail.com> escribi?:
    Gabriel Genellina wrote:
    En Sun, 28 Sep 2008 19:25:30 -0300, Stef Mientki
    <stef.mientki at gmail.com> escribi?:
    I'm trying to implement autocompletion into my editor.
    But I find some weird behavior,
    or at least I don't have the faintest idea why this behavior
    occures,
    and even more important how to solve it
    In the example below I try to autocomplete " wx.s" , which in my
    humble opinion should at least produce "wx.stc" (and some others ).
    wx is a package. Modules within the package are not, by default,
    attributes of the package - unless they're imported in __init__.py
    or your code imports them.
    So the autocompleter is doing the right thing
    in what perspective ?
    the autocompleter is only meant to assist the program writer ;-)
    It's hard to write an autocompleter that does the right thing in all
    cases :)

    For a package, you have several sources for possibly valid attributes:

    - its dir() (that is, the package object's own attributes)
    - the __all__ attribute, when it exists
    - the list of modules inside the package directory or directories
    (given by its __path__ attribute)

    Sometimes __init__.py is empty - and enumerating the modules inside
    the directory is the right thing to do. Sometimes the author
    explicitely imports things in __init__.py, things that comprise the
    public interfase to the package. In that case I'd not like to see
    "private" modules appearing in the list.
    Combine with __all__, which might be defined or not. Mix all those,
    choose your own autocomplete algorithm, and see what happens...
    - wx.stc does not exist until it is explicitely imported.
    I guess I've to study the package.
    For the moment I'll implement a user editable list of additions.

    But with your remarks I tried __all__
    And now I wonder why rlcompleter is not simply using "wx.__all__",
    it than does gets all the items ?
    __all__ isn't always defined. It's only used when you do "from xxx
    import *" AFAIK.
    thanks Gabriel,
    very valuable information for me,
    I'll bookmark this message for later use.
    cheers,
    Setf

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 28, '08 at 10:25p
activeSep 30, '08 at 8:18p
posts5
users2
websitepython.org

2 users in discussion

Stef Mientki: 3 posts Gabriel Genellina: 2 posts

People

Translate

site design / logo © 2022 Grokbase