generics.py
64 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r2498 | # encoding: utf-8 | ||
Brian Granger
|
r2205 | """Generic functions for extending IPython. | ||
vivainio
|
r851 | |||
Fernando Perez
|
r1229 | See http://cheeseshop.python.org/pypi/simplegeneric. | ||
vivainio
|
r851 | |||
Brian Granger
|
r2498 | Here is an example from IPython.utils.text:: | ||
vivainio
|
r851 | |||
def print_lsstring(arg): | ||||
Fernando Perez
|
r1229 | "Prettier (non-repr-like) and more informative printer for LSString" | ||
vivainio
|
r851 | print "LSString (.p, .n, .l, .s available). Value:" | ||
print arg | ||||
print_lsstring = result_display.when_type(LSString)(print_lsstring) | ||||
Brian Granger
|
r2205 | """ | ||
vivainio
|
r851 | |||
Brian Granger
|
r2498 | #----------------------------------------------------------------------------- | ||
# Copyright (C) 2008-2009 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2205 | from IPython.core.error import TryNext | ||
Fernando Perez
|
r1229 | from IPython.external.simplegeneric import generic | ||
Brian Granger
|
r2498 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r2205 | @generic | ||
vivainio
|
r851 | def result_display(result): | ||
Brian Granger
|
r2205 | """Print the result of computation.""" | ||
vivainio
|
r851 | raise TryNext | ||
Brian Granger
|
r2498 | |||
Brian Granger
|
r2205 | @generic | ||
vivainio
|
r851 | def inspect_object(obj): | ||
Brian Granger
|
r2205 | """Called when you do obj?""" | ||
vivainio
|
r851 | raise TryNext | ||
vivainio
|
r908 | |||
Brian Granger
|
r2498 | |||
Brian Granger
|
r2205 | @generic | ||
vivainio
|
r908 | def complete_object(obj, prev_completions): | ||
Brian Granger
|
r2205 | """Custom completer dispatching for python objects. | ||
Parameters | ||||
---------- | ||||
obj : object | ||||
The object to complete. | ||||
prev_completions : list | ||||
List of attributes discovered so far. | ||||
vivainio
|
r908 | This should return the list of attributes in obj. If you only wish to | ||
Brian Granger
|
r2205 | add to the attributes already discovered normally, return | ||
vivainio
|
r908 | own_attrs + prev_completions. | ||
""" | ||||
raise TryNext | ||||
Brian Granger
|
r2498 | |||