snipstr
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.
Metadata
Release files for snipstr 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| snipstr-1.0.4.tar.gz | 5.9 kB | Without provenance | Details for snipstr-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| snipstr-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for snipstr-1.0.4-py3-none-any.whl |
Total release size: 13.6 kB
Release files / snipstr-1.0.4.tar.gz
| Download URL | snipstr-1.0.4.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e50fe9d24ff11bfc675c3457b729c2dfa4d645c19cd8371de52034602bd12f4
|
|
BLAKE2b-256 checksum How to use checksums |
df45ae7764f9e397488ada2ab11c675dd8b7b46d76e10934c61ca1a697068f98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.12 Darwin/23.6.0
|
Release files / snipstr-1.0.4-py3-none-any.whl
| Download URL | snipstr-1.0.4-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e51d3686610572dd310dd5f72aa95a7da0b87f8b72bfd12c5feb1044cdf1c94
|
|
BLAKE2b-256 checksum How to use checksums |
011a06f74baa2bb9289525f8609cd1b1a493a081c1a1444e3d459a61087137f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.12 Darwin/23.6.0
|
Security for snipstr 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.