Byte Commander Byte Commander - 6 months ago 19
Python Question

Possible to make custom string literal prefixes in Python?

Let's say I have a custom class derived from

str
that implements/overrides some methods:

class mystr(str):
# just an example for a custom method:
def something(self):
return "anything"


Now currently I have to manually create instances of
mystr
by passing it a string in the constructor:

ms1 = mystr("my string")

s = "another string"
ms2 = mystr(s)


This is not too bad, but it lead to the idea that it would be cool to use a custom string prefix similar to
b'bytes string'
or
r'raw string'
or
u'unicode string'
.

Is it somehow possible in Python to create/register such a custom string literal prefix like
m
so that a literal
m'my string'
results in a new instance of
mystr
?

Or are those prefixes hard-coded into the Python interpreter?

Answer

Those prefixes are hardcoded in the interpreter, you can't register more prefixes.


What you could do however, is preprocess your Python files, by using a custom source codec. This is a rather neat hack, one that requires you to register a custom codec, and to understand and apply source code transformations.

Python allows you to specify the encoding of source code with a special comment at the top:

# coding: utf-8

would tell Python that the source code encoded with UTF-8, and will decode the file accordingly before parsing. Python looks up the codec for this in the codecs module registry. And you can register your own codecs.

The pyxl project uses this trick to parse out HTML syntax from Python files to replace them with actual Python syntax to build that HTML, all in a 'decoding' step. See the codec package in that project, where the register module registers a custom codec search function that transforms source code before Python actually parses and compiles it. A custom .pth file is installed into your site-packages directory to load this registration step at Python startup time. Another project that does the same thing to parse out Ruby-style string formatting is interpy.

All you have to do then, is build such a codec too that'll parse a Python source file (tokenizes it, perhaps with the tokenize module) and replaces string literals with your custom prefix with mystr(<string literal>) calls. Any file you want parsed you mark with # coding: yourcustomcodec.

I'll leave that part as an exercise for the reader. Good luck!

Note that the result of this transformation is then compiled into bytecode, which is cached; your transformation only has to run once per source code revision, all other imports of a module using your codec will load the cached bytecode.

Comments