Extend Python syntax by defining custom string prefix
Project description
x-strings
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
to1
- Using
-B
switch forpython
- Calling
sys.dont_write_bytecode = True
in the code
References
Project is inspired by pyxl4.
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
File details
Details for the file x_strings-0.1.2.tar.gz
.
File metadata
- Download URL: x_strings-0.1.2.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-56-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d835f5a6a8ba09a59aa86000350071232a0fb9d142b1ba1afd2cbf5f332ad7e7 |
|
MD5 | db70bda1b3eb79d0373700142386c1c8 |
|
BLAKE2b-256 | 79ff2812d0dc66e4378f9aa836c2231d967cb42ece241289cccded6aaf7b0691 |
File details
Details for the file x_strings-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: x_strings-0.1.2-py3-none-any.whl
- Upload date:
- Size: 2.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.10.6 Linux/5.15.0-56-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cedb86438563af60d267b003679f1f132edb1931b8dbcf5154a77497e826aad3 |
|
MD5 | 98f29f5074fd3c65575b1d51a272d683 |
|
BLAKE2b-256 | fab6841a4993b0ce57350aa063b8bb092df8e930e041d437715ed73955cbc5fc |