IPython - default print to print head & tail on long variables

(To work only on IPython, either on the terminal, or on QTConsole, and not on a laptop). Is there a way to make the regular print statement (yes, 2.7) automatically print the head and tail (or even just the head) of the if variable is it some arbitrary size?

If I'm print dataframeon a pandas TV frame that is too big, pandas automatically just prints the head. I would like it to work the same on numpy lists and arrays, so the next time I accidentally printed a giant array by accident, I did not type 100 pages of numbers. (I’m sure I could write a function for this, but I use print reflexively, so I wonder if there is a way to control how IPython will be displayed - indeed, I would rather change what the console displays and not what the program does .)

+4
source share
1 answer

print, - ipython, :

In [13]: x = range(3)

In [14]: x
Out[14]: [0, 1, 2]

In [15]: print x
[0, 1, 2]

-, x Out[14] Out[14].

ipython, -. , , ipython pprint . - pprint.pprint/pprint.pformat/pprint.PrettyPrinter . ipython.

-, print x Python. python 2.x , print. python 3.x print() - , .

, , , __str__ __repr__, numpy:

numpy.set_printoptions(threshold=2)
numpy.array(range(10)).__str__()
'[0 1 2 ..., 7 8 9]'
print numpy.array(range(10))
[0 1 2 ..., 7 8 9]

, ipython %page, %page x, x , less , .

P.S. ipython "", , , , , .

+1

Source: https://habr.com/ru/post/1535063/


All Articles