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.4.tar.gz (5.9 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.4-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for snipstr-1.0.4.tar.gz
Algorithm Hash digest
SHA256 8e50fe9d24ff11bfc675c3457b729c2dfa4d645c19cd8371de52034602bd12f4
MD5 232c2ef6a191d3a74a37848393516ad9
BLAKE2b-256 df45ae7764f9e397488ada2ab11c675dd8b7b46d76e10934c61ca1a697068f98

See more details on using hashes here.

File details

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

File metadata

  • Download URL: snipstr-1.0.4-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.12 Darwin/23.6.0

File hashes

Hashes for snipstr-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e51d3686610572dd310dd5f72aa95a7da0b87f8b72bfd12c5feb1044cdf1c94
MD5 086ba6be87101a202b32b63ef3f9bcf4
BLAKE2b-256 011a06f74baa2bb9289525f8609cd1b1a493a081c1a1444e3d459a61087137f0

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