Skip to main content

Basically textwrap.dedent with t-string support.

Project description

better dedent

PyPI - Version PyPI - Python Version


Like textwrap.dedent, but with t-string support.

For example, given this code string:

code = r"""
def strip_each(lines):
    new_lines = []
    for line in lines:
        new_lines.append(line.rstrip("\n"))
    return new_lines
""".strip("\n")

Using the dedent function with a t-string that uses code in a replacement field would maintain the indentation in a sensible way:

>>> print(dedent(t"""\
...     Example function:
...         {code}
... 
...     That function was indented properly!""")
...
Example function:
    def strip_each(lines):
        new_lines = []
        for line in lines:
            new_lines.append(line.rstrip("\n"))
        return new_lines

That function was indented properly!

Note that using an f-string would not work sensibly:

>>> print(dedent(f"""\
...     Example function:
...         {code}
...
...     That function was NOT indented properly!"""))
Example function:
    def strip_each(lines):
new_lines = []
for line in lines:
    new_lines.append(line.rstrip("\n"))
return new_lines

That function was NOT indented properly!

undent

This package also includes an undent function, which will strip a leading newline (note the lack of \ after t"""):

>>> print(undent(t"""
...     Example function:
...         {code}
...     That function was indented properly!"""))
Example function:
    def strip_each(lines):
        new_lines = []
        for line in lines:
            new_lines.append(line.rstrip("\n"))
        return new_lines
That function was indented properly!

The undent function will also strips a trailing newline by default:

>>> print(undent(t"""
...     Example function:
...         {code}
...     That function was indented properly!
... """))
Example function:
    def strip_each(lines):
        new_lines = []
        for line in lines:
            new_lines.append(line.rstrip("\n"))
        return new_lines
That function was indented properly!
>>> print("Note that there's no blank line above this prompt")
Note that there's no blank line above this prompt

Passing strip_trailing=False to undent will suppress trailing newline removal.

Installation

pip install better-dedent

License

better-dedent is distributed under the terms of the MIT license.

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

better_dedent-0.0.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

better_dedent-0.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file better_dedent-0.0.1.tar.gz.

File metadata

  • Download URL: better_dedent-0.0.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for better_dedent-0.0.1.tar.gz
Algorithm Hash digest
SHA256 05684d2bd2d6859f497379fc86d905f7bcae475c71e96bb417f7c32f6cdcc8b8
MD5 2eb02c2645dc235ceb6f5d01d486e129
BLAKE2b-256 a085d26d90b1717a122cd222a286f6bab2c1182049d8e54469b79729411703bf

See more details on using hashes here.

File details

Details for the file better_dedent-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for better_dedent-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5129616c0b775c8dcb9280523cffb1293ca9eb5bcf15d3e56c872cbb1fe4b7fa
MD5 c9ba4e40d3637d6226b0ca3afa95fe75
BLAKE2b-256 dcf113916706845ae4f328243914e86f8be93426a65a1ad459145b78f47fcede

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