Skip to main content

x-strings

PyPI - Version PyPI - License PyPI - Downloads

Extend Python syntax by defining custom string prefix. Provide function that transforms strings with your prefix.

This corresponds to Tagged Template Literals feature in JavaScript.

This project is only a toy. Technique used here shouldn't be considered as a good coding practice due to the fact how it is implemented. I don't recommend using it in production code. Hopefully it can be inspiring though.

Features

  • Multiple encodings can be defined
  • Multiple prefixes can be defined
  • Prefixes can have more that one letter
  • Encodings can take arguments (captured by regex)
  • Doesn't corrupt Python syntax-error messages

Examples

Simple example is shown below.

app.py:

# coding: x-strings
print(x"Hello World")

launcher.py:

import xstrings
xstrings.register({'x': lambda t: t + "!!!"})

import app

Notice exclamation marks added to the end of the message:

$ python3 -B examples/launcher.py
Hello World!!!

This and more advanced examples can be found in here.

Caching

By default Python stores decoded code in __pycache__. Source file needs to be changed to get cache re-generated. This may complicate development of launcher.py. You can prevent Python from generating cache by either:

  • Setting env var: PYTHONDONTWRITEBYTECODE to 1
  • Using -B switch for python
  • Calling sys.dont_write_bytecode = True in the code

References

Project is inspired by pyxl4.

Metadata

Release files for x-strings 0.1.2

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

Source distribution (sdist)

Source distribution for x-strings 0.1.2
File Size Uploaded
x_strings-0.1.2.tar.gz 2.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for x-strings 0.1.2
File Interpreter ABI Platform
x_strings-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 4.9 kB

Release files / x_strings-0.1.2.tar.gz

Download URL x_strings-0.1.2.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d835f5a6a8ba09a59aa86000350071232a0fb9d142b1ba1afd2cbf5f332ad7e7
BLAKE2b-256 checksum
How to use checksums
79ff2812d0dc66e4378f9aa836c2231d967cb42ece241289cccded6aaf7b0691
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-56-generic

Release files / x_strings-0.1.2-py3-none-any.whl

Download URL x_strings-0.1.2-py3-none-any.whl
Size 2.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cedb86438563af60d267b003679f1f132edb1931b8dbcf5154a77497e826aad3
BLAKE2b-256 checksum
How to use checksums
fab6841a4993b0ce57350aa063b8bb092df8e930e041d437715ed73955cbc5fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-56-generic

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

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