Skip to main content

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

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 string
  • safe: str - The processed ASCII string
  • is_truncated: bool - Whether the result was truncated
  • is_empty: bool - Whether the safe string is empty

Methods

  • startswith(prefix: str) -> bool - Check if safe string starts with prefix
  • endswith(suffix: str) -> bool - Check if safe string ends with suffix

String Methods

  • str(safe) - Get the safe string
  • len(safe) - Get length of safe string
  • bool(safe) - Check if safe string is non-empty
  • safe == other - Compare with another StrSafe or string
  • hash(safe) - Get hash of safe string

Processing Pipeline

The library processes strings through these steps:

  1. Transliterate to ASCII with unidecode.
  2. Apply case transformation ('lower', 'upper', or 'unchanged').
  3. Replace or remove disallowed characters.
  4. Collapse consecutive replacement characters.
  5. Strip replacement characters from start/end.
  6. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strsafe-1.0.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strsafe-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

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

Hashes for strsafe-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b2f3382e61740ec05163886a48c1a48b7de551008ceef790e7d0c80e08f65dee
MD5 1868cfed1e09ea179f83293e461403c3
BLAKE2b-256 18ee24de782589ab28efce8997b301461a4b9a2a49d81cda975e10a7217d1124

See more details on using hashes here.

Provenance

The following attestation bundles were made for strsafe-1.0.0.tar.gz:

Publisher: publish.yaml on aceat64/strsafe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for strsafe-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cc889b1d32be9280026d272abc266d8d9728ed58c73e9b11cd3d23a9bb0b6e4
MD5 7c8d3ebe26ba5867661201fad68d8089
BLAKE2b-256 fb2aec6b3d02cecaa87c537a9fb93073291f368c43636f640a9e08ac06c73f96

See more details on using hashes here.

Provenance

The following attestation bundles were made for strsafe-1.0.0-py3-none-any.whl:

Publisher: publish.yaml on aceat64/strsafe

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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