Skip to main content

Escapism

Simple escaping of text, given a set of safe characters and an escape character.

Usage

Not much to it. Two functions:

escaped = escapism.escape('string to escape')
# 'string_20to_20escape'
original = escapism.unescape(escaped)

There are two optional arguments you can pass to escape():

  • safe: a string or set of characters that don't need escaping. Default: ascii letters and numbers.
  • escape_char: a single character used for escaping. Default: _. escape_char will never be considered a safe value.

unescape() accepts the same escape_char argument as escape() if a value other than the default is used.

import string
import escapism
safe = string.ascii_letters + string.digits + '@_-.+'
escape_char = r'%'
escaped = escapism.escape('foø-bar@%!xX?', safe=safe, escape_char=escape_char)
# 'fo%C3%B8-bar@%25%21xX%3F'
original = escapism.unescape(escaped, escape_char=escape_char)

Slugs

escapism 1.1 adds a safe_slug API (extracted from kubespawner 7).

When to use safe_slug instead of escape:

  • you don't need to recover the original string (safe_slug is lossy), but you do need:
  • length requirements
  • start/end rules
  • arbitrary other validity requirements
  • uniqueness

safe_slug takes an is_valid callable, which you can use to specify whether a key passes a validity check. If it passes, the string is returned unmodified.

The default is_valid callable applies a strict subset of various kubernetes rules, so it will always return a unique string that is valid in just about any kubernetes field (object name, label values, etc.)

  • min length of 1
  • max length of 63
  • contains only lowercase ascii letters, numbers, and '-'
  • starts with a letter
  • ends with a letter or number

If the input string does not pass the is_valid check, it is stripped and hashed to ensure validity and uniqueness.

This does:

  • cast to lowercase
  • strip all non-alphanumeric characters
  • if it doesn't start with a letter, prefix with x-
  • truncate the safe subset
  • append a hash of the original name, utf8-encoded, after ---

Examples:

safe_slug("valid-slug") # "valid-slug"
safe_slug("4start") # "x-4start---3a570dbd"
safe_slug("a" * 64, max_length=20) # "aaaaaaaaa---ffe054fe"
safe_slug("üñîçø∂é") # "x---4072f5a6"

Note: the 'safe' result is not customizable, which means that safe_slug can only be used if this trim-and-hash result is valid in your scheme.

Release files for escapism 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for escapism 1.1.0
File Size Uploaded
escapism-1.1.0.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for escapism 1.1.0
File Interpreter ABI Platform
escapism-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / escapism-1.1.0.tar.gz

Download URL escapism-1.1.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
add130e48a85bb1aaea3e74fb031f5033c7055aedb39a3265f923d5f40c3f974
BLAKE2b-256 checksum
How to use checksums
d289e7d4dc55d41be5f0999be2a28fd0d4864b4222e9ae4292732e31aaa46696
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 11, 2025.

Transparency log

Release files / escapism-1.1.0-py3-none-any.whl

Download URL escapism-1.1.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f35bf68009704cbd55dd834add47cc964c12e760cf08749f7cbe60ed882963a0
BLAKE2b-256 checksum
How to use checksums
f8669f198b16f7551271c32ff0961f21388c21b0d8a4796a1aa1d25b25c0340d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 11, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page