/Doc/library/rlcompleter.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 66 lines · 46 code · 20 blank · 0 comment · 0 complexity · 0fa6296542d42e1c8140059c63a5bd76 MD5 · raw file

  1. :mod:`rlcompleter` --- Completion function for GNU readline
  2. ===========================================================
  3. .. module:: rlcompleter
  4. :synopsis: Python identifier completion, suitable for the GNU readline library.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. The :mod:`rlcompleter` module defines a completion function suitable for the
  7. :mod:`readline` module by completing valid Python identifiers and keywords.
  8. When this module is imported on a Unix platform with the :mod:`readline` module
  9. available, an instance of the :class:`Completer` class is automatically created
  10. and its :meth:`complete` method is set as the :mod:`readline` completer.
  11. Example::
  12. >>> import rlcompleter
  13. >>> import readline
  14. >>> readline.parse_and_bind("tab: complete")
  15. >>> readline. <TAB PRESSED>
  16. readline.__doc__ readline.get_line_buffer( readline.read_init_file(
  17. readline.__file__ readline.insert_text( readline.set_completer(
  18. readline.__name__ readline.parse_and_bind(
  19. >>> readline.
  20. The :mod:`rlcompleter` module is designed for use with Python's interactive
  21. mode. A user can add the following lines to his or her initialization file
  22. (identified by the :envvar:`PYTHONSTARTUP` environment variable) to get
  23. automatic :kbd:`Tab` completion::
  24. try:
  25. import readline
  26. except ImportError:
  27. print "Module readline not available."
  28. else:
  29. import rlcompleter
  30. readline.parse_and_bind("tab: complete")
  31. On platforms without :mod:`readline`, the :class:`Completer` class defined by
  32. this module can still be used for custom purposes.
  33. .. _completer-objects:
  34. Completer Objects
  35. -----------------
  36. Completer objects have the following method:
  37. .. method:: Completer.complete(text, state)
  38. Return the *state*th completion for *text*.
  39. If called for *text* that doesn't include a period character (``'.'``), it will
  40. complete from names currently defined in :mod:`__main__`, :mod:`__builtin__` and
  41. keywords (as defined by the :mod:`keyword` module).
  42. If called for a dotted name, it will try to evaluate anything without obvious
  43. side-effects (functions will not be evaluated, but it can generate calls to
  44. :meth:`__getattr__`) up to the last part, and find matches for the rest via the
  45. :func:`dir` function. Any exception raised during the evaluation of the
  46. expression is caught, silenced and :const:`None` is returned.