Skip to main content

tabstops

Lianes8 Certified

Expand and unexpand tabs like str.expandtabs and the POSIX expand and unexpand tools, with tab stop lists, terminal display columns and streaming.

Install

pip install tabstops

Usage

from tabstops import expand, unexpand

# Tabs to spaces, following str.expandtabs column rules.
expand("name\tvalue")  # -> 'name    value'
expand("a\tb", 4)  # -> 'a   b'
expand("01\t012\n0123\t01234", 4)  # -> '01  012\n0123    01234'

# Spaces back to tabs. Leading blanks only, unless all=True.
unexpand("        x")  # -> '\tx'
unexpand("a       b")  # -> 'a       b'
unexpand("a       b", all=True)  # -> 'a\tb'
unexpand(" \tx")  # -> '\tx'

A lone space after text that happens to reach a stop stays a space, as with the unexpand tool, and expand(unexpand(text)) == expand(text) holds for any text.

Tab stop lists

tabs takes an interval, a list of columns (counted from zero), or the -t syntax of the tools, including the /N (every multiple of N after the list) and +N (every N columns after the last stop) tails.

from tabstops import TabStops, expand, unexpand

expand("a\tb\tc", [3, 10])  # -> 'a  b      c'
expand("a\tb\tc\td", "4,8")  # -> 'a   b   c d'
expand("\t\t\tx", "1,+8")  # -> '                 x'
unexpand("ab    cd", "2,4", all=True)  # -> 'ab\t  cd'

stops = TabStops.parse("2,4,/8")
stops.next_stop(4)  # -> 8
str(stops)  # -> '2,4,/8'

A tab past the last listed stop becomes a single space; blanks past it are left alone.

Display columns

Columns count code points by default. width="display" uses a per-code-point model: East Asian Wide and Fullwidth characters take two columns, combining marks and invisible characters take none. The data comes from Unicode 18.0.0 and does not depend on the Python version.

from tabstops import char_width, display_width, expand

expand("\u4e2d\tx", 4)  # -> '\u4e2d   x'
expand("\u4e2d\tx", 4, width="display")  # -> '\u4e2d  x'
expand("e\u0301\tx", 4, width="display")  # -> 'e\u0301   x'
char_width("\u4e2d")  # -> 2
char_width("\u00b1", ambiguous_wide=True)  # -> 2
display_width("e\u0301\u4e2d!")  # -> 4

Same rules as the tools

By default a carriage return restarts the column, as in str.expandtabs. With posix=True only a newline does, and a backspace moves the column back one place, which is what expand and unexpand do. initial=True expands only the tabs that start a line, like expand -i.

from tabstops import expand

expand("ab\r\tc")  # -> 'ab\r        c'
expand("ab\r\tc", posix=True)  # -> 'ab\r     c'
expand("abc\b\tx", posix=True)  # -> 'abc\b      x'
expand("\ta\tb", 4, initial=True)  # -> '    a\tb'

Streaming

Expander and Unexpander take text in pieces and keep the column across them; expand_stream and unexpand_stream wrap any iterable of strings, such as an open file.

from tabstops import Unexpander, expand_stream

"".join(expand_stream(["a", "\tb\n"], 4))  # -> 'a   b\n'

engine = Unexpander(4)
engine.feed("  ")  # -> ''
engine.feed("  x")  # -> '\tx'

Command line

tabstops expand [-t LIST] [-i] [--display] [--python] [FILE ...]
tabstops unexpand [-t LIST] [-a] [--first-only] [--display] [--python] [FILE ...]

The options follow GNU expand and unexpand (-t implies -a for unexpand, and repeated -t lists are joined), and so do the defaults. --display counts display columns and --python applies the str.expandtabs rules. Input is read as UTF-8; bytes that do not decode are passed through unchanged. python -m tabstops works too.

Two differences from GNU: columns count characters, while GNU counts bytes, so non-ASCII text is measured by code points (or display widths with --display); and the obsolete -8 form of -t 8 is not accepted.

Why

The standard library expands tabs but never compresses them back, has no tab stop lists, and counts a wide CJK character or a combining accent as one column. tabstops gives both directions with one stop model, checked against str.expandtabs and against GNU coreutils, and measures columns with current Unicode data.

The GUIDE documents the complete API, errors, streaming, width limitations and resource use. See the CHANGELOG for upgrades.

License

MIT. See LICENSE. The width table derives from the Unicode Character Database; see LICENSE-UNICODE.

Metadata

Release files for tabstops 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tabstops 1.0.0
File Size Uploaded Provenance Actions
tabstops-1.0.0.tar.gz 37.7 kB Without provenance Details for tabstops-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tabstops 1.0.0
File Interpreter ABI Platform Actions
tabstops-1.0.0-py3-none-any.whl Python 3 none any Details for tabstops-1.0.0-py3-none-any.whl

Total release size: 56.7 kB

Release files / tabstops-1.0.0.tar.gz

Download URL tabstops-1.0.0.tar.gz
Size 37.7 kB
Tags Source
SHA-256 checksum
How to use checksums
be8c11867293e0d8a9669b8a62db0ff5c9550f2961d4de8b363e1d8a45ddc7ad
BLAKE2b-256 checksum
How to use checksums
2bb73c4099b38001fc150146789944e9b93d672f64eb283c1bb1f5e1cfca815a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Release files / tabstops-1.0.0-py3-none-any.whl

Download URL tabstops-1.0.0-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
705557bdea5dda1da42a80530fe657178af0f733b06b8aeb71ee6b02a3dd0550
BLAKE2b-256 checksum
How to use checksums
5d7de461aade4e53c8e2501c61d75496e0a4ac959346ab811303432e85f02cd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for tabstops 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page