Skip to main content

A lightweight pretty-printer for data structures.

Project description

BeautiPy

PyPI version Python Versions License: MIT

A lightweight pretty-printer for complex, nested data structures and structured text. Improves readability by adding indentation and line breaks.

Features

  • Works with any object via its str() representation
  • Handles non-standard structured text
  • Configurable indentation and formatting rules
  • Simple and dependency-free
  • Includes a CLI tool

Installation

pip install beautipy

Or using uv:

uv add beautipy

Requires Python 3.8 or higher.

Quick Start

Python

from beautipy import beautify

data = {'method': 'GET', 'status': 200}
print(beautify(data))

Output:

{
    'method': 'GET',
    'status': 200
}

Terminal

cat data.txt | beautipy > formatted.txt

You can also run as a module: python -m beautipy.

Usage

Python API

The core functionality is provided by the beautify function.

from beautipy import beautify

beautify(
    obj: object,
    *,
    blank_line_depth: int = 0,
    opener_same_line: bool = False,
    compact_operators: bool = False,
    expand_empty: bool = False,
    indent: str = '    '
) -> str
Parameter Type Default Description
obj object - Object to format. If not a string, str(obj) is used.
blank_line_depth int 0 Number of initial nesting levels that get a blank line.
opener_same_line bool False Keep {, [, ( on the same line.
compact_operators bool False Omit spaces around = and :.
expand_empty bool False Expand empty structures (e.g. {}) to multiple lines.
indent str ' ' Indentation string (4 spaces by default).

Raises: ValueError if blank_line_depth < 0.

Command Line

BeautiPy can also be used directly from the terminal.

# From file
beautipy < messy.json

# From stdin
echo '[1,2,3]' | beautipy

# From argument
beautipy '{"key":"value"}'

# Multiple options
beautipy --indent '  ' -s '{key:value}'

Options

  • -b, --blank-line-depth N: Add blank lines at depth N. Default is 0.
  • -s, --opener-same-line: Keep opening brackets inline.
  • -o, --compact-operators: Do not add spaces around = and :.
  • -e, --expand-empty: Expand empty structures (e.g. []).
  • -i, --indent STR: Set indentation string. Default is ' ' (4 spaces).
  • --version: Show version information.

Non-standard Structured Text

Since BeautiPy operates on text rather than parsing, it can format custom structured text and does not require valid JSON, Python, or any specific syntax:

text = 'Arya[age:9,parents:("mother"="Tara",father:Bardia),children:{}]'
print(beautify(text))

Output:

Arya
[
    age: 9,
    parents: 
    (
        "mother" = "Tara",
        father: Bardia
    ),
    children: {}
]

More usage examples can be found in the examples/ directory:

How It Works

BeautiPy operates purely at the character level:

  1. Reads input as text
  2. Identifies structural characters: {, [, (, ,, etc.
  3. Applies formatting rules (indentation, spacing, newlines)

This allows reasonable output for custom or made-up syntax.

Notes

  • Not a parser: Cannot extract or modify data — no semantic understanding
  • Not a validator: Does not check syntactic correctness

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository
  2. Create a feature branch (git switch -c feature/amazing-feature or git checkout -b feature/amazing-feature)
  3. Install the package in development mode with dev dependencies:
    • pip install -e ".[dev]" or uv sync --extra dev
  4. Run tests: pytest (from the project root)
  5. Submit a pull request

Please ensure:

  • Code follows PEP 8
  • Changes are well-documented

License

MIT License — free to use, modify, and distribute. See the LICENSE file for details.

Links

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

beautipy-0.1.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beautipy-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file beautipy-0.1.0.tar.gz.

File metadata

  • Download URL: beautipy-0.1.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for beautipy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06844439bb85d95cfe7ee0c42fec743696436639ebbbb64e8119f57d0036d106
MD5 42cec5fdd137a3a3bdadf08141396940
BLAKE2b-256 cdb4915f3ec048178effec7032387867f81f17196bc3874fc73e49a3496a1cce

See more details on using hashes here.

File details

Details for the file beautipy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: beautipy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.10

File hashes

Hashes for beautipy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b13667c0cc1bb8cfa76a5301c22336e93d70ca0b5adeb5d1ec0447f0654a80a7
MD5 5f5e64b0b39625fcc88e99279d57edfe
BLAKE2b-256 5eb97277c5b4cfbb2c37aaf73629a8fd33b34e98805355cec0451823c938f40d

See more details on using hashes here.

Supported by

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