##// END OF EJS Templates
Merging Fernando's fixes to test_iplib.py.
Merging Fernando's fixes to test_iplib.py.

File last commit:

r1229:937e0cf6
r1957:e73c1d57 merge
Show More
generics.py
54 lines | 1.7 KiB | text/x-python | PythonLexer
Fernando Perez
Small doc cleanup.
r1229 ''' 'Generic' functions for extending IPython.
vivainio
crlf normalization
r851
Fernando Perez
Small doc cleanup.
r1229 See http://cheeseshop.python.org/pypi/simplegeneric.
vivainio
crlf normalization
r851
Fernando Perez
Small doc cleanup.
r1229 Here is an example from genutils.py:
vivainio
crlf normalization
r851
def print_lsstring(arg):
Fernando Perez
Small doc cleanup.
r1229 "Prettier (non-repr-like) and more informative printer for LSString"
vivainio
crlf normalization
r851 print "LSString (.p, .n, .l, .s available). Value:"
print arg
print_lsstring = result_display.when_type(LSString)(print_lsstring)
(Yes, the nasty syntax is for python 2.3 compatibility. Your own extensions
Fernando Perez
Small doc cleanup.
r1229 can use the niftier decorator syntax introduced in Python 2.4).
vivainio
crlf normalization
r851 '''
Fernando Perez
Small doc cleanup.
r1229 from IPython.ipapi import TryNext
from IPython.external.simplegeneric import generic
vivainio
crlf normalization
r851 def result_display(result):
""" print the result of computation """
raise TryNext
result_display = generic(result_display)
def inspect_object(obj):
""" Called when you do obj? """
raise TryNext
vivainio
implement complete_object(obj, prev_completions) generic function to provide custom completers for python object attributes (as opposed to string dispatching)
r908 inspect_object = generic(inspect_object)
def complete_object(obj, prev_completions):
""" Custom completer dispatching for python objects
obj is the object itself.
prev_completions is the list of attributes discovered so far.
This should return the list of attributes in obj. If you only wish to
add to the attributes already discovered normally, return
own_attrs + prev_completions.
"""
raise TryNext
complete_object = generic(complete_object)
#import os
#def my_demo_complete_object(obj, prev_completions):
# """ Demo completer that adds 'foobar' to the completions suggested
# for any object that has attribute (path), e.g. 'os'"""
# if hasattr(obj,'path'):
# return prev_completions + ['foobar']
# raise TryNext
#
#my_demo_complete_object = complete_object.when_type(type(os))(my_demo_complete_object)