PyPformat
is a simple and highly customizable python pretty-formatting package designed as an alternative to the built-in pprint library - PyPformat
uses a different, more natural formatting style and provides extensive personalization capabilities, including text colorizing or customized indentation marking, on top of the basic options like compact printing.
The example below demostrates the difference in the default outputs produced by the pprint
and PyPformat
libraries.
>>> from pprint import pprint
>>> import pformat as pf
>>>
>>> from collections import ChainMap, Counter, OrderedDict, UserDict, defaultdict
>>>
>>> mapping = {
... "key1": 1,
... "key2": OrderedDict({"key3": 3, "key4": 4}),
... "key5": defaultdict(
... str,
... {
... "key6": 6,
... "a_very_long_dictionary_key7": ChainMap(
... {"key10": [10, 11, 12, 13], "key8": 8, "key9": 9}
... ),
... "key11": Counter("Hello"),
... },
... ),
... "key12": UserDict({0: "a", 1: "b", 2: "c"}),
... }
>>>
>>> pprint(mapping)
{'key1': 1,
'key12': {0: 'a', 1: 'b', 2: 'c'},
'key2': OrderedDict({'key3': 3, 'key4': 4}),
'key5': defaultdict(<class 'str'>,
{'a_very_long_dictionary_key7': ChainMap({'key10': [10,
11,
12,
13],
'key8': 8,
'key9': 9}),
'key11': Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1}),
'key6': 6})}
>>>
>>> formatter = pf.PrettyFormatter()
>>> print(formatter(mapping))
{
'key1': 1,
'key2': OrderedDict({
'key3': 3,
'key4': 4,
}),
'key5': defaultdict(<class 'str'>, {
'key6': 6,
'a_very_long_dictionary_key7': ChainMap({
'key10': [
10,
11,
12,
13,
],
'key8': 8,
'key9': 9,
}),
'key11': Counter({
'H': 1,
'e': 1,
'l': 2,
'o': 1,
}),
}),
'key12': {
0: 'a',
1: 'b',
2: 'c',
},
}
Important
- The minimum (tested) python version required to use the
PyPformat
package is 3.9. - The complete functionality of the
PyPformat
package (including all format configuration options) is described in the PyPformat - Usage document. - While the
PyPformat
package is already quite versatile and customizable, its development is ongoing. A detailed list of the planned features/improvements can be found in the PyPformat - TODO document.
The PyPformat
package can be installed via pip:
pip install pypformat
The PyPformat - Dev notes document contains the information about project development, testing and formatting.
The PyPformat
project is licenced under the MIT Licence, which can be inspected in the LICENCE file in the project's root directory.