[IPython-user] Completion in non-interactive windows (NOT a Windows question!)

Alexander Schmolck a.schmolck at gmx.net
Mon Jan 10 14:58:44 CST 2005

John J Lee <jjl at pobox.com> writes:

> On Sat, 8 Jan 2005, Fernando Perez wrote:
> [...]
>> 1.  What ipython does: your objects live in memory, and you can rely on
>> python's introspection capabilities to extract attribute names.  A hook to GNU
>> readline binds this to the tab key, but you could get the same results in a
>> gui where you have full control of keyboard events.
> This is also the python-mode project's approach, I see.
>> 3.  Writing a full-blown textual analyzer which can understand the structure
>> of python code as it is being written.  This would give you the full
>> capabilities of real completion in an editor, but it's also the most difficult
>> one to write.
> Trustudio and Pythonwin take this approach.
>> For an editor, ultimately #3 is what you really want, since you may be writing
>> code which can really not be loaded in your current environment (imagine
>> coding on your laptop something which can't realistically run outside of some
>> big database server).
> Sure.
>> I have to say that so far though, I've been able to function quite well with
>> just # 1 and 2.  IPython gives me accurate completion of live objects, and
> [...]
> I've come to the same conclusion.  Would of course also be pleased to see
> an unreasonable man/woman do this "properly" (ie. approach #3 above) in
> emacs.

3. Is of course impossible to do "properly" (i.e. you simply *can't* determine
everything you'd need for code completion to work 100% statically), but if all
you'd like is self.<tab> to do method completion on the methods of the current
class (and possibly superclass methods, too) then I think that's really fairly
trivial (i.e. one could in principle code up a version that handle's most
cases in less than 30 minutes -- either by just using emacs functionality or
if you prefer calling a python script from emacs that returns the required

Here is an outline of a primitive example algorithm for emacs:

- py-beginning-of-def-or-class (repeat till the def starts with self, otherwise
  it's just a local function definition)
- extract definition arguments per regexp or simply by moving by forward 2
  words and then copying each following word and moving forward to the next word)
- repeat the same for all defs at the same level till you reach a line that
  starts with 'class'
- Goto the end of the class with py-end-of-def-or-class and then move up
  definitions till you go past your original starting point

Properties are easily added but if you want completion on superclass methods
that requires of course a bit more work and you'll presumably want to cache
results (but even here a simple minded version should be quickly coded) --
have a look at bicycle repair man, though -- I'd think that they must already
have done most of the hard work so maybe it ought to be possible to leverage
that for code completion.


p.s: the above of course tacitly assumed that py-beginning-of-def-or-class
etc. actually work -- not sure whether that's the case, but even if it isn't
the case that shouldn't constitute much of an obstacle.

More information about the IPython-user mailing list