Skip to main content

Programmatically increase and decrease text indentation.

Project description

Indenter

pypi badge

Indenter is a Python package that assists with programmatically indenting text to arbitrary levels using with blocks and +/- operators. It has zero dependencies.

Use Indenter to get easy structured output, even as your control flow weaves through multiple functions:

from indenter import Indenter

def validate_email(ind, email):
  print(ind + 'Validating email...')
  # ...

def parse_date(ind, date):
  print(ind + f'Parsing date {date}')
  # ...
  with ind:
    print(ind + 'Checking components')
    y, m, d = date.split('-')
  print(ind + 'Date is valid!')

def get_user_record(ind):
  print(ind + 'Fetching user from database...')
  validate_email(ind + 1, 'ben@twos.dev')
  birthday = parse_date(ind + 1, '1990-08-21')
  print(ind + 'Fetched user!')
  # ...

ind = Indenter(symbol='→ ')
get_user_record(ind)

Output:

Fetching user from database...
→ Validating email...
→ Parsing date 1990-08-21
→ → Checking components
→ Date is valid!
Fetched user!

Getting started

Requirements

  • Python 3.6+

Installing

pip install indenter

Usage

from indenter import Indenter
with Indenter() as ind:
  print(ind + "Text to be indented")

# Output:
#   Text to be indented

You can nest an arbitrary number of with calls:

from indenter import Indenter
ind = Indenter()
with ind:
  print(ind + "I'm one level deep")
  with ind:
    print(ind + "I'm two levels deep")
  print(ind + "I'm one level deep again")

# Output:
#   I'm one level deep
#     I'm two levels deep
#   I'm one level deep again

You can manually adjust the indentation level using +/-. Use this to indent output by nested function calls correctly:

import logging
from indenter import Indenter

ind = Indenter()

func do_work(ind):
  logging.debug(ind + "Doing some work")

func do_business_logic():
  ind = Indenter()
  logging.debug(ind + "Doing some business logic")
  do_work(ind + 1)
  logging.debug(ind + "Finishing some busines logic")

do_some_business_logic()

# Output:
# Doing some business logic
#   Doing some work
# Finishing some busines logic

Customization

The default indentation symbol is two spaces. You can override this by passing symbol:

from indenter import Indenter

# Indent with arrows
with Indenter(symbol="→ ") as ind:
  print(ind + "I'm indented with an arrow")
  with ind:
    print(ind + "I'm indented with two arrows")

# Indent with 4 spaces per level
with Indenter(symbol="    ") as ind:
  print(ind + "I'm indented by four spaces")

# Indent with tabs
with Indenter(symbol="\t") as ind:
  print(ind + "I'm indented by one tab")

# Output:
# → I'm indented by an arrow
# → → I'm indented by two arrows
#     I'm indented by four spaces
# 	I'm indented by one tab

When using custom symbols, nested withs will inherit the symbol of their parent. If you need more than one type of indentation at once, you must make and manage multiple Indenters.

The indenter starts at zero levels of indentation by default, and increases by one level for each with block. This can be overridden to start at other levels, such as 1:

from indenter import Indenter
ind = Indenter(start=1)
print(ind + "First-level indentation")
with ind:
  print(ind + "Second-level indentation")

# Output:
#   First-level indentation
#     Second-level indentation

Contributing

I welcome contributions and foster an inclusive environment.

Testing

Testing requires pytest (pip install pytest).

pytest

Releasing

(mostly notes for myself)

To release, first bump the version number in setup.cfg, commit the result, then run:

rm -rf dist
pip install build twine
python -m build --wheel
twine check dist/* # optional; check for common issues
twine upload dist/*

For ease of browsing by users, you should also cut a release on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

indenter-1.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file indenter-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: indenter-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.10.1

File hashes

Hashes for indenter-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9167e3cc61d6ddfbbe5892fb7a288a52cf640dda516beace756192fd57bbbb0e
MD5 217d2d26daf076372345e45e93e0f084
BLAKE2b-256 088504213e58c39c012490867d81f1729de04f921dcc3b96052ee5ba4bb75eac

See more details on using hashes here.

Supported by

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