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
Resultobject:.value: Wrapped text (string).error: Error message (string)
- Input validation:
textmust be a stringcolumn_widthmust 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wordwrap-0.2.4.tar.gz.
File metadata
- Download URL: wordwrap-0.2.4.tar.gz
- Upload date:
- Size: 9.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edc6771625854dd3c72c61fddb81635f8969fc185e6f515fbf2f4ee975509942
|
|
| MD5 |
f4237fb5982f412f2eb49e32d5a12e66
|
|
| BLAKE2b-256 |
26c62792dccb833222d633128a4297f881f3fdce857e5b951c969fd5fa0d415f
|
File details
Details for the file wordwrap-0.2.4-py3-none-any.whl.
File metadata
- Download URL: wordwrap-0.2.4-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
891ec10158972163503236e5a2ee9acbb0e1dcb914a32d7a3aae4cad8fb42397
|
|
| MD5 |
03d68d32cafb1ea43f3e58898c9df133
|
|
| BLAKE2b-256 |
629035ac750bb8114916fc4238d6b8fed6a009f5a7f9d0e241d14554c6b6515d
|