Skip to main content

A lightweight library for easy-to-use text truncation with a friendly interface.

Project description

snipstr

codecov tests pypi package version status pypi downloads supported python versions wemake-python-styleguide mypy ruff license Poetry

SnipStr logo

A lightweight library for easy-to-use text truncation with a friendly interface.

Installation

pip install snipstr

Usage

All you need is to import SnipStr:

>>> from snipstr import SnipStr
>>> text = 'Python source code and installers are available for download for all versions!'
>>> s = SnipStr(text)
>>> s.snip_to(16).by_side('right').with_replacement_symbol()
>>> str(s)  # 'Python source...'
>>> s.by_side('left')
>>> str(s)  # '...all versions!'
>>> s.snip_to(30)
>>> str(s)  # '... download for all versions!'
>>> s.source  # 'Python source code and installers are available for download for all versions!'

Hashing

SnipStr instances are hashable, which allows them to be used as dictionary keys or in sets. The hash is computed based on all instance attributes: source, snip_to, by_side, with_replacement_symbol.

>>> from snipstr import SnipStr
>>> s1 = SnipStr('Hello, World!')
>>> s1.snip_to(8).by_side('right').with_replacement_symbol()
>>> s2 = SnipStr('Hello, World!')
>>> s2.snip_to(8).by_side('right').with_replacement_symbol()
>>> hash(s1) == hash(s2)  # True

Collections

Since SnipStr is hashable and supports equality, instances can be added to sets and used as dictionary keys:

>>> from snipstr import SnipStr
>>> s1 = SnipStr('Hello')
>>> s1.snip_to(3)
>>> s2 = SnipStr('Hello')
>>> s2.snip_to(3)
>>> s3 = SnipStr('World')
>>> s3.snip_to(3)
# Using in sets (duplicates are removed)
>>> snip_set = {s1, s2, s3}
>>> len(snip_set)  # will return 2
# Using as dictionary keys
>>> snip_dict = {s1: 'first', s3: 'second'}
>>> snip_dict[s2]  # will return 'first', s2 equals s1

Comparison

SnipStr instances support rich comparison operations based on their target length:

>>> from snipstr import SnipStr
>>> short = SnipStr('Hello, World!')
>>> short.snip_to(5)
>>> long = SnipStr('Hello, World!')
>>> long.snip_to(10)
>>> short < long  # True
>>> short <= long  # True
>>> long > short  # True
>>> long >= short  # True

Equality

Two SnipStr instances are equal if all instance attributes (source, snip_to, by_side, with_replacement_symbol) match:

>>> from snipstr import SnipStr
>>> s1 = SnipStr('Hello')
>>> s1.snip_to(3).by_side('right')
>>> s2 = SnipStr('Hello')
>>> s2.snip_to(3).by_side('right')
>>> s3 = SnipStr('Hello')
>>> s3.snip_to(3).by_side('left')
>>> s1 == s2  # will return True
>>> s1 == s3  # Will return False 

License

SnipStr is released under the MIT License. See the bundled LICENSE file for details.

The logo was created using Font Meme.

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

snipstr-1.0.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

snipstr-1.0.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file snipstr-1.0.2.tar.gz.

File metadata

  • Download URL: snipstr-1.0.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/23.6.0

File hashes

Hashes for snipstr-1.0.2.tar.gz
Algorithm Hash digest
SHA256 74d7c88eb1db3c2a1d8fd19d94db4b1258589aa4e9becdda988d0968fcb904b5
MD5 61e73d365a802e95832bd5e6c7a48396
BLAKE2b-256 7592d6778770ad9460b77a5163ec254a38980db5703075703046af5468e7d9e6

See more details on using hashes here.

File details

Details for the file snipstr-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: snipstr-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/23.6.0

File hashes

Hashes for snipstr-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 afd2512374f9a4af783f849a76a1fe3026011eb3977303f3d478a1449295bbce
MD5 03afd9fdabfb0c30384022a60b773503
BLAKE2b-256 bf3c130b758a01bfa6be635259829d7ae664c1710bb68b9880c996cf7a7822d1

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