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 aligned and hanging indents for JSON, dataclasses, named tuples, and any custom formatted object such as Numpy arrays.

For the full documentation, see here.

Installation

Windows:

py -m pip install prettyformatter

Unix/MacOS:

python3 -m pip install prettyformatter

Imports

from prettyformatter import PrettyClass, PrettyDataclass
from prettyformatter import pprint, pformat, register

JSON Data

prettyformatter works with JSON data.

batters = [
    {"id": "1001", "type": "Regular"},
    {"id": "1002", "type": "Chocolate"},
    {"id": "1003", "type": "Blueberry"},
    {"id": "1004", "type": "Devil's Food"},
]

toppings = [
    {"id": "5001", "type": None},
    {"id": "5002", "type": "Glazed"},
    {"id": "5005", "type": "Sugar"},
    {"id": "5007", "type": "Powdered Sugar"},
    {"id": "5006", "type": "Chocolate with Sprinkles"},
    {"id": "5003", "type": "Chocolate"},
    {"id": "5004", "type": "Maple"},
]

data = {"id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": batters, "topping": toppings}

pprint:

prettyformatter attempts to compromise between alignment, readability, and horizontal/vertical compactness.

Support for JSON data is also as easy as pprint(json=True).

from prettyformatter import pprint

pprint(data, json=True)
"""
{
    "id"    : "0001",
    "type"  : "donut",
    "name"  : "Cake",
    "ppu"   : 0.55,
    "batters":
        [
            {"id": "1001", "type": "Regular"},
            {"id": "1002", "type": "Chocolate"},
            {"id": "1003", "type": "Blueberry"},
            {"id": "1004", "type": "Devil's Food"}
        ],
    "topping":
        [
            {"id": "5001", "type": None},
            {"id": "5002", "type": "Glazed"},
            {"id": "5005", "type": "Sugar"},
            {"id": "5007", "type": "Powdered Sugar"},
            {"id": "5006", "type": "Chocolate with Sprinkles"},
            {"id": "5003", "type": "Chocolate"},
            {"id": "5004", "type": "Maple"}
        ]
}
"""

pprint supports the same parameters as print, meaning saving to files is as easy as file=file.

from prettyformatter import pprint

with open("cake.json", mode="w") as file:
    pprint(data, json=True, file=file)

PrettyDataclass

prettyformatter supports dataclasses easily.

@dataclass
class Person(PrettyDataclass):
    name: str
    birthday: str
    phone_number: str
    address: str


print(Person("Jane Doe", "2001-01-01", "012-345-6789", "123 Sample St."))
"""
Person(
    name=
        "Jane Doe",
    birthday=
        "2001-01-01",
    phone_number=
        "012-345-6789",
    address=
        "123 Sample St.",
)
"""

register

Custom formatters for existing classes can be registered.

import numpy as np

@register(np.ndarray)
def pformat_ndarray(obj, specifier, depth, indent, shorten, json):
    if json:
        return pformat(obj.tolist(), specifier, depth, indent, shorten, json)
    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]]),
}
"""

pprint(dict.fromkeys("ABC", np.arange(9).reshape(3, 3)), json=True)
"""
{
    "A" : [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
    "B" : [[0, 1, 2], [3, 4, 5], [6, 7, 8]],
    "C" : [[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.9.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

prettyformatter-1.9.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prettyformatter-1.9.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for prettyformatter-1.9.0.tar.gz
Algorithm Hash digest
SHA256 049e9ba665c3b0e3c2eea3b520a6ff305f7e199fa1cb86ea7d298634ab147004
MD5 529a1ce536739ed54311c39b80786e82
BLAKE2b-256 c09a95c8b9d02045c23f9508abd9aca334172974a1ced7c46977fb783494bcab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prettyformatter-1.9.0-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.5

File hashes

Hashes for prettyformatter-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d38eb35e2044e93d63afb1d6ea7a6aec5d20f779106deb4ce506de7b19165c6e
MD5 5bd35977233473d3e4be69efb6f75301
BLAKE2b-256 cc6432145aec20409785ef8e0336a10f8b3a47e3c41cbeb9bcfcb6743694662e

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