Properly pprint of nested objects
Project description
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}}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file prettier-0.0.7.tar.gz
.
File metadata
- Download URL: prettier-0.0.7.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c34b8cd09fd9c8956c05d6395ea3f575e0122dce494ba57685c07065abed427 |
|
MD5 | 66be291ef6bcdc7937f4a6ca22ac60c9 |
|
BLAKE2b-256 | c90ea3375ca8f83f03662f22cbfee801badc4e4da19e4c06ade5ccfab4297678 |
File details
Details for the file prettier-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: prettier-0.0.7-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20e76791de41cafe481328dd49552303f29ca192151cee1b120c26f66cae9bfc |
|
MD5 | 728aa4402ec8c402d3e4724bd69858a2 |
|
BLAKE2b-256 | f2cfc401d2af75c2c271375bbe3654e408af2c296650f88b8b3d3942e3c8b63e |