Welcome, guest | Sign In | My Account | Store | Cart

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.

Python, 58 lines
 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.

2 comments

Steven Bethard 16 years, 10 months ago  # | flag

reformulate as a class. Why don't you put this all into a class?

py> class ShortForm(object):
...     def __init__(self, max_size=100):
...         self.displayhook = None
...         self.max_size = max_size
...         self.clip_size = (max_size - 3)//2
...     def load(self):
...         self.displayhook = sys.displayhook
...         sys.displayhook = self
...     def unload(self):
...         if self.displayhook is not None:
...             sys.displayhook = self.displayhook
...     def __call__(self, value):
...         if value is not None:
...             r = repr(value)
...             if len(r) > self.max_size:
...                 sys.stdout.write('%s...%s\n' % (r[:self.clip_size],
...                                                 r[-self.clip_size:]))
...             else:
...                 sys.stdout.write('%s\n' % r)
...
py> '1'*60
'111111111111111111111111111111111111111111111111111111111111'
py> sf = ShortForm(20)
py> sf.load()
py> '1'*60
'1111111...1111111'
py> sf.unload()
py> '1'*60
'111111111111111111111111111111111111111111111111111111111111'

Note that storing the old displayhook like this also works correctly in the recursive case (whereas restoring sys.__displayhook__ doesn't).

jessw (author) 16 years, 8 months ago  # | flag

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?

Created by jessw on Sat, 22 Jan 2005 (PSF)
Python recipes (4591)
jessw's recipes (1)

Required Modules

Other Information and Tasks