Skip to main content

Detects the indentation type and size of any string.

Project description

detect-indent for Python Test

Python port of sindresorhus/detect-indent All attribution goes to that project.

Detect the indentation of code

Pass in a string of any kind of text and get the indentation.

Use cases

  • Persisting the indentation when modifying a file.
  • Have new content match the existing indentation.
  • Setting the right indentation in your editor.

Install

pip install detect-indent

Usage

from detect_indent import detect_indent

sample_string = '''
Example string
  with an indentation
    of two spaces
'''

indent = detect_indent(sample_string)
print(indent)  # {'amount': 2, 'type': 'space', 'indent': '  '}

API

Returns a dictionary with the following stats about indentation:

  • amount {number} - Amount of indentation, for example 2. Will be 0 if indentation could not be detected.
  • type {'tab' | 'space' | None} - Type of indentation. Possible values are 'tab', 'space' or None if no indentation is detected
  • indent {string} - A string representing the actual indentation, for example a string of two spaces.

Algorithm

The current algorithm looks for the most common difference between two consecutive non-empty lines. More Details

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

detect-indent-1.0.0.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

detect_indent-1.0.0-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

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