Skip to main content

print-dict

Motivation

Apparently, pretty-printing nested python dictionaries with values such as classes and functions (where you can't use json.dumps) is not as straightforward as you would think.

See: https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries

This library tries to make it a little bit easier.

Install or Upgrade

$ pip install -U print-dict

Usage

from print_dict import pd
pd({'key': 'value'})

# ------------------------
# Output:
# {
#    'key': 'value'
# } 
# ------------------------

# Or

from print_dict import print_dict
print_dict({'key': 'value'})


# Get the string without printing
from print_dict import format_dict
string = format_dict({'key': 'value'})

Example 1

Code:

from print_dict import pd

dict1 = {
    'key': 'value'
}

pd(dict1)

Output:

{
    'key': 'value'
}

Example 2

Code:

from print_dict import pd


class Object1:
    pass


class Object2:

    def __repr__(self):
        return "<Object2 info>"


def custom_method():
    pass


object1 = Object1()

data = {
    "one": "value-one",
    "two": "value-two",
    "three": "value-three",
    "four": {
        '1': '1', '2': '2', '3': [1, 2, 3, 4, 5], '4': {
            'method': custom_method,
            'tuple': (1, 2),
            'unicode': u'\u2713',
            'ten': 'value-ten',
            'eleven': 'value-eleven',
            '3': [1, 2, 3, 4]
        }
    },
    "object1": object1,
    "object2": Object2(),
    "class": Object1

}

pd(data)

Output:


{
    'one': 'value-one',
    'two': 'value-two',
    'three': 'value-three',
    'four': {
        '1': '1',
        '2': '2',
        '3': [1, 2, 3, 4, 5],
        '4': {
            'method': <function custom_method at 0x7ff6ecd03e18>,
            'tuple': (1, 2),
            'unicode': '✓',
            'ten': 'value-ten',
            'eleven': 'value-eleven',
            '3': [1, 2, 3, 4]
        }
    },
    'object1': <__main__.Object1 object at 0x7ff6ecc588d0>,
    'object2': <Object2 info>,
    'class': <class '__main__.Object1'>
}


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

print-dict-0.1.19.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

print_dict-0.1.19-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

Release history Release notifications | RSS feed

This release

0.1.19 This release

2 files

0.1.18

2 files

0.1.17

2 files

0.1.16

2 files

0.1.15

2 files

0.1.14

2 files

0.1.13

2 files

0.1.12

2 files

0.1.11

2 files

0.1.10

2 files

0.1.9

2 files

0.1.8

2 files

0.1.7

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page