I don't think this is an issue with pretty print not being able to handle nested default dicts.
This is the description in the docs for the compact parameter:
compact impacts the way that long sequences (lists, tuples, sets, etc) are formatted. If compact is false (the default) then each item of a sequence will be formatted on a separate line. If compact is true, as many items as will fit within the width will be formatted on each output line.
The key bit is "as many items as will fit within the width". As the default width is 80, as soon as the output is past that value you will end up with one item per line regardless of whether compact defined.
Activity
iritkatriel commented on Apr 11, 2022
What are you suggesting?
ravi140222 commented on Apr 11, 2022
it should be like this,
{'__add__', '__contains__', '__delattr__', '__delitem__', '__eq__', '__ge__', '__getattribute__', '__gt__', ...}instead of so many rows
should I work on creating a pr for this?
ravi140222 commented on Apr 12, 2022
for a
I get the output as,
{'__add__', '__contains__', '__delattr__', '__delitem__', '__eq__', '__ge__', '__getattribute__', '__gt__', ...}when I use,
but the case I created this issue for, it does not work even after using compact=True
I think so, the problem here is that one could nest defaultdicts
that is,
is again,
the current pretty print cannot handle such a situation.
this nesting could go even further, like,
pretty print needs to be changed, to handle nested defaultdicts.
iritkatriel commented on Apr 12, 2022
I don't understand. Can you write a complete script to reproduce what you are seeing?
This is what I see:
>>> p = defaultdict(lambda: defaultdict(set)) >>> pprint(p, compact=True) defaultdict(<function <lambda> at 0x108f22fc0>, {}) >>> pprint(p, compact=False) defaultdict(<function <lambda> at 0x108f22fc0>, {})vainaixr commented on Apr 12, 2022
(this is my alternative account)
just run this,
def enhanced_dir(arg, categorize=True, show_types=False): from collections import defaultdict if not categorize: return dir(arg) passed = defaultdict(lambda : defaultdict(set)) failed = defaultdict(set) passed_ = defaultdict(lambda : defaultdict(set)) failed_ = defaultdict(lambda : defaultdict(set)) x = arg for method in dir(arg): type_ = type(eval(f'x.{method}')) try: qualname = eval(f'x.{method}.__qualname__') qualname = qualname.split('.') passed['x'][qualname[0]].add(qualname[1]) passed_['x'][type_].add(qualname[1]) except: failed['x'].add(method) failed_['x'][type_].add(method) if show_types: return [passed, passed_] return passedwookie184 commented on Apr 12, 2022
I don't think this is an issue with pretty print not being able to handle nested default dicts.
This is the description in the docs for the compact parameter:
The key bit is "as many items as will fit within the width". As the default width is 80, as soon as the output is past that value you will end up with one item per line regardless of whether compact defined.
vainaixr commented on Apr 12, 2022
yes, thanks, I was not aware of using width, I am able to get the desired output, after increasing the width to 120.
iritkatriel commented on Apr 12, 2022
Thank you.