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 '  ' -b 2 -s '{k1:[v11,v12], k2:[v21,v22]}'

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.1.tar.gz (14.9 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.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: beautipy-0.1.1.tar.gz
  • Upload date:
  • Size: 14.9 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.1.tar.gz
Algorithm Hash digest
SHA256 5aa7a8f072896498120894defeb563cf31be0ab4a96573ea5e1b1522171e21fa
MD5 383d632cc450397e4b8c1e308d2c0c3a
BLAKE2b-256 c52390dbf86887802368eb31ec9601fc7a4593693407c23db6510bf1e7eddc4b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: beautipy-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cf2c62e645c62d41ae8442d5c136e6566dd213dc558342f99db040a5b95b7553
MD5 7fbac6ae0bffc05bb557a3cd8c79425e
BLAKE2b-256 73fe3d08c850660a5b4e4ff38e76f4a0f096416695fa9d5cf510c5045ee1444e

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