Skip to main content

A simple library for wrapping text to a fixed column width.

Project description

wordwrap

Wordwrap is a Python library for wrapping text to a fixed column width, designed for easy import and use in your own projects.

Installation

Install from source (local):

pip install .

Usage as a library

Import and use the main API:

from wordwrap import WordWrap

result = WordWrap.wrap_text("This is a long line of text.", 10)
if result.is_success():
   print(result.value)  # Wrapped text with lines <= 10 chars
else:
   print(f"Error: {result.error}")

API

wordwrap

Wordwrap is a Python library for wrapping text to a fixed column width, designed for easy import and robust error handling.

Installation

Install from PyPI:

pip install wordwrap

Or from source (local):

pip install .

Usage

Import and use the main API:

from wordwrap import WordWrap

result = WordWrap.wrap_text("This is a long line of text.", 10)
if result.is_success():
    print(result.value)  # Wrapped text with lines <= 10 chars
else:
    print(f"Error: {result.error}")

API Reference

WordWrap.wrap_text(text: str, column_width: int) -> Result

  • Returns a Result object:
    • .value: Wrapped text (string)
    • .error: Error message (string)
  • Input validation:
    • text must be a string
    • column_width must be a positive integer

Features

  • Wraps text to a specified column width, preserving words
  • Handles long words and whitespace correctly
  • Returns a Result object for error handling
  • Python 3.8+

Examples

from wordwrap import WordWrap

# Example 1: Short text
result = WordWrap.wrap_text("word", 10)
print(result.value)  # "word"

# Example 2: Text exactly the column width
result = WordWrap.wrap_text("word123456", 10)
print(result.value)  # "word123456"

# Example 3: Text that requires splitting
result = WordWrap.wrap_text("a long line of text", 10)
print(result.value)  # "a long \nline of \ntext"

# Example 4: Long words
result = WordWrap.wrap_text("word superlongword text", 9)
print(result.value)  # "word \nsuperlong\nword text"

# Example 5: Multiple spaces
result = WordWrap.wrap_text("word    with   spaces", 10)
print(result.value)  # "word    \nwith   \nspaces"

License

MIT


Background

This library is inspired by the Word Wrap kata by Robert C. Martin (Uncle Bob).

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

wordwrap-0.2.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

wordwrap-0.2.2-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file wordwrap-0.2.2.tar.gz.

File metadata

  • Download URL: wordwrap-0.2.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for wordwrap-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a4443ff8148a93d5db4df08900667f06a9608b0f212d3162dfdc07e1789b1f39
MD5 0a3f0d82232bb388ece78a1357e609d0
BLAKE2b-256 522a2356b32a48e1a9406957d013f0efcf45d74b334891714d5ffc5b39739855

See more details on using hashes here.

File details

Details for the file wordwrap-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: wordwrap-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for wordwrap-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7ca20ffc366c5dee69ccd0697a7ad74fd03b151495111ce61ac9cab5c211668d
MD5 74099a896a5241e9bc6af9c7e5bcd4c0
BLAKE2b-256 4bb1f8d2076736d260307a0a089092d909478cb981c462e406eae3ba832d0a4d

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