Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting print output

I have a number of objects that I need to print out to the terminal (for debugging). The normal print function is almost perfect, except that some objects are too large, so print would create millions of lines of output. I'd like to create a function that does what print does, except that the output is truncated after a predefined number of characters, replacing the rest with ....

What's a good way to do that?

Note that performance is a concern, so ideally I'd prefer not to save a gigabyte-sized string and then take the first few characters from it; similarly, pprint is a bit of a problem since it sorts keys in dictionaries (and with millions of keys it takes a while).

Example:

obj = [ [1, 2, 3], list(range(1000000)) ]
my_print(obj, 20)
# should output:
# [[1, 2, 3], [0, 1, 2...

Python 3, if it matters.

like image 257
max Avatar asked Mar 28 '15 20:03

max


People also ask

What is print () in Python?

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

How do I print a specific number of characters in Python?

Using the * operator to print a character n times in Python In the print() function we can specify the character to be printed. We can use the * operator to mention how many times we need to print this value. See the code below. In the above example, we printed the character five times using the * operator.

How do you print on different lines in Python?

🔹 In Summary The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.


1 Answers

The reprlib module (Python 3.x only) suggested by @m0nhawk is made exactly for this purpose. Here's how you would use it:

If you're fine with the default limits, you can simply use reprlib.repr(obj):

import reprlib

obj = [[1, 2, 3], list(range(10000))]

print(reprlib.repr(obj))

Output:

[[1, 2, 3], [0, 1, 2, 3, 4, 5, ...]]

In order to customize the available limits, simply create a reprlib.Repr instance and set the appropriate instance attributes:

r = reprlib.Repr()
r.maxlist = 4       # max elements displayed for lists
r.maxstring = 10    # max characters displayed for strings

obj = [[1, 2, 3], list(range(10000)), 'looooooong string', 'a', 'b', 'c']

print(r.repr(obj))

Output:

[[1, 2, 3], [0, 1, 2, 3, ...], 'lo...ing', 'a', ...]

If you're dealing with sequence objects that refer to themselves, you can use Repr.maxlevel to limit the recursion depth:

lst = [1, 2, 3]
lst.append(lst)  # oh my!

r = reprlib.Repr()
r.maxlevel = 5   # max recursion depth

print(r.repr(lst))

Output:

[1, 2, 3, [1, 2, 3, [1, 2, 3, [1, 2, 3, [1, 2, 3, [...]]]]]]

Note that reprlib.repr() returns a string, but doesn't print it (unless you're in an interactive console where the result of every expression you enter gets evaluated and its representation displayed).

like image 109
Lukas Graf Avatar answered Sep 17 '22 14:09

Lukas Graf