Skip to main content

Pretty formatter enables pretty formatting using hanging indents, dataclasses, ellipses, and simple customizability by registering formatters.

Project description

prettyformatter

Pretty formatter enables pretty formatting using hanging indents, dataclasses, ellipses, and simple customizability by registering formatters.

Installation

Windows:

py -m pip install prettyformatter

Unix/MacOS:

python3 -m pip install prettyformatter

Imports

from prettyformatter import PrettyDataclass, pprint, pformat, register

Basic Usage

Long containers are truncated.

pprint(list(range(1000)))
"""
[0, 1, 2, 3, 4, ..., 997, 998, 999]
"""

Large nested structures are split into multiple lines, while things which (reasonably) fit on a line will remain on one line.

Notice that trailing commas are used.

Notice that multi-line dictionaries have key-value pairs indented at different levels.

pprint([{i: {"ABC": [list(range(30))]} for i in range(5)}])
"""
[
    {
        0:
            {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
        1:
            {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
        2:
            {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
        3:
            {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
        4:
            {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
    },
]
"""

The current depth and indentation size can be modified.

pprint([{i: {"ABC": [list(range(30))]} for i in range(5)}], indent=2)
"""
[
  {
    0:
      {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
    1:
      {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
    2:
      {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
    3:
      {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
    4:
      {'ABC': [[0, 1, 2, 3, 4, ..., 27, 28, 29]]},
  },
]
"""

Dataclasses are supported by subclassing the PrettyDataclass.

from dataclasses import dataclass
from typing import List

bit_data = list(range(1000))

@dataclass
class Data(PrettyDataclass):
    data: List[int]
 
print(Data(big_data))  # Normal print.
"""
Data(data=[0, 1, 2, 3, 4, ..., 997, 998, 999])
"""

@dataclass
class MultiData(PrettyDataclass):
    x: List[int]
    y: List[int]
    z: List[int]

print(MultiData(big_data, big_data, big_data))
"""
MultiData(
    x=[0, 1, 2, 3, 4, ..., 997, 998, 999],
    y=[0, 1, 2, 3, 4, ..., 997, 998, 999],
    z=[0, 1, 2, 3, 4, ..., 997, 998, 999],
)
"""

Custom formatters can be registered.

import numpy as np

@register(np.ndarray)
def pformat_ndarray(obj, specifier, depth, indent):
    with np.printoptions(formatter=dict(all=lambda x: format(x, specifier))):
        return repr(obj).replace(\"\\n\", \"\\n\" + \" \" * depth)

pprint(dict.fromkeys("ABC", np.arange(9).reshape(3, 3)))
"""
{
    'A':
        array([[0, 1, 2],
               [3, 4, 5],
               [6, 7, 8]]),
    'B':
        array([[0, 1, 2],
               [3, 4, 5],
               [6, 7, 8]]),
    'C':
        array([[0, 1, 2],
               [3, 4, 5],
               [6, 7, 8]]),
}
"""

Project details


Download files

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

Source Distribution

prettyformatter-1.0.4.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

prettyformatter-1.0.4-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file prettyformatter-1.0.4.tar.gz.

File metadata

  • Download URL: prettyformatter-1.0.4.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for prettyformatter-1.0.4.tar.gz
Algorithm Hash digest
SHA256 a6e22f4cbcaf9a30b8d34b61b0eb82f4cf73e491ead1790c4c94c3f6aa65fa6f
MD5 89039f6d8c32301001d04b0fccf31c16
BLAKE2b-256 624093b6f1f6193cdf4c6a1c7f650eea82d7a782c4f13b27fd42756dc366f5af

See more details on using hashes here.

File details

Details for the file prettyformatter-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for prettyformatter-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 99f296cd898a54867d2d329b78043f9f352c3b8609441c440ab57b3765f55b01
MD5 80b8827e2243bceb07fded2f90d8b00e
BLAKE2b-256 f21bda9b898eb3dbea6eaca837071a51b8dda72b44b2ae96d3075d0c68fc6b99

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page