I'm trying to print a dict() using Pythons Rich. From my understanding, this should output the data on different lines etc. A bit like pprint.
But I'm getting:
>>> from rich import print
>>> print(output)
{'GigabitEthernet0/1': {'description': '## Connected to leaf-2 ##', 'type': 'iGbE', 'oper_status': 'up',
'phys_address': '5000.0009.0001', 'port_speed': 'auto speed', 'mtu': 1500, 'enabled': True, 'bandwidth': 1000000,
'flow_control': {'receive': False, 'send': False}, 'mac_address': '5000.0009.0001', 'auto_negotiate': True,
'port_channel': {'port_channel_member': False}, 'duplex_mode': 'auto', 'delay': 10, 'accounting': {'other': {'pkts_in':
0, 'chars_in': 0, 'pkts_out': 431258, 'chars_out': 25875480}, 'ip': {'pkts_in': 513383, 'chars_in': 42910746,
'pkts_out': 471188, 'chars_out': 45342027}, 'dec mop': {'pkts_in': 0, 'chars_in': 0, 'pkts_out': 7163, 'chars_out':
551551}, 'arp': {'pkts_in': 3845, 'chars_in': 230700, 'pkts_out': 3846, 'chars_out': 230760}, 'cdp': {'pkts_in': 72010,
'chars_in': 18866620, 'pkts_out': 79879, 'chars_out': 31221768}}, 'ipv4': {'10.1.1.5/30': {'ip': '10.1.1.5', ...
Any suggestions?
TL;DR If your dictionary turns out to be not a dict, make an explicit conversion.
From contents of your dictionary I assume your
outputis from network device configuration like Cisco IOS, I'm dark on these areas and can't quite figure out where you've got your data from.There's chance that module or script you used to get
outputmay have actually returned adictlooking type called MappingProxyType.I speculate this is why your text is colored but not prettified.
For example lets see what
rich.printdoes withstr.__dict__:This will look like this, just as yours.
Do note this is xfce4 Terminal running in WSL2, drawn to X410 X-server. A fully capable terminal.
This indeed looks like plain
dict, but lets check what it actually is:As you see, despite it's output looks like a dictionary, it's not.
types.MappingProxyTypeIs essentially a read-only dictonary which isn't necessarily adict. Devs of 3rd party libraries likerichmight have forgotten existence of such type. If that's the case, thenrich.printwill do what built-inprint()would do: to call__repr__/__str__- now just treating it as mere string.We can confirm this behavior by passing string that looks like
__repr__of a method, and that still gets rich text treatment.And also by creating instance of
MappingProxyTypeyourself.From which you can see how type affects
rich.print's output.To fix, simply convert
types.MappingProxyTypetodict.And that's way more pretty than it used to be - excluding
__doc__value which is a single string and can't be helped.