Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Nice, intelligent truncation of text.

https://travis-ci.com/icemac/icemac.truncatetext.svg?branch=master https://coveralls.io/repos/github/icemac/icemac.truncatetext/badge.svg?branch=travis Current version on PyPI Supported Python versions

Fork me on: https://github.com/icemac/icemac.truncatetext

Nicely truncate text

Intelligent truncation of text means that truncation does not take place inside a word but only between words. So the required length is only an approximation. The result text might be a bit longer than the required length:

>>> from icemac.truncatetext import truncate
>>> 'I was here.'[:3]
'I w'
>>> truncate('I was here.', 3)
'I was ...'

What can get truncated?

Only instances of basestring (str on Python 3) can be truncated:

>>> truncate(3, 3)
Traceback (most recent call last):
ValueError: 3 is no instance of basestring or None
>>> print(truncate(u'Lorem ipsum', 5))
Lorem ...

None is handled nicely:

>>> truncate(None, 4)
''

What is returned?

Always at least one word is returned even when it is longer than the required length:

>>> truncate('Lorem ipsum', 1)
'Lorem ...'

If the text contains only of one word which is longer than the desired length it is returned without an ellipsis:

>>> truncate('The-really-long-word', 5)
'The-really-long-word'

If the text is shorter than the desired length it is returned without the ellipsis, too:

>>> truncate('Lorem ipsum', 11)
'Lorem ipsum'

Where gets the text truncated?

Truncation also takes place at tabs and linebreaks:

>>> truncate("I was here.\nNow I'm away", 11)
'I was here. ...'
>>> truncate("I was here.\rNow I'm away", 12)
'I was here.\rNow ...'
>>> truncate("I was here.\tNow I'm away", 11)
'I was here. ...'

I do not want as ellipsis.

truncate takes an optional argument which defines the ellipsis string:

>>> print(truncate(u'Lorem ipsum', 5, ellipsis=u':::'))
Lorem :::

Changelog

1.1.1 (2018-11-16)

  • Move repository to Github.

1.1 (2018-11-15)

  • Add support for Python 3.7.

1.0.post1 (2017-06-22)

  • Fix the description so PyPI accepts it.

1.0 (2017-05-13)

  • Add support for Python 3.4 up to 3.6, PyPy2 and PyPy3.

  • Drop support for Python 2.5 and 2.6.

  • Change license from ZPL to MIT.

0.3 (2012-11-13)

  • Moved sources to bitbucket.org.

  • Added Travis-CI.

  • Dropped official support for Python 2.3 and Python 2.4. (Although these rather old Python versions still might be used with this release.)

0.2.1 (2011-07-28)

  • Updated categories: Python 2.7 is supported, too.

0.2 (2009-09-26)

  • Added ability pass a string used as ellipsis string.

0.1 (2009-01-31)

  • Initial public release.

Hacking

Fork me on bitbucket.org or put your issues to the issue tracker.

Release files for icemac.truncatetext 1.1.1

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

Source distribution (sdist)

Source distribution for icemac.truncatetext 1.1.1
File Size Uploaded
icemac.truncatetext-1.1.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for icemac.truncatetext 1.1.1
File Interpreter ABI Platform
icemac.truncatetext-1.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.0 kB

Release files / icemac.truncatetext-1.1.1.tar.gz

Download URL icemac.truncatetext-1.1.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7d8e0ff5ae1caa6b7e8bebb6122074d7fcc7c40259345a8f9e0eca2786cc4a18
BLAKE2b-256 checksum
How to use checksums
b3551e5d31e2948da58193c769eff83bb675a001b439239cdf3ed1ab42aaac98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.10.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.7 CPython/2.7.15

Release files / icemac.truncatetext-1.1.1-py2.py3-none-any.whl

Download URL icemac.truncatetext-1.1.1-py2.py3-none-any.whl
Size 7.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8f412e22a3c7c0ee92a084a0976fce66bdf91f2beb2efaa54fb4c1363004cf10
BLAKE2b-256 checksum
How to use checksums
c013416d89f9981c90a5d42d2866e9d08d78690e6788c68e1c03ed48a6649b26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.10.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.19.7 CPython/2.7.15
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