Note
This documentation is for a development version of IPython. There may be significant differences from the latest stable release (1.2.1).
Word completion for IPython.
This module is a fork of the rlcompleter module in the Python standard library. The original enhancements made to rlcompleter have been sent upstream and were accepted as of Python 2.3, but we need a lot more functionality specific to IPython, so this module will continue to live as an IPython-specific utility.
Original rlcompleter documentation:
This requires the latest extension to the readline module (the completes keywords, built-ins and globals in __main__; when completing NAME.NAME..., it evaluates (!) the expression up to the last dot and completes its attributes.
It’s very cool to do “import string” type “string.”, hit the completion key (twice), and see the list of names defined by the string module!
Tip: to use the tab key as the completion key, call
readline.parse_and_bind(“tab: complete”)
Notes:
Bases: object
Bases: object
An object to split an input line in a manner similar to readline.
By having our own implementation, we can expose readline-like completion in a uniform manner to all frontends. This object only needs to be given the line of text to be split and the cursor position on said line, and it returns the ‘word’ to be completed on at the cursor after splitting the entire line.
What characters are used as splitting delimiters can be controlled by setting the delims attribute (this is a property that internally automatically builds the necessary regular expression)
Return the string of delimiter characters.
Split a line of text with a cursor at the given position.
Bases: IPython.config.configurable.Configurable
Create a new completer for the command line.
Completer(namespace=ns,global_namespace=ns2) -> completer instance.
If unspecified, the default namespace where completions are performed is __main__ (technically, __main__.__dict__). Namespaces should be given as dictionaries.
An optional second namespace can be given. This allows the completer to handle cases where both the local and global scopes need to be distinguished.
Completer instances should be used as the completion mechanism of readline via the set_completer() call:
readline.set_completer(Completer(my_namespace).complete)
Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is evaluatable in self.namespace or self.global_namespace, it will be evaluated and its attributes (as revealed by dir()) are used as possible completions. (For class instances, class members are are also considered.)
WARNING: this can still invoke arbitrary C code, if an object with a __getattr__ hook is evaluated.
Return the next possible completion for ‘text’.
This is called successively with state == 0, 1, 2, ... until it returns None. The completion should begin with ‘text’.
Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names currently defined in self.namespace or self.global_namespace that match.
Bases: IPython.core.completer.Completer
Extension of the completer class with IPython-specific features
IPCompleter() -> completer
Return a completer object suitable for use by the readline library via readline.set_completer().
Inputs:
Wrapper around the complete method for the benefit of emacs and pydb.
Find completions for the given text and line context.
This is called successively with state == 0, 1, 2, ... until it returns None. The completion should begin with ‘text’.
Note that both the text and the line_buffer are optional, but at least one of them must be given.
Parameters: | text : string, optional
|
---|---|
Returns: | text : str
matches : list
|
Match filenames, expanding ~USER type strings.
Most of the seemingly convoluted logic in this completer is an attempt to handle filenames with spaces in them. And yet it’s not quite perfect, because Python’s readline doesn’t expose all of the GNU readline details needed for this to be done correctly.
For a filename with a space in it, the printed completions will be only the parts after what’s already been typed (instead of the full completions, as is normally done). I don’t think with the current (as of Python 2.3) Python readline it’s possible to do better.
Match magics
Match named parameters (kwargs) of the last open function
Match attributes or global python names
Return the state-th possible completion for ‘text’.
This is called successively with state == 0, 1, 2, ... until it returns None. The completion should begin with ‘text’.
Parameters: | text : string
|
---|
Return whether a string has open quotes.
This simply counts whether the number of quote characters of either type in the string is odd.
Returns: | If there is an open quote, the quote character is returned. Else, return False. |
---|
Escape a string to protect certain characters.
Expand ‘~’-style usernames in strings.
This is similar to os.path.expanduser(), but it computes and returns extra information that will be useful if the input was being used in computing completions, and you wish to return the completions with the original ‘~’ instead of its expanded value.
Parameters: | path : str
|
---|---|
Returns: | newpath : str
tilde_expand : bool
tilde_val : str
|
Does the opposite of expand_user, with its outputs.
key for sorting that penalizes magic commands in the ordering
Normal words are left alone.
Magic commands have the initial % moved to the end, e.g. %matplotlib is transformed as follows:
%matplotlib -> matplotlib%
[The choice of the final % is arbitrary.]
Since “matplotlib” < “matplotlib%” as strings, “timeit” will appear before the magic “%timeit” in the ordering
For consistency, move “%%” to the end, so cell magics appear after line magics with the same name.
A check is performed that there are no other “%” in the string; if there are, then the string is not a magic command and is left unchanged.
returns the strings in the __all__ attribute