Paragraphs

1.0.1 · maintenance · verified Thu Apr 16

Paragraphs is a Python library designed to simplify the incorporation of long strings into Python code, making them more readable and maintainable. It provides a simple function to format multi-line strings, aiming to prevent common formatting issues that arise with extensive text blocks in code. The current version is 1.0.1, and its release cadence appears to be infrequent, with the last update in 2019.

Common errors

Warnings

Install

Imports

Quickstart

The `par` function takes a multi-line string (often a triple-quoted string) and formats it, typically by dedenting and re-wrapping it to remove extraneous whitespace and improve readability. This is particularly useful for constructing human-readable messages or documentation within the code.

from paragraphs import par

long_text = par(
    """
    This is a very long string that needs to be incorporated
    into Python code without looking messy. The 'paragraphs'
    library helps format such text blocks beautifully, handling
    line breaks and indentation consistently. It's especially
    useful for error messages, documentation strings, or large
    text data that needs to be easily editable and readable
    within the code itself.
    """
)

print(long_text)
# Expected output will be the text, re-wrapped and dedented automatically by 'par'.
# The exact wrapping depends on the internal logic of 'par'.

# Example of usage within an exception message:
class CustomError(Exception):
    def __init__(self, detail: str):
        self.detail = detail

    def __str__(self):
        return par(
            f"""
            An error occurred: {self.detail}. This message is designed
            to be human-readable and automatically formatted by 'par'.
            The original issue was due to an unexpected input value.
            Please review the input and try again.
            """
        )

try:
    raise CustomError("Invalid configuration file")
except CustomError as e:
    print(e)

view raw JSON →