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 PrettyClass, 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.
Shortening the data is also toggleable.
See help(prettyformatter.pprint)
for more information.
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]]},
},
]
"""
The pretty string can be used elsewhere.
s = pformat([{i: {"ABC": [list(range(30))]} for i in range(5)}])
print(s)
"""
[
{
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
big_data = list(range(1000))
Dataclass fields are pretty formatted.
@dataclass
class Data(PrettyDataclass):
data: List[int]
print(Data(big_data))
"""
Data(data=[0, 1, 2, 3, 4, ..., 997, 998, 999])
"""
Long dataclasses are split into multiple lines.
@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],
)
"""
Nested data is indented deeper.
@dataclass
class NestedData(PrettyDataclass):
data: List[List[int]]
print(NestedData([big_data] * 1000))
"""
NestedData(
data=[
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
...,
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
[0, 1, 2, 3, 4, ..., 997, 998, 999],
],
)
"""
If there are more than 3 fields and the dataclass is long, then fields and their values are split, similar to a dict.
@dataclass
class Person(PrettyDataclass):
name: str
birthday: str
phone_number: str
address: str
print(Person("Jane Doe", "2001-01-01 08:23:00", "012-345-6789", "123 Sample St. City Country ZIP_CODE"))
"""
Person(
name=
'Jane Doe',
birthday=
'2001-01-01 08:23:00',
phone_number=
'012-345-6789',
address=
'123 Sample St. City Country ZIP_CODE',
)
"""
Named tuples work like dataclasses, but requires pprint
instead of
print
.
from typing import NamedTuple
big_data = list(range(1000))
class Data(NamedTuple):
data: List[int]
pprint(Data(big_data))
"""
Data(data=[0, 1, 2, 3, 4, ..., 997, 998, 999])
"""
Custom formatters for your classes can be defined.
class PrettyHelloWorld(PrettyClass):
def __pformat__(self, specifier, depth, indent, shorten):
return f"Hello world! Got {specifier!r}, {depth}, {indent}, {shorten}."
print(PrettyHelloWorld())
"""
Hello world! Got '', 0, 4, True.
"""
Use f-strings with your classes.
"""
format_spec ::= [[[shorten|]depth>>]indent:][specifier]
shorten ::= T | F
depth ::= digit+
indent ::= digit+ without leading 0
specifier ::= anything else you want to support e.g. ".2f"
"""
print(f"{PrettyHelloWorld():F|5>>6:.2f}")
Hello World! Got '.2f', 5, 6, False.
Custom formatters for existing classes can be registered.
import numpy as np
@register(np.ndarray)
def pformat_ndarray(obj, specifier, depth, indent, shorten):
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
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 prettyformatter-1.2.1.tar.gz
.
File metadata
- Download URL: prettyformatter-1.2.1.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f80a2852b2adb1adc48a146a56233a21b36c75e04ae6b031c4c342a6d247d10 |
|
MD5 | a9eca14e6d425b05373fbc9b0494db57 |
|
BLAKE2b-256 | cbd60356a910d8c5d003b410940332a72561a414ecad600a3f93e05c1bd33d4d |
File details
Details for the file prettyformatter-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: prettyformatter-1.2.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e066b40c7cd862e75d5426bde30b35887d82438a7406becacd83ecd116697a0d |
|
MD5 | 7dcf7857e65686c8aab3114f3568520f |
|
BLAKE2b-256 | 25c7e6c867004c0b3528027b0a1ce7bb594e5d9db2d0a9659fceed714bf89d36 |