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. More advanced examples can be found in here.
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 launcher.py
Hello World!!!
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.0.tar.gz
.
File metadata
- Download URL: x_strings-0.1.0.tar.gz
- Upload date:
- Size: 2.1 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 | b0771c1dad6d09eda23ddf300fb1e2986e24dfeade6777f045c492a2e4b422bf |
|
MD5 | edc1d12d53d8911de1efedf98d5bb37f |
|
BLAKE2b-256 | beec008155311c40fdf6ccb979431bb75a81b2da69350ad19ca96cff5ecd29e8 |
File details
Details for the file x_strings-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: x_strings-0.1.0-py3-none-any.whl
- Upload date:
- Size: 2.5 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 | 4cf03b8ea8219534e15d018547c7ff88a04221e6df50afdd03766afdaa592f83 |
|
MD5 | 49e59fa54019e4adc26dc3c0846827ef |
|
BLAKE2b-256 | 1abff32e96bd61be0365222577c4befd5ccc927deec621a247235895eee3fc88 |