Short Form prevents the Python shell from printing out giant piles of text.
It is a hack that ties into the display system. When you are working with a multi-megabyte text file, referenced under the name txt
, which takes 10 minutes to be printed in full (if you accidentally type '>>> txt') it's really nice to have this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #Written by Jesse Weinstein <jessw@netwood.net>.
#Released under the Python license on Sat Jan 22 02:56:11 2005.
#shortForm.py
#Short Form prevents the Python shell from printing out giant piles of text.
#Python version: er, 2.3 and later. Or maybe earlier versions...
#http://www.netwood.net/usr/jessw
#Possible categories: Utilities, Python shell hacks
#First release.
#Released to Useless Python (http://www.uselesspython.com), Vaults of Parnassus,
# ASPN Python Cookbook, and my site.
"""Short Form prevents the Python shell from printing out giant piles of text.
It is a hack that ties into the display system. When you are working with a multi-megabyte text file,
referenced under the name `txt`, which takes 10 minutes to be printed in full (if you accidentally
type '>>> txt') it's really nice to have this.
Besides actually turning it off, it can be evaded by using `print` or even sys.stdout.write itself.
ShortForm is only used when an object is repr'ed and printed."""
import sys
def toggle_ShortForm():
"""Toggle the shortening of output."""
if sys.displayhook==shortForm:
off()
else:
on()
def install(max_size=100, size=30):
"""Use this to start up ShortForm.
max_size is the maximum length the repr can be before it's shortened.
size is the length of the beginning and end which is printed. The total length of a shorted item is
size*2+3. The +3 is for the elipsis."""
sys.shortFormMaxSize=max_size
sys.shortFormSize=size
__builtins__['sft']=toggle_ShortForm
def on():
"Turn on ShortForm."""
if 'shortFormSize' not in dir(sys):
install()
sys.displayhook=shortForm
def off():
"""Turn off ShortForm."""
sys.displayhook=sys.__displayhook__
def remove():
"""Remove the items ShortForm places in the `sys` namespace."""
del sys.shortFormMaxSize
del sys.shortFormSize
del __builtins__['sft']
def shortForm(item):
"""Write out a shortened form of the provided item."""
if item is None:
return
r=repr(item)
if len(r) > sys.shortFormMaxSize:
sys.stdout.write(r[:sys.shortFormSize]+' ... '+\
r[-sys.shortFormSize:]+'\n')
else:
sys.stdout.write(r+'\n')
|
This may not be the most elegant way of doing this. Please suggest further cleanups, if you see them.
reformulate as a class. Why don't you put this all into a class?
Note that storing the old displayhook like this also works correctly in the recursive case (whereas restoring sys.__displayhook__ doesn't).
That's clearly better. Thanks! Sigh. I knew that wasn't the most elegant way. Putting it in a class is clearly cleaner. Thanks. May I publish your class based version under the BSD license in the same places I'm now publishing my old version?