[Python-ideas] Custom string prefixes

Haoyi Li haoyi.sg at gmail.com
Mon May 27 21:41:29 CEST 2013


The scala people intend to use their version of this for things like xml
literals:

snippet = xml"<h>hello world!</h1>"

Which are parsed and interned at compile time. Generally, it would be
useful for embedding snippets of non-python code in strings within a python
program, and have them resolved at import-time and interned to prevent
run-time errors or performance overheads.

I'm thinking of regexes and libraries like
Parsimonious<https://github.com/erikrose/parsimonious>,
which take literal strings to do stuff with, would benefit from this. No
more "should I or should I not re.compile() my
regexes<http://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile>",
no more worrying about whether the ad-hoc global compiled-regex cache is
going to start evicting my regexes. For the vast majority of cases, regexes
are static literals and can be compiled at import time without any fuss or
global side effects (e.g. evicting others from the cache).


On Mon, May 27, 2013 at 2:35 PM, Devin Jeanpierre <jeanpierreda at gmail.com>wrote:

> On Mon, May 27, 2013 at 1:18 PM, Göktuğ Kayaalp
> <goktug.kayaalp at gmail.com> wrote:
> > A possible solution is:
> >
> >     - In parse time, any string literal is a /raw string/, regardless of
> >       what prefix it has or if it even has a prefix.
> >
> >     - The /raw string/ is then passed to user-land in this raw state,
> >       and then, if no prefix is applied, it is parsed as a standard
> >       string, otherwise the requested prefix is applied.
> >
> >     - In case of a user-land raw string (e.g. r"yo"), the prefix
> >       function can be the identity function (e.g. f(x) = x).
> >
> > This is possibly not the most ideal solution, but it is a solution.
>
> This is beginning to sound like E-style quasiliterals.
>
> http://www.erights.org/elang/grammar/quasi-overview.html
>
> -- Devin
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130527/5ab5c674/attachment.html>


More information about the Python-ideas mailing list