q icon indicating copy to clipboard operation
q copied to clipboard

Prevent string and dictionary truncation

Open l3ender opened this issue 3 years ago • 4 comments

I've noticed the following when using q:

  • long strings are abbreviated with ... in the middle.
  • dictionary objects with greater than 10 entries print the first 10 and then truncate the rest using ....

Example:

obj = {"my_key": {"aaa": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec et hendrerit ante. Aliquam iaculis magna sed ipsum scelerisque porttitor.", "bbb": "2", "ccc": "3", "ddd": "4", "eee": "5", "fff": "6", "ggg": "7", "hhh": "8", "iii": "9", "jjj": "10", "kkk": "11"}}
q(obj)

# output:
{'my_key': {'aaa': 'Lorem ipsum dolor sit amet, consectetur adipisci...am iaculis magna sed ipsum scelerisque porttitor.', 'bbb': '2', 'ccc': '3', 'ddd': '4', 'eee': '5', 'fff': '6', 'ggg': '7', 'hhh': '8', 'iii': '9', 'jjj': '10', ...}}

Is there a way to prevent it? I'm not very proficient in python so my apologies if this is a simple question. Thanks!!

l3ender avatar May 22 '21 03:05 l3ender

Hi! There isn't a friendly way to configure this at the moment, but you could edit q.py to achieve this by altering the maxdict and maxstring attributes, which control the maximum number of dictionary items and maximum number of characters in a string that are shown. You can do this by setting the appropriate attribute just after line 95:

    TEXT_REPR = pydoc.TextRepr()  # this is line 95

    TEXT_REPR.maxdict = 20
    TEXT_REPR.maxstring = 500

zestyping avatar May 22 '21 03:05 zestyping

It seems like any changes I make to q.py don't get used when I next run my script. I'm using a virtual environment and editing the file in the virtual env, like:

./venv/lib/python3.9/site-packages/q.py

I've added the TEXT_REPR.maxdict = 20 line, but still don't see the change:

-> python
Python 3.9.1 (default, Dec 10 2020, 10:36:35)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import q
>>> q.TEXT_REPR.maxdict
10

Is there anything else I need to do, or am I editing the wrong file?

l3ender avatar Jun 23 '21 22:06 l3ender

I think all truncation needs to be disabled by default. This is a wonderful debugging tool, but cutting off debugging data is counterproductive

rothman857 avatar Feb 14 '22 16:02 rothman857

FYI, raised a PR (https://github.com/zestyping/q/pull/60) to address the truncation, assuming this repo is still maintained.

The default truncation levels are far too short for someone debugging.

If the default trunction is still desired, enter: "q.short" in your script.

Long trucation is set by default and can be explitly set by: "q.long"

The truncation can be manuall set by: "q.long = 2000000"

rothman857 avatar Mar 23 '22 15:03 rothman857