Skip to main content

Prettier - properly pprint of nested objects

If you are trying to output nested data structures, each of which is individually displayed as a dictionary representation, the nesting levels are not separated, such an output is difficult to perceive.

class PPrintMixin:
    def __str__(self):
        return str(self.__dict__)


SAMPLE_DICT = {'a': None}
SAMPLE_DICT['a'] = SAMPLE_DICT


class A(PPrintMixin):
    def __init__(self):
        self.attrs = ('a', 'b', 'c')
        self.a = 10
        self.b = B()
        self.c = {'a': 1, 'b': 2, 'c': 3}
        setattr(self, 'd\nd', {'a': 1, 'b': 2, 'c': 3})
        self.e = 'a\na'
        self.f = 'a' * 100
        self.g = self
        self.h = SAMPLE_DICT


class B(PPrintMixin):
    def __init__(self):
        setattr(self, 'a \n a', {'a': 1, 'b': {'a': 1, 'b': 2, 'c': 3}, 'c\nc': C()})
        self.b = [C(), 1]
        self.c = C()


class C(PPrintMixin):
    def __init__(self):
        self.a = ('b', 'c')
        self.b = 'a \n a'
        self.c = {}

You can expect the following output:

{'attrs': ('a', 'b', 'c'), 'a': 10, 'b': <__main__.B object at 0x1034b74c0>, 'c': {'a': 1, 'b': 2, 'c': 3}, 'd\nd': {'a': 1, 'b': 2, 'c': 3}, 'e': 'a\na', 'f': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'g': <__main__.A object at 0x1033fb3a0>, 'h': {'a': {...}}}

But if you use a PPrintMixin instead of the standard one, then you can expect the following output:

{'a': 10,
 'd
   d': {'a': 1,
        'b': 2,
        'c': 3},
 'e': 'a
       a',
 'c': {'a': 1,
       'b': 2,
       'c': 3},
 'h': {'a': <RecursionError on 4304997312>},
 'b': {'a 
           a': {'a': 1,
                'b': {'a': 1,
                      'b': 2,
                      'c': 3},
                'c
                  c': {'a': ('b', 'c'),
                       'c': {},
                       'b': 'a 
                              a'}},
       'c': {'a': ('b', 'c'),
             'c': {},
             'b': 'a 
                    a'},
       'b': [<__main__.C object at 0x100bf3a60>, 1]},
 'attrs': ('a', 'b', 'c'),
 'f': 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
 'g': <RecursionError on 4307498032>}

If you want to create an own Mixin, just import MixinFactory. For example, you can use the endpoint argument. This is the name of the attribute that stores the list of arguments to print. Let's combine some Mixins, like this:

from prettier import MixinFactory

AttrsPrintMixin = MixinFactory(endpoint='attrs')
APrintMixin = MixinFactory(endpoint='a')


class A(AttrsPrintMixin):
    ...


class B(PrintMixin):
    ...


class C(APrintMixin):
    ...

The output will be:

{'a': 10,
 'b': {'c': {'b': 'a 
                    a',
             'c': {}},
       'b': [<__main__.C object at 0x100b478b0>, 1],
       'a 
           a': {'a': 1,
                'b': {'a': 1,
                      'b': 2,
                      'c': 3},
                'c
                  c': {'b': 'a 
                              a',
                       'c': {}}}},
 'c': {'a': 1,
       'b': 2,
       'c': 3}}

Metadata

Release files for prettier 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prettier 0.0.7
File Size Uploaded
prettier-0.0.7.tar.gz 16.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prettier 0.0.7
File Interpreter ABI Platform
prettier-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 32.4 kB

Release files / prettier-0.0.7.tar.gz

Download URL prettier-0.0.7.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6c34b8cd09fd9c8956c05d6395ea3f575e0122dce494ba57685c07065abed427
BLAKE2b-256 checksum
How to use checksums
c90ea3375ca8f83f03662f22cbfee801badc4e4da19e4c06ade5ccfab4297678
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release files / prettier-0.0.7-py3-none-any.whl

Download URL prettier-0.0.7-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20e76791de41cafe481328dd49552303f29ca192151cee1b120c26f66cae9bfc
BLAKE2b-256 checksum
How to use checksums
f2cfc401d2af75c2c271375bbe3654e408af2c296650f88b8b3d3942e3c8b63e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.2

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release 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