prettyformatter


Nameprettyformatter JSON
Version 2.0.13 PyPI version JSON
download
home_page
SummaryPretty formatter enables pretty formatting using hanging indents, dataclasses, ellipses, and simple customizability by registering formatters.
upload_time2023-05-14 00:29:13
maintainer
docs_urlNone
author
requires_python>=3.5
licensemit
keywords pretty print format indent dataclass
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://simpleart.github.io/prettyformatter/).

## Installation

Windows:

```
py -m pip install prettyformatter
```

Unix/MacOS:

```
python3 -m pip install prettyformatter
```

## Imports

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

## JSON Data

`prettyformatter` works with JSON data.

```python
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)`.

```python
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`.

```python
from prettyformatter import pprint

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

## `PrettyDataclass`

`prettyformatter` supports dataclasses easily.

```python
@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.

```python
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]]
}
"""
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "prettyformatter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "",
    "keywords": "pretty,print,format,indent,dataclass",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/52/35/bd496971bbfb3f203c074ad0b6ac85cb4bd8eef5502148c76cb692323c99/prettyformatter-2.0.13.tar.gz",
    "platform": null,
    "description": "# prettyformatter\n\nPretty formatter enables pretty formatting using aligned and hanging\nindents for JSON, dataclasses, named tuples, and any custom formatted\nobject such as Numpy arrays.\n\nFor the full documentation, see\n[here](https://simpleart.github.io/prettyformatter/).\n\n## Installation\n\nWindows:\n\n```\npy -m pip install prettyformatter\n```\n\nUnix/MacOS:\n\n```\npython3 -m pip install prettyformatter\n```\n\n## Imports\n\n```python\nfrom prettyformatter import PrettyClass, PrettyDataclass\nfrom prettyformatter import pprint, pformat, register\n```\n\n## JSON Data\n\n`prettyformatter` works with JSON data.\n\n```python\nbatters = [\n    {\"id\": \"1001\", \"type\": \"Regular\"},\n    {\"id\": \"1002\", \"type\": \"Chocolate\"},\n    {\"id\": \"1003\", \"type\": \"Blueberry\"},\n    {\"id\": \"1004\", \"type\": \"Devil's Food\"},\n]\n\ntoppings = [\n    {\"id\": \"5001\", \"type\": None},\n    {\"id\": \"5002\", \"type\": \"Glazed\"},\n    {\"id\": \"5005\", \"type\": \"Sugar\"},\n    {\"id\": \"5007\", \"type\": \"Powdered Sugar\"},\n    {\"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\"},\n    {\"id\": \"5003\", \"type\": \"Chocolate\"},\n    {\"id\": \"5004\", \"type\": \"Maple\"},\n]\n\ndata = {\"id\": \"0001\", \"type\": \"donut\", \"name\": \"Cake\", \"ppu\": 0.55, \"batters\": batters, \"topping\": toppings}\n```\n\n## `pprint`:\n\n`prettyformatter` attempts to compromise between alignment,\nreadability, and horizontal/vertical compactness.\n\nSupport for JSON data is also as easy as `pprint(json=True)`.\n\n```python\nfrom prettyformatter import pprint\n\npprint(data, json=True)\n\"\"\"\n{\n    \"id\"    : \"0001\",\n    \"type\"  : \"donut\",\n    \"name\"  : \"Cake\",\n    \"ppu\"   : 0.55,\n    \"batters\":\n        [\n            {\"id\": \"1001\", \"type\": \"Regular\"},\n            {\"id\": \"1002\", \"type\": \"Chocolate\"},\n            {\"id\": \"1003\", \"type\": \"Blueberry\"},\n            {\"id\": \"1004\", \"type\": \"Devil's Food\"}\n        ],\n    \"topping\":\n        [\n            {\"id\": \"5001\", \"type\": None},\n            {\"id\": \"5002\", \"type\": \"Glazed\"},\n            {\"id\": \"5005\", \"type\": \"Sugar\"},\n            {\"id\": \"5007\", \"type\": \"Powdered Sugar\"},\n            {\"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\"},\n            {\"id\": \"5003\", \"type\": \"Chocolate\"},\n            {\"id\": \"5004\", \"type\": \"Maple\"}\n        ]\n}\n\"\"\"\n```\n\n`pprint` supports the same parameters as `print`, meaning saving to\nfiles is as easy as `file=file`.\n\n```python\nfrom prettyformatter import pprint\n\nwith open(\"cake.json\", mode=\"w\") as file:\n    pprint(data, json=True, file=file)\n```\n\n## `PrettyDataclass`\n\n`prettyformatter` supports dataclasses easily.\n\n```python\n@dataclass\nclass Person(PrettyDataclass):\n    name: str\n    birthday: str\n    phone_number: str\n    address: str\n\n\nprint(Person(\"Jane Doe\", \"2001-01-01\", \"012-345-6789\", \"123 Sample St.\"))\n\"\"\"\nPerson(\n    name=\n        \"Jane Doe\",\n    birthday=\n        \"2001-01-01\",\n    phone_number=\n        \"012-345-6789\",\n    address=\n        \"123 Sample St.\",\n)\n\"\"\"\n```\n\n## `register`\n\nCustom formatters for existing classes can be registered.\n\n```python\nimport numpy as np\n\n@register(np.ndarray)\ndef pformat_ndarray(obj, specifier, depth, indent, shorten, json):\n    if json:\n        return pformat(obj.tolist(), specifier, depth, indent, shorten, json)\n    with np.printoptions(formatter=dict(all=lambda x: format(x, specifier))):\n        return repr(obj).replace(\"\\n\", \"\\n\" + \" \" * depth)\n\npprint(dict.fromkeys(\"ABC\", np.arange(9).reshape(3, 3)))\n\"\"\"\n{\n    \"A\":\n        array([[0, 1, 2],\n               [3, 4, 5],\n               [6, 7, 8]]),\n    \"B\":\n        array([[0, 1, 2],\n               [3, 4, 5],\n               [6, 7, 8]]),\n    \"C\":\n        array([[0, 1, 2],\n               [3, 4, 5],\n               [6, 7, 8]]),\n}\n\"\"\"\n\npprint(dict.fromkeys(\"ABC\", np.arange(9).reshape(3, 3)), json=True)\n\"\"\"\n{\n    \"A\" : [[0, 1, 2], [3, 4, 5], [6, 7, 8]],\n    \"B\" : [[0, 1, 2], [3, 4, 5], [6, 7, 8]],\n    \"C\" : [[0, 1, 2], [3, 4, 5], [6, 7, 8]]\n}\n\"\"\"\n```\n",
    "bugtrack_url": null,
    "license": "mit",
    "summary": "Pretty formatter enables pretty formatting using hanging indents, dataclasses, ellipses, and simple customizability by registering formatters.",
    "version": "2.0.13",
    "project_urls": {
        "Bug Tracker": "https://github.com/SimpleArt/prettyformatter/issues",
        "Documentation": "https://simpleart.github.io/prettyformatter/",
        "Homepage": "https://simpleart.github.io/prettyformatter/",
        "Repository": "https://github.com/SimpleArt/prettyformatter"
    },
    "split_keywords": [
        "pretty",
        "print",
        "format",
        "indent",
        "dataclass"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "747e05789d89503ac31466b543915a73cc27a8367b21f37fed547252396943d5",
                "md5": "d886972a2397eef20a4d044e18b0091f",
                "sha256": "a719caca66590ca266fb8418bdc2a54e29666c73d4f7609bddd110167c7d45b3"
            },
            "downloads": -1,
            "filename": "prettyformatter-2.0.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d886972a2397eef20a4d044e18b0091f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 15134,
            "upload_time": "2023-05-14T00:29:10",
            "upload_time_iso_8601": "2023-05-14T00:29:10.317904Z",
            "url": "https://files.pythonhosted.org/packages/74/7e/05789d89503ac31466b543915a73cc27a8367b21f37fed547252396943d5/prettyformatter-2.0.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5235bd496971bbfb3f203c074ad0b6ac85cb4bd8eef5502148c76cb692323c99",
                "md5": "b788fbfd6ec9ed444354a2c9798978d0",
                "sha256": "ff6e20da06667a6c07394a82908fb46f747e6d933bfeeaa7532334e19b38c9ab"
            },
            "downloads": -1,
            "filename": "prettyformatter-2.0.13.tar.gz",
            "has_sig": false,
            "md5_digest": "b788fbfd6ec9ed444354a2c9798978d0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 14065,
            "upload_time": "2023-05-14T00:29:13",
            "upload_time_iso_8601": "2023-05-14T00:29:13.019863Z",
            "url": "https://files.pythonhosted.org/packages/52/35/bd496971bbfb3f203c074ad0b6ac85cb4bd8eef5502148c76cb692323c99/prettyformatter-2.0.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-14 00:29:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SimpleArt",
    "github_project": "prettyformatter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "prettyformatter"
}
        
Elapsed time: 0.10052s