Skip to main content

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 to 1
  • Using -B switch for python
  • Calling sys.dont_write_bytecode = True in the code

References

Project is inspired by pyxl4.

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

x_strings-0.1.1.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

x_strings-0.1.1-py3-none-any.whl (2.5 kB view details)

Uploaded Python 3

File details

Details for the file x_strings-0.1.1.tar.gz.

File metadata

  • Download URL: x_strings-0.1.1.tar.gz
  • Upload date:
  • Size: 2.2 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

Hashes for x_strings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d1de7b641d8f104a18749b83a74e9c35c335a9f6ec444bf070d60f9485ddf907
MD5 d1fe9eff1430e4c780aa26990aa30a0c
BLAKE2b-256 3f425445cb1684bbcb7ed609470130535673c59cb92d8902bdc59ec5096ac2fb

See more details on using hashes here.

File details

Details for the file x_strings-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: x_strings-0.1.1-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

Hashes for x_strings-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7f695937decc7a811a33142b4dbf657e6efc69962b9e09be8f0e0715c6b17c63
MD5 8dd820e01a00f433a0dba316c4b2097a
BLAKE2b-256 11a6357bffe2ab9b84a047d99144f07d599dad35806dcb3fd63e5cb188f4d83e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page