[Python-ideas] Custom string prefixes
Göktuğ Kayaalp
goktug.kayaalp at gmail.com
Mon May 27 12:41:50 CEST 2013
Hi,
I wanted to share this idea of (not possibly only) mine with Python
Core developers.
I think it would add some significant flexibility to Python to let users
define custom string prefixes. What I mean by a string prefix is,
a letter prefixing the string literal, modifying the behavior of it,
e.g.
>>> r"^\?\? (?P<branch>.*)$"
is a so-called /raw string/ which does not interpret character escapes.
I would suggest that providing ability to define custom string prefixes
would benefit the programmer, especially enrich Python based DSLs.
Let me provide some examples:
- I know that there is a library called `decimal`, which provides
facilities for finer floating point arithmetic. A `Decimal`
class is used to express these numbers and operations, resulting in
>>> decimal.Decimal ("1.6e-9") * decimal.Decimal ("1.0e9")
which is a little bit messy. This can easily be cured by
>>> from decimal import Decimal as D
>>> D ("1.6e-9") * D ("1.0e9")
but I'd enounce that the following is more concise and readable:
>>> D"1.6e-9" * D"1.0e9"
with removed parens.
- I use a little program I wrote instead of make and rake for my daily
computer usage called poke[*], where I do a lot of string
interpolation.
from poke.utils import shell, build as _build
OUTDIR, PROGRAM = "out", "lsvirtues"
def build ():
t0 = _build.CompilationTask (
["laziness.c", "hubris.c", "impatience.c"],
"{outdir}/{progn}".format(outdir=OUTDIR, progn=PROGRAM),
"gcc -Wall -o {outfile} {infiles}")
t0.commit ()
def clean ():
shell.sh ("rm -fr {0} *.o .pokedb".format (OUTDIR))
Even though `str.format (*, **)` is cool, I think using an
'interpolated string' prefix can help clean up stuff a little bit:
# ...
def build ():
t0 = _build.CompilationTask ([...], I"{OUTDIR}/{progn}", ...)
def clean ():
shell.sh (I"rm -fr {OUTDIR} *.o .pokedb")
I thought of subclassing the `str` and overloading some unary operators,
e.g. "~", but, I believe this is not very well suited because
- still requires some mess: ~s("hello {name}")
- or, a monkey-patched `str`: ~"we should{should_we} do that"
- and, a tilde is not as meaningful as a letter.
One can find many other uses too, which saves some redundant method
calls and stuff for a particular task. And such an addition to the
language would be compatible too, in the sense that it will be possible
to run older scripts on a runtime with this implemented.
I'm looking forward to your criticisms and advices. I've searched this
online and asked in the chatroom (#python) and I'm nearly sure that I'm
not asking for a feature that is already present. Being a beginner, I
can say that I'm kind of nervous to post here, where really experienced
people discuss the features of an internationally-adopted language.
[*] https://github.com/gkya/poke
Greetings,
Göktuğ.
--
Göktuğ Kayaalp <goktug.kayaalp at gmail.com>
More information about the Python-ideas
mailing list