Transliterate Unicode strings to 'safe' ASCII strings.
Project description
StrSafe
Transliterate Unicode strings to 'safe' ASCII strings suitable for filenames, identifiers, and other contexts where only basic ASCII characters would be 'safe' or allowed.
Installation
uv
uv add strsafe
pip
pip install strsafe
Requirements
- Python 3.10+
- unidecode
Quick Start
from strsafe import StrSafe
# Basic usage
safe = StrSafe("Héllo Wörld! 🌍")
print(safe) # "hello_world"
print(safe.original) # "Héllo Wörld! 🌍"
# Custom configuration
safe = StrSafe(
"Café & Restaurant",
case="upper",
replacement_char="-",
allow_hyphen=True,
max_length=10
)
print(safe) # "CAFE-RESTA"
API Reference
StrSafe Class
Constructor
StrSafe(
text: str,
*,
case: Literal["lower", "upper", "unchanged"] = "lower",
replacement_char: str = "_",
allow_underscore: bool = True,
allow_hyphen: bool = False,
allow_period: bool = False,
collapse_replacement: bool = True,
strip_replacement: Literal["both", "start", "end", "neither"] = "both",
max_length: int | None = 64,
)
Properties
original: str- The original input stringsafe: str- The processed ASCII stringis_truncated: bool- Whether the result was truncatedis_empty: bool- Whether the safe string is empty
Methods
startswith(prefix: str) -> bool- Check if safe string starts with prefixendswith(suffix: str) -> bool- Check if safe string ends with suffix
String Methods
str(safe)- Get the safe stringlen(safe)- Get length of safe stringbool(safe)- Check if safe string is non-emptysafe == other- Compare with another StrSafe or stringhash(safe)- Get hash of safe string
Processing Pipeline
The library processes strings through these steps:
- Transliterate to ASCII with unidecode.
- Apply case transformation ('lower', 'upper', or 'unchanged').
- Replace or remove disallowed characters.
- Collapse consecutive replacement characters.
- Strip replacement characters from start/end.
- Truncate to
max_length.
Error Handling
# Invalid replacement character
StrSafe("test", replacement_char="ab") # ValueError: replacement_char must be a single character or empty string
# Invalid max_length
StrSafe("test", max_length=-1) # ValueError: max_length must be non-negative or None
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strsafe-1.0.0.tar.gz.
File metadata
- Download URL: strsafe-1.0.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2f3382e61740ec05163886a48c1a48b7de551008ceef790e7d0c80e08f65dee
|
|
| MD5 |
1868cfed1e09ea179f83293e461403c3
|
|
| BLAKE2b-256 |
18ee24de782589ab28efce8997b301461a4b9a2a49d81cda975e10a7217d1124
|
Provenance
The following attestation bundles were made for strsafe-1.0.0.tar.gz:
Publisher:
publish.yaml on aceat64/strsafe
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strsafe-1.0.0.tar.gz -
Subject digest:
b2f3382e61740ec05163886a48c1a48b7de551008ceef790e7d0c80e08f65dee - Sigstore transparency entry: 238043712
- Sigstore integration time:
-
Permalink:
aceat64/strsafe@125e335056990ff1b782a4407a2a6d037ca23cc9 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/aceat64
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@125e335056990ff1b782a4407a2a6d037ca23cc9 -
Trigger Event:
release
-
Statement type:
File details
Details for the file strsafe-1.0.0-py3-none-any.whl.
File metadata
- Download URL: strsafe-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cc889b1d32be9280026d272abc266d8d9728ed58c73e9b11cd3d23a9bb0b6e4
|
|
| MD5 |
7c8d3ebe26ba5867661201fad68d8089
|
|
| BLAKE2b-256 |
fb2aec6b3d02cecaa87c537a9fb93073291f368c43636f640a9e08ac06c73f96
|
Provenance
The following attestation bundles were made for strsafe-1.0.0-py3-none-any.whl:
Publisher:
publish.yaml on aceat64/strsafe
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strsafe-1.0.0-py3-none-any.whl -
Subject digest:
3cc889b1d32be9280026d272abc266d8d9728ed58c73e9b11cd3d23a9bb0b6e4 - Sigstore transparency entry: 238043719
- Sigstore integration time:
-
Permalink:
aceat64/strsafe@125e335056990ff1b782a4407a2a6d037ca23cc9 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/aceat64
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@125e335056990ff1b782a4407a2a6d037ca23cc9 -
Trigger Event:
release
-
Statement type: