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')
>>> str(s)  # 'Python source co'
>>> s.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.3.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.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: snipstr-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 0ebd5e6ab6b233e0925e5f7f400575e88d53822814808e6ebf13fdada3446024
MD5 5ccdb092521317dbf5d60ba2e281a8c3
BLAKE2b-256 a6e95dfc2a0496ad705f52a3ff0c2bb80c8c13629f8ca596cde636f3d30cbf70

See more details on using hashes here.

File details

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

File metadata

  • Download URL: snipstr-1.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a9633639846ba164494a2f25125fb3146e8ddb7ed8fe0d5810c06bccc6525a4d
MD5 3f982be696b44e16ff9ae481e16d9013
BLAKE2b-256 4582ee0d3889d7522edfe851b9f590e4c7706f31b445f3c7d2358cd9e4d79920

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