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

Curlifier 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.1.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.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: snipstr-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 7043986f0dd53bff9f9332693cd3f78e1852a6710caedb00e8b2783a799a0681
MD5 370bbc3a0929e57a86e7671b8698b5f6
BLAKE2b-256 9f901e5f54056682f53d9570ad7534491f44e4ca2ff54798c23365d158161e66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: snipstr-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3576b1bdc500a9b81688928577f41ab7ade44dd17398128d0e039b863da7ee7
MD5 218f625b3f7922cf242573408bcf4e7e
BLAKE2b-256 bb05afadd4766c456df1f8f9e3fe185064a749731a3e9c40e8268a86928d8229

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